TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-18-2009, 11:32 PM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default print() vs echo()

Hey guys, I wanted to provide this description of print and echo.
Not my words...


Quote:
Even though print() and echo() are essentially interchangeable most of the time, there is a
substantial difference between them. While print() behaves like a function with its own
return value (although it is a language construct), echo() is actually a language construct that
has no return value and cannot, therefore, be used in an expression.

However, I can't think of any possible uses where I can't use one and not the other.

Can anyone provide an example of using print() in an expression where you can't with echo()??

TIA....
allworknoplay is offline  
Reply With Quote
Old 04-19-2009, 09:41 AM   #2 (permalink)
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

Quite simple:

PHP Code:
// thats wrong
if(echo('some string'))
{
    echo(
'It will not work');
}

// while this would work 
if(print('some string'))
{
    print(
'It will not work');
}

// of course this would work to
if(print('some string'))
{
    echo(
'It will not work');

Sakakuchi is offline  
Reply With Quote
Old 04-19-2009, 10:15 AM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Using print as an if condition isn't a particularly useful example since it always returns 1. An example where echo cannot be used, where print can, is:

PHP Code:
($something) or (print 'not something'); 
However, the times where you would use boolean comparisons like that might be rare depending on your style of coding.
Salathe is offline  
Reply With Quote
Old 04-19-2009, 04:25 PM   #4 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

thanks guys....seems like print and echo are still very very closely related to each other...

hopefully I won't run into any issues where I would require print and not echo.....
allworknoplay is offline  
Reply With Quote
Old 05-22-2009, 10:24 PM   #5 (permalink)
The Contributor
 
cecilia's Avatar
 
Join Date: May 2009
Location: LA, CA
Posts: 87
Thanks: 0
cecilia is on a distinguished road
Default

Ive been wondering about something between these two. I think I read somewhere that echo is processed faster than print. Is this true?
cecilia is offline  
Reply With Quote
Old 05-22-2009, 10:48 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

The real difference between echo and print, in terms of speed of execution, is to all intents and purposes negligible. There are much more time-consuming parts of most scripts which are far more worthy of optimisation effort.
Salathe is offline  
Reply With Quote
Old 05-24-2009, 03:32 AM   #7 (permalink)
The Wanderer
Good Samaritan 
 
gunder's Avatar
 
Join Date: May 2009
Posts: 20
Thanks: 0
gunder is on a distinguished road
Default

Cecilia I was actually looking into this the other day. The difference in speed really wouldn't be noticeable. Echo() is slightly (and I do mean slightly) faster because it doesn't return a value like print() does. Personally I'm going to use echo() in my pages because it's one less letter to type then print() .. and yeah I'm really that lazy.
gunder is offline  
Reply With Quote
Old 05-27-2009, 12:01 PM   #8 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Although I still don't see the upside of using print instead of echo, I can at least conclude that using () after echo and print is completely and utterly useless. They, when working with frameworks, components or even large script, they take up more bytes and therefore, the script becomes larger and takes longer to process.
When I see people's scripts with;
PHP Code:
echo ( 'My Echo' );
// and of course ...
print ( 'My print (duh..)' ); 
I find it a excessive use of overhead. When compared,
PHP Code:
echo 'My echo';
// and of course ...
print 'My print (duh..)'
...is a lot more easier on the eyes, very much so if you're working between if/elseif/else, for, foreach, while, switch and other functions. Use () only when you absolutely need to. :)

And yes, echo processes just slightly better than print, but it isn't notable. Create your own script and test it out on your own server (or XAMP, WAMP) using;
PHP Code:
$timer['start'] = microtime();
echo 
'a';
echo 
'a';
echo 
'a';
// Couple of hundred times...
echo 'a';
echo 
'a';
echo 
'a';
$timer['end'] = microtime(); 
And compare those diffirences.

ReSpawN
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 05-27-2009, 01:17 PM   #9 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Another thing to note is that echo accepts multiple parameters where as print does not.

Some people prefer to pass multiple string's to echo in multiple parameters rather than concatenation.
PHP Code:
echo 'String 1''String 2';

echo 
'String 1' 'String 2'
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 05-27-2009, 01:46 PM   #10 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Another good point made for echo sketchMedia. I forgot that one although I use it daily. Exactly as print can be used to print out variables with cutoffs, only echo allows for multiple parameters.

Currently I still prefer to set control the output, then parse it, and then output is using my own template engine or SMARTY (using {$name} in HTML)
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Advance Pagination Class Rendair Advanced PHP Programming 13 02-01-2013 11:08 AM
World of Warcraft Armory xml Grabber with cURL mortisimus Show Off 144 06-25-2012 12:34 PM
update on my youtube rss grabber need help. [NO FLAME] codefreek Absolute Beginners 13 07-14-2008 02:30 PM
My dynamic menu lesP Advanced PHP Programming 7 01-21-2008 10:28 PM
I present... my first original script obolus Absolute Beginners 14 12-29-2007 04:05 AM


All times are GMT. The time now is 12:09 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design