15 March, 2009

Differences between echo and print

There are 2 commands called echo and print. Both do practically the same thing. There are some differences. Print behaves like function and echo is a bit faster.

Display string:

1 <php
2 //Echo displays this line faster.
3 echo 'This will be displayed.';
4 //Print is a bit slower.
5 print 'This will be displayed.';
6 ?>



Difference between ''(single quotes) and ""(double quotes):

1 <?php
2 $string = 'dog';
3 //This prints: This is a $string.
4 echo 'This is a $string.';
5 //This prints: This is a dog.
6 echo "This is a $string.";
7 //This prints: This is a dog.
8 echo 'This is a ' . $string . '.';
9 ?>
There is no point to use double quotes if you dont have any string in it, it just makes the code a bit slower.


Display html code with echo or print:

1 <?php
2 //The right way
3 echo '<img src="picture.png" border="0" />';
4 //The wrong way
5 echo "<img src='picture.png' border='0' />";
6 ?>



Parameters:

1 <?php
2 //Echo can take multiple parameters
3 echo 'parameter1', 'parameter2', 'parameter...';
4 //Print can't take multipleparameters
5 print 'No multiple parameters possible, because I behave like a function';
6 ?>


Some day here will be some examples, about prints function like behaviour being a useful thing.

No comments:

Post a Comment