Reply
Multiple echoes/prints vs 1?
Old 05-09-2007, 07:32 PM Multiple echoes/prints vs 1?
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
I'm curious and hoping you all will provide some feedback. I notice a lot of code uses more than 1 echo statement where it's not necessary. For example,

PHP Code:
echo 'line 1 ';
echo 
'line 2 ';
echo 
'line 3 '
where
PHP Code:
echo 'line 1 line 2 line 3'
would work, or if you want newlines, some will do this:

PHP Code:
echo "line 1 \n";
echo 
"line 2 \n";
echo 
"line 3 \n"
where
PHP Code:
 echo "line 1
line 2
line 3"

Is there any advantage/disadvantage between the two methods other than perceived readability?

I'm just wondering if I'm missing something here.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
When You Register, These Ads Go Away!
Old 05-09-2007, 08:18 PM Re: Multiple echoes/prints vs 1?
tripy's Avatar
Fetchez la vache!

Posts: 2,054
Name: Thierry
Location: In the void
I'd say maybe just 1 thing...
When you put a string between single quote, you cannot use variables in it.
PHP Code:
echo 'this is the $count time you come here'
Will output it as it it spelled. You must do
PHP Code:
echo 'this is the '.$count.' time you come here'
to have variables translated to their values.

Otherwise, there is still 1 more writing that I find much more readable for outputing multiple lines.
PHP Code:
$lang="PHP";
echo <<<HTML
this is a 
            multiline
   $lang
text string.
HTML; 
Everything between the tags you put after <<< and before <tag>; is treated as the content of your variable.
I often use this technique to output large HTML blocks.
Or to compose my SQL queries, for instance.
You just have to take care that the ending tag is on the first column of the line; no spaces or tabs before it.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is online now
Reply With Quote
View Public Profile
 
Old 05-09-2007, 08:53 PM Re: Multiple echoes/prints vs 1?
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
Yeah, I knew about the single quotes versus double quotes issue. I usually use single quotes and always pull out the variables so constants that I don't know about don't get parsed in the double quotes.

I used to use heredoc notation (that's with the 3 < symbols), but it caused problems b/c I found that I often wanted to do some processing in the middle for some reason (e.g. selected an option in a dropdown) and then I had to break it and restart it and the code just turned into spaghetti over time.

I'm thinking that it must just be a personal preference thing for people. There are always so many minor considerations that can have an impact on really large scales that it's a bit hard to stay on top of it all. I just recently learned, for example, that a switch statement is faster than a series of if/elseif statements - I always avoided them because my editor (Crimson Editor) can find the start and end of a block very quickly but switch statements aren't as easy to traverse through if you're looking for the 3rd option when programming.

Thanks for the feedback!
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Multiple echoes/prints vs 1?
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12500 seconds with 12 queries