Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created June 8, 2010 15:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cowboy/430200 to your computer and use it in GitHub Desktop.
Save cowboy/430200 to your computer and use it in GitHub Desktop.
PHP "JSON heredoc" for inline data.. good? bad? comments? I'm trying to keep it simple.
<?php
## Parse query string.
$sb_league = $_GET['league'] ? strtolower( $_GET['league'] ) : 'all';
## Nav data, as a JSON heredoc. Much less verbose than PHP arrays!
$navs = json_decode(<<<JSON
{
"mlb": {
"title_link": "http://stats.boston.com/mlb/scoreboard.asp",
"nav": [
{ "title": "Full Schedule", "url": "http://stats.boston.com/mlb/teamreports.asp?tm=02&report=schedule" },
{ "title": "Statistics", "url": "http://stats.boston.com/mlb/teamreports.asp?yr=2009&tm=2&btnGo=Go&report=stats" },
{ "title": "Standings", "url": "http://stats.boston.com/mlb/standings.asp" }
]
},
"nfl": {
"title_link": "http://stats.boston.com/fb/scoreboard.asp",
"nav": [
{ "title": "Full Schedule", "url": "http://stats.boston.com/fb/teamstats.asp?yr=2009&tm=17&btnGo=Go&type=schedules" },
{ "title": "Statistics", "url": "http://stats.boston.com/fb/teamstats.asp?teamno=17&type=stats" },
{ "title": "Standings", "url": "http://stats.boston.com/fb/totalstandings.asp" },
{ "title": "Season timeline", "url": "http://timelines.boston.com/patriots" }
]
},
"nhl": {
"title_link": "http://stats.boston.com/nhl/scoreboard.asp",
"nav": [
{ "title": "Full Schedule", "url": "http://stats.boston.com/nhl/teamstats.asp?teamno=01&btnGo=Go&type=schedule" },
{ "title": "Statistics", "url": "http://stats.boston.com/nhl/teamstats.asp?teamno=01&btnGo=Go&type=stats" },
{ "title": "Standings", "url": "http://stats.boston.com/nhl/standings_conference.asp" },
{ "title": "Season timeline", "url": "http://timelines.boston.com/bruins#/seasons/2009" }
]
},
"nba": {
"title_link": "http://stats.boston.com/nba/scoreboard.asp",
"nav": [
{ "title": "Full Schedule", "url": "http://stats.boston.com/nba/teamstats.asp?teamno=02&btnGo=Go&type=schedule" },
{ "title": "Statistics", "url": "http://stats.boston.com/nba/teamstats.asp?teamno=02&btnGo=Go&type=totstats" },
{ "title": "Standings", "url": "http://stats.boston.com/nba/standings_conference.asp" },
{ "title": "Season timeline", "url": "http://timelines.boston.com/celtics" }
]
}
}
JSON
,true) or die( 'Invalid JSON, please validate at www.jsonlint.com.' );
$nav = $navs[ $sb_league ];
## Output.
?><div class="scorebelt">
<div class="scorebelt-nav">
<? if ( $nav['title_link'] ): ?>
<h3><a href="<?= $nav['title_link'] ?>">Scoreboard</a></h3>
<? else: ?>
<h3>Scoreboard</h3>
<? endif; ?>
<ul>
<? foreach ( $nav['nav'] as $item ): ?>
<li><a href="<?= $item['url'] ?>"><?= $item['title'] ?></a></li>
<? endforeach; ?>
</ul>
</div>
</div>
@jdsharp
Copy link

jdsharp commented Jun 8, 2010

I love it, very clean way to abstract out and embed some data.

@iansym
Copy link

iansym commented Jun 8, 2010

This is definitely interesting as I've always hated writting nested PHP arrays. Would be curious to get a performance benchmark for it though as there is an extra step with the json_decode() call.

@cowboy
Copy link
Author

cowboy commented Jun 8, 2010

Fortunately, this will be run periodically via cron, so performance optimizations at that level are a non-issue.

@jaubourg
Copy link

jaubourg commented Jun 8, 2010

I like it too.

I put the whole data on top and made the json_decode and json writing at the same time. Simplified a bit, nothing fancy: http://gist.github.com/430229

Performance could be an issue but I use json_encode/json_decode extensively in my php projects and they seem performant enough. You could always put the data in a secondary file and have an helper class that would cache the corresponding serialized php data structure... though I'm not sure if it would be any faster in the end due to more file input/output.

What would be awesome is a php extension that would pre-process inlined JSON... one can only dream...

... or use Node.js :P

@cowboy
Copy link
Author

cowboy commented Jun 8, 2010

You know, I had considered trying this pattern, but totally forgot. Thanks!!

$navs = json_decode(<<<JSON
{
  "foo": "bar"
}
JSON
,true);

@joshvarner
Copy link

Just remember that no matter how efficiently json_decode() performs here, the fact that you're not using native PHP arrays will bypass any opcode caching systems like APC or xcache. You also delay syntax checking to the moment json_decode() is called. Just thought I'd throw those thoughts out there. :)

@cowboy
Copy link
Author

cowboy commented Jun 10, 2010

voxwerk - thanks for the info. As a primarily JavaScript developer, I only intended this approach to be used as a "quick hack" to manage simple data structures for basic one-off scripts, not as a replacement for anything more robust. And of course, with the corresponding PHP TextMate bundle JSON heredoc syntax highlighting patch, syntax errors shouldn't be an issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment