Author Topic: [PHP] Organizing a JSON Array  (Read 23927 times)

0 Members and 2 Guests are viewing this topic.

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Organizing a JSON Array
« Reply #15 on: April 13, 2010, 08:17:21 pm »
Fine, print vs printf aside, in his very first post he said it could be easily adapted to any kind of array, so assuming the input will be JSON formatted is bad.

No, not really.  He wrote a regular expression because he wasn't aware of the built in JSON capabilities.  In reality, it should be using json_decode.

If you're writing a generic parser, then regular expressions is a reasonable way to do it, but it's not the most efficient.  That's what I'm getting at.  I'm sure I'd elect to use regex instead of writing a lexer, tokenizer, parser, etc.  However, you were nitpicking on matters of efficiency, and it didn't seem appropriate to do that and use regular expressions in the same breath...

I'm pointing out a double-standard, not saying using regular expressions is inappropriate.

And as for readability, comments exist for a reason, although they should be used to explain why, not how (I'll give you that one).  Additionally, changing " to ' boosts performance and doesn't mangle readability like you seem to be implying.

Comments only go so far.  They don't make a hideously mangled line of code easier to follow or modify...

The performance boost isn't worth considering for almost all applications, and it can easily mangle readability.  If you're printing HTML, it's much easier to keep track of where you are and what quotes/braces need closed if you use printf instead of print with single ticks.

Of course I'm willing to grant that there's no reason to do

Code: [Select]
print "1234";
Instead of

Code: [Select]
print '1234';,

but I think it's absurd to bring it up as if it's going to make some sort of valuable difference in the code's performance.

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [PHP] Organizing a JSON Array
« Reply #16 on: April 13, 2010, 08:58:20 pm »
Well then fine, eat this: with single quotes in printing you won't have to escape all the double quotes for HTML entities.

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Organizing a JSON Array
« Reply #17 on: April 13, 2010, 09:13:39 pm »
Well then fine, eat this: with single quotes in printing you won't have to escape all the double quotes for HTML entities.

But you have to break the string to insert any sort of variable.  This is why I prefer printf.

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [PHP] Organizing a JSON Array
« Reply #18 on: April 14, 2010, 08:43:22 pm »
Well then fine, eat this: with single quotes in printing you won't have to escape all the double quotes for HTML entities.

But you have to break the string to insert any sort of variable.  This is why I prefer printf.
There are no variables in constant strings.

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Organizing an Array
« Reply #19 on: April 14, 2010, 09:36:17 pm »
I'm talking about stuff like this:


print '<form act=' $_SERVER['PHP_SELF'] . ' method="POST">';


In this case, I don't think it makes any sort of difference, but I think you run into far fewer problems if you stick to doing something like this:


printf
('<form action="%s" method="post">'$_SERVER['PHP_SELF']);


Amusingly, in this case, it's equivalent to just not specifying an action, but whatever. ;P

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [PHP] Organizing a JSON Array
« Reply #20 on: April 15, 2010, 01:16:37 pm »
I don't know anything about WoW, but it sounded to me like this script would be called quite often to get new values of....stuff.

Maybe once a month, if that. This script was being used to generate coordinate pairs to put into a route file.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [PHP] Organizing a JSON Array
« Reply #21 on: April 15, 2010, 10:12:18 pm »
Well then fine, eat this: with single quotes in printing you won't have to escape all the double quotes for HTML entities.
Html can have double or single quotes, so both of these are equally valid:

print "<a href='http://www.google.ca/'>";

print '<a href="http://www.google.ca/">';

Offline Chavo

  • x86
  • Hero Member
  • *****
  • Posts: 2219
  • no u
    • View Profile
    • Chavoland
Re: [PHP] Organizing a JSON Array
« Reply #22 on: April 16, 2010, 12:10:18 am »
He's referring to the php rendering.  Single quotes are treated as string literals, double quotes are parsed for variables.  Citation
« Last Edit: April 16, 2010, 12:12:16 am by Chavo »

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [PHP] Organizing a JSON Array
« Reply #23 on: April 17, 2010, 02:09:14 am »
He's referring to the php rendering.  Single quotes are treated as string literals, double quotes are parsed for variables.  Citation
You aren't wrong about the facts, but that's not at all what he said in the post I quoted. He talked about "escaping double quotes for HTML entities".

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: [PHP] Organizing a JSON Array
« Reply #24 on: June 23, 2010, 10:54:48 am »
I don't want to necropost.. But what is wrong with heredoc?
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: [PHP] Organizing a JSON Array
« Reply #25 on: June 23, 2010, 02:37:57 pm »
I don't want to necropost.. But what is wrong with heredoc?

They don't do anything better than other methods, and I'd be willing to bet 90%+ of php programmers don't know about that.
And like a fool I believed myself, and thought I was somebody else...

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [PHP] Organizing a JSON Array
« Reply #26 on: June 23, 2010, 04:03:14 pm »
I don't like to use heredoc because it screws with my tabbing.

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: [PHP] Organizing a JSON Array
« Reply #27 on: June 23, 2010, 04:05:17 pm »
I don't want to necropost.. But what is wrong with heredoc?

They don't do anything better than other methods, and I'd be willing to bet 90%+ of php programmers don't know about that.
With what Sidoh said regarding efficiency in this case I completely agree. If that is what you are pointing out, then I agree with you as well.

If you are saying heredoc is no better than single quoted or double quoted strings in any way, then I have to disagree. Heredoc does not require escaping for double quotes, or concatenation to use variables. Heredoc also looks better. A con is the tabbing, but tell me which is more readable:
Code: (Single Quoted) [Select]
       printf('<form action="' . $_SERVER['PHP_SELF'] . '" method="POST">');
        printf('<textarea name="array" cols="100" rows="25"></textarea><br />');
        printf('<input type="submit" name="submit" />');
        printf('</form>');
Code: (Double Quoted) [Select]
       printf("<form action=\"{$_SERVER['PHP_SELF']}\" method=\"POST\">");
        printf("<textarea name=\"array\" cols=\"100\" rows=\"25\"></textarea><br />");
        printf("<input type=\"submit\" name=\"submit\" />");
        printf("</form>");
Code: (Heredoc) [Select]
$form = <<<GGWP:
<form action="{$_SERVER['PHP_SELF']}" method="POST">
<textarea name="array" cols="100" rows="25"></textarea><br />
<input type="submit" name="submit" />
</form>
GGWP;
printf($form);
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Organizing a JSON Array
« Reply #28 on: June 23, 2010, 06:58:35 pm »
I still like ending PHP tags more.  Easier to structure things via spacing.

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: [PHP] Organizing a JSON Array
« Reply #29 on: June 23, 2010, 10:09:25 pm »
I still like ending PHP tags more.  Easier to structure things via spacing.
That works too! :D Very flexible, but it comes at the cost of having to open/close tags and echo/print for every variable.
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!