echo and print statements

PHP 5 echo and print statements

Note:

array: array

PHP echo and print statements

Differences between echo and print:

·                                                                                                                                                                                                                              Echo - can output one or more strings

·           print - only allows to output one string, the return value is always 1

Tips: The output speed of echo is faster than that of print. Echo has no return value, while print has a return value of 1.


PHP echo statement

echo is a language structure, which can be used without parentheses or with parentheses: echo or echo().

Display string

The following example demonstrates how to use the echo command to output a string (the string can contain HTML tags):

Example (1)

<?php
 echo "<h2>PHP is fun!</h2>";
 echo "Hello world!<br>";
 echo "I'm about to learn PHP!<br>";
 echo "This", " string", " was", " made", " with multiple parameters.";
 ?>

Display variables

The following example demonstrates how to use the echo command to output variables and strings:

Example (2)

<?php
 $txt1="Learn PHP";
 $txt2="cccccccc ";
 $cars=array("Volvo","BMW","Toyota");
echo $txt1;
 echo "<br>";
 echo "Study PHP at $txt2";
 echo "My car is a {$cars[0]}";
 ?>

PHP The print statement

print is also a language structure that can use parentheses or not: print or print().

Display string

The following example demonstrates how to use the print command to output a string (the string can contain HTML tags):

Example (3)

<?php
 print "<h2>PHP is fun!</h2>";
 print "Hello world!<br>";
 print "I'm about to learn PHP!";
 ?>

-------------------------------------------------- -------------------------------------------------- ----------------------------------

Display variables

The following example demonstrates how to use the print command to output variables and strings:

Example (4)

<?php
 $txt1="Learn PHP";
 $txt2="ccccccc";
 $cars=array("Volvo","BMW","Toyota");
 
 print $txt1;
 print "<br>";
 print "Study PHP at $txt2";
 print "My car is a {$cars[0]}";
 ?>


Continuing Learning
||
<?php echo "<h2>PHP is fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This", " string", " was", " made", " with multiple parameters."; ?>
submitReset Code