TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   print() vs echo() (http://www.talkphp.com/absolute-beginners/4158-print-vs-echo.html)

allworknoplay 04-18-2009 11:32 PM

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....

Sakakuchi 04-19-2009 09:41 AM

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');



Salathe 04-19-2009 10:15 AM

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.

allworknoplay 04-19-2009 04:25 PM

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.....

cecilia 05-22-2009 10:24 PM

Ive been wondering about something between these two. I think I read somewhere that echo is processed faster than print. Is this true?

Salathe 05-22-2009 10:48 PM

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.

gunder 05-24-2009 03:32 AM

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.

ReSpawN 05-27-2009 12:01 PM

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

sketchMedia 05-27-2009 01:17 PM

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'


ReSpawN 05-27-2009 01:46 PM

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)


All times are GMT. The time now is 09:31 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0