Jump to content

Good/bad Practice : Html In Php Statement.


newrehmi

Recommended Posts

hello there w3teachers and friends. I have some question. I believe that I actually have wrote like a several tens of thousand line of script. But, just a few weeks ago, I met some script that changed a bit of my belief. Well, simply let me make turn the question into some example:

case 1: <?phpif($foo == "foo"){echo "<span>some statement is running</span>";}?> case 2:<?phpif($foo != "foo"){?><span>theres no running statement</span><?php}?>

okay, the example script that has changed my belief is the second case. So, my question is:1. which example do you thing is good for practice 2. I can see that sometimes, the second case is easier, but sometimes may burden since I need to write <?php echo ;?> to instill some php in it. Thanks a lot and give a comment!!

Link to comment
Share on other sites

I'm having a hard time following that example. The syntax is pretty wonky given that you have opening php tags within already opened php tags. What exactly are you trying to compare?

Link to comment
Share on other sites

I'm having a hard time following that example. The syntax is pretty wonky given that you have opening php tags within already opened php tags. What exactly are you trying to compare?
sorry, i've corrected some part.. hope you can see it clearly. Obviously, the statement of the second case is outside of php script.
Link to comment
Share on other sites

I am guessing the cases outside the PHP are javascript related?I think the first example is best because of the code readability. If you are that worried about squeezing every drop of performance out of the script, the open/close tags may eliminate them however do not quote me on that. For absolute peak performance on that, you should use single quotes rather than double. This is faster because less escape characters are available and it does not substitute variables. That is just my opinion.However I will also say this. With if statements containing only one line of code, you may re-write it as follows:if (condition) echo "Condition met"; As soon as there is more than one piece of code you must: if (condition) { echo "Condition met"; $condMet = true;}

Link to comment
Share on other sites

For absolute peak performance on that' date=' you should use single quotes rather than double[/quote']Note: The difference on that is EXTREMELY tiny. Less than a millisecond per million literal strings. => It's not something to worry about. As for the single line condition - although it might be slightly better for the parser ("better" having weight similar to that with single vs. double quotes => negligible), there's a large trade off in readability, i.e. you may start reading the code thinking you have one line, until you look into it with "fresh" eyes to find out you had recently added a second line that creates problems. Personally, I use it rarely, but when I do, I always make it on the same line as the "if" in order to keep it readable, like:
if (condition) echo "Condition met";

Some might find this slightly less readable than having it on two lines, but in THIS way you avoid any potential problems if introducing multiple lines. As for the initial question... I personally use the second approach, not for performance reasons (which I'm willing to bet will be negligible), but because IDEs can do syntax highlighting on HTML with the second approach. Most don't do that with plain strings or heredocs. If the string I'm outputting isn't HTML, I typically use echo (big strings typically end up in a file).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...