Reply
Print VS Echo
Old 03-27-2005, 03:52 PM Print VS Echo
Skilled Talker

Posts: 51
I've always considered print() and echo() interchangeable and never seen any clues that either did something different than the other.

Is there a difference between print() and echo()? Is one intended for certain cases the other isn't? Or.... ?

Just curious. Thanks.


PS While I'm asking dumb questions, I've read that include() is supposed to be used in conditional situations, whereas require() should be used in non-conditional ones. I pretty much always used require() regardless of the context. Is this a problem, or does it even really matter?

Thanks again.
gregory is offline
Reply With Quote
View Public Profile
 
Sponsored Links (We share ad revenue):
 
Old 03-27-2005, 04:17 PM
Phaedrus's Avatar
Ultra Talker

Posts: 271
Location: CA
From what I understand, echo() is slightly faster than print(). However, the difference is probably insignificant.

One thing though, if you want to echo() a large block of code, like a table or something, I've heard that instead of doing this:
PHP Code:
echo '<table>';
echo 
'<tr><td>';
echo 
'this is inside the table cell';
echo 
'</td></tr>';
echo 
'</table>'
it's faster to put everything into a string variable, i.e. $table, and then echo $table once at the end:
PHP Code:
$table '<table>';
$table.= '<tr><td>';
$table.= 'this is inside the table cell';
$table.= '</td></tr>';
$table.= '</table>';

echo 
$table
I've heard that that's faster than calling echo() multiple times.
__________________
Free Teacher Websites

Last edited by Phaedrus : 03-27-2005 at 04:21 PM.
Phaedrus is offline
Reply With Quote
View Public Profile
 
Old 03-27-2005, 05:48 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Phaedrus, I'm not sure why it would be "faster" to do what you describe. The system still must handle the string concatenation that you describe.

However, I would stress that even if one echo command does save some time over numerous ones, the advantage would be insignificant.
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 03-27-2005, 06:27 PM
Experienced Talker

Posts: 36
All of your questions can be answered correctly at their corrosponding pages in the PHP manual (www.php.net/function_name).
tress is offline
Reply With Quote
View Public Profile
 
Old 03-27-2005, 06:33 PM
Christopher's Avatar
Iced Cap

Posts: 3,105
Location: Toronto, Ontario
echo will be a little bit faster because it will not return a value. The difference in speed, however, would be insignificant as Phaedrus stated.

print is good when you need to use it as a function, for example:

PHP Code:
while(print("Here we go again"))
{
    
// ...
}

// or
$something $myvar ? print("Success!") : false
As to your example Phaedrus, I would think it would be faster to echo the entire string out at once because you don't have to write the string to the variable memory first, then access it again upon print. The few lines of the table in your example probably wouldn't save or spend any time either way. But if you are working with hundreds of lines, you could probably shed a few hundredths of microseconds off
__________________
Devlog - Latest PHP Article: MVC with the Zend Framework
::The New Tech - Technology Forum
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Old 05-07-2008, 03:32 AM Re: Print VS Echo
Junior Talker

Posts: 1
Name: Gordon Jewett
This seems to answer this topic pretty well...

http://www.faqts.com/knowledge_base/...l/aid/1/fid/40
gjewett is offline
Reply With Quote
View Public Profile
 
Old 05-07-2008, 09:34 AM Re: Print VS Echo
dansgalaxy's Avatar
Eat, Sleep, Code

Posts: 5,381
Name: Dan
Location: Swindon
Just to say about require and include.

require() (or require_once()) means that if it cannot include it for what ever reason the whole script will stop and return error.

Include (or include_once()) means it will try and continue running even if PHP cannot load the file.

As far as i know neither nor is specifically used in loops or switches although often they are.

Usually i would say require would be good for things like config files which have to be loaded where as if the file to be used is something like a template file maybe include would be better.

Also incase you were wondering require/include_once() means IF it hasnt already been loaded before load it if it has dont, usually helpful for things like functions files so it doesnt cause reclassification errors where it tries to make a function twice.

Hope this helps

TP appriciarted if it did
__________________
Support Children's Cancer and Leukaemia Movement shop online at buy.at/calm ~ Calmcharity.org
PHP Code:
if($_POST == true && $help == true) { $tp++; } else { $e_hug == true 
dansgalaxy is online now
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 05-07-2008, 10:41 AM Re: Print VS Echo
addonchat's Avatar
Skilled Talker

Posts: 97
Name: Chris Duerr
Quote:
Originally Posted by gregory View Post
PS While I'm asking dumb questions, I've read that include() is supposed to be used in conditional situations, whereas require() should be used in non-conditional ones. I pretty much always used require() regardless of the context. Is this a problem, or does it even really matter?
It only matters if you intend to require a file that might be missing -- which shouldn't ever be the case -- in which case your program will immediately halt execution; whereas with include you have the ability to determine if there was an issue and decide what to do from there.

For what it's worth, I see way too many PHP apps using include(), and providing no error notification, when they should be using require(). Similarly, the value of the require_once() and include_once() functions shouldn't be under-estimated.
__________________
Chris Duerr
AddonChat Java Chat Software
http://www.addonchat.com/ - Affiliate Program
addonchat is offline
Reply With Quote
View Public Profile
 
Old 05-07-2008, 10:51 PM Re: Print VS Echo
mgraphic's Avatar
Truth Seeker

Posts: 2,191
Name: Keith Marshall
Location: West Hartford, CT
require() will throw a fatel error on failure (and the current php parser will die) and include() will only issue a warning on failure.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Sponsored Links (We share ad revenue):
 
Reply     « Reply to Print VS Echo
 

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.53735 seconds with 14 queries