News:

Help! We're trapped in the computer, and the computer is trapped in 2008! Someone call the time police!

Main Menu

[PHP] Organizing a JSON Array

Started by Joe, April 12, 2010, 02:46:52 AM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Sidoh

Quote from: rabbit on April 13, 2010, 07:43:52 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.

Quote from: rabbit on April 13, 2010, 07:43:52 PM
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

print "1234";

Instead of

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.

rabbit

Well then fine, eat this: with single quotes in printing you won't have to escape all the double quotes for HTML entities.

Sidoh

Quote from: rabbit 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.

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

rabbit

Quote from: Sidoh on April 13, 2010, 09:13:39 PM
Quote from: rabbit 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.

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.

Sidoh

I'm talking about stuff like this:

Quote from: rabbit on April 12, 2010, 07:17:32 AM
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

Joe

Quote from: rabbit on April 13, 2010, 02:17:33 AM
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.
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


iago

Quote from: rabbit 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.
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/">';

Chavo

#22
He's referring to the php rendering.  Single quotes are treated as string literals, double quotes are parsed for variables.  Citation

iago

Quote from: Chavo 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
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".

Lance

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!

Blaze

Quote from: Lance on June 23, 2010, 10:54:48 AM
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...

rabbit

I don't like to use heredoc because it screws with my tabbing.

Lance

Quote from: Blaze on June 23, 2010, 02:37:57 PM
Quote from: Lance on June 23, 2010, 10:54:48 AM
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!

Sidoh

I still like ending PHP tags more.  Easier to structure things via spacing.

Lance

Quote from: Sidoh on June 23, 2010, 06:58:35 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!