Jump to content

Print VS Echo functions


JFab

Recommended Posts

Hello Killersites,

 

This is my first [relative] thread that might even help some of you. You may have been wondering, "What is the difference between Echo and Print?". I'm here to answer you that!

 

You may notice that these two scripts do identically the same thing:

 

<?php
print "Hello World!";
?>

 

<?php
echo "Hello World!";
?>

 

The only difference between these two scripts is that I use "print" rather than "echo", vice versa. I find that most old-fashioned programmers use 'print' rather than 'echo', due to the use in Java and C++, however in C++ it is actually called 'printf'. They both do the same exact thing, so why are there two!?

 

Read more (learnphponline article)

Link to comment
Share on other sites

I do remember reading somewhere that echo was slightly faster, but I don't think I ever saw statistics that backed it up. Thanks for posting. :)

 

No problem :)

 

echo is slightly faster, but not a worry if your in a habit of using print. I understand a lot of programmers coming from C still accidently use 'printf' instead of 'print' thinking it's the same function, hehe.

Link to comment
Share on other sites

I looked to see if one was deprecated, but it seems that both are still valid.

 

If you look at the php.net/manual pages,

 

echo

 

(PHP 4, PHP 5)

 

echo — Output one or more strings

Description

void echo ( string $arg1 [, string $... ] )

 

 

print

 

(PHP 4, PHP 5)

 

print — Output a string

Description

int print ( string $arg )

 

there are small differences. Echo can be used for more than one string and the code is different.

 

I use echo because that's what Stefan's video tutorial uses! Also I think there might be some confusion using print where people might think that it's supposed to start a computer's printer!

 

Could someone please explain the difference between

void echo ( string $arg1 [, string $... ] )

and

int print ( string $arg )

Link to comment
Share on other sites

Well PHP was made to make the move from one language to PHP as smooth as possible, that's why there are a lot of functions doing the same thing with different names.. and depending on from what language you emigrated from you find people using the different styles.

 

And that's what I like about PHP so many different styles you can use, and you pretty much find fimiliar ways/functions in it from java, c, perl etc.

 

Anyway about the print and echo difference:

 

The print function .. or well it's not a function but a language contruct, returns a int value whereas echo returns no value that is it's a void function. Thus the difference in speed.

 

But still it's the way you code that determine the speed of how the code works.

 

So echo "String 1"."String2";

 

is a lot slower than just doing echo on one string as the two strings need to be merged into one.

 

to not screw the time benefit of echo, you should use it's ability to handle multiple strings at once

 

echo "String1", "String2", "String3";

 

is the correct way to go, and in a impressive loop this method would save us a a few seconds in the long run.

 

 

So to sum this up, small writing to the screen, let say one liners there is no noticable difference between print and echo.

 

It's when you start outputning multiple strings at once that you start noticing a difference.

Edited by krillz
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...