your image

PHP echo and print Statements - javatpoint

javapoint
Related Topic
:- PHP

PHP echo and print Statements

We frequently use the echo statement to display the output. There are two basic ways to get the output in PHP:

  • echo
  • print

echo and print are language constructs, and they never behave like a function. Therefore, there is no requirement for parentheses. However, both the statements can be used with or without parentheses. We can use these statements to output variables or strings.

Difference between echo and print

echo

  • echo is a statement, which is used to display the output.
  • echo can be used with or without parentheses.
  • echo does not return any value.
  • We can pass multiple strings separated by comma (,) in echo.
  • echo is faster than print statement.

print

  • print is also a statement, used as an alternative to echo at many times to display the output.
  • print can be used with or without parentheses.
  • print always returns an integer value, which is 1.
  • Using print, we cannot pass multiple arguments.
  • print is slower than echo statement.

You can see the difference between echo and print statements with the help of the following programs.

For Example (Check multiple arguments)

You can pass multiple arguments separated by a comma (,) in echo. It will not generate any syntax error.

 

141.6K

3. PHP Inheritance | Build a CMS using OOP PHP tutorial MVC [2020]

  1. <?php  
  2.      $fname = "Gunjan";  
  3.      $lname = "Garg";  
  4.      echo "My name is: ".$fname,$lname;  
  5. ?>  

Output:

It will generate a syntax error because of multiple arguments in a print statement.

  1. <?php  
  2.      $fname = "Gunjan";  
  3.      $lname = "Garg";  
  4.      print "My name is: ".$fname,$lname;  
  5. ?>  

Output:

For Example (Check Return Value)

echo statement does not return any value. It will generate an error if you try to display its return value.

  1. <?php  
  2.      $lang = "PHP";  
  3.      $ret = echo $lang." is a web development language.";  
  4.      echo "</br>";  
  5.      echo "Value return by print statement: ".$ret;   
  6. ?>  

Output:

As we already discussed that print returns a value, which is always 1.

  1. <?php  
  2.      $lang = "PHP";  
  3.      $ret = print $lang." is a web development language.";  
  4.      print "</br>";  
  5. print "Value return by print statement: ".$ret;   
  6. ?>  

Output:

 

Comments