How to use PHP echo and print


PHP echo and print Statements

The PHP echo statement is commonly used to view the output. There are two simple methods of having an output in PHP.

  • echo
  • print

Echo and print are language constructs and never work as a method. Therefore parentheses have no condition. Both statements can, be used with or without parentheses. Those statements may be used to output variables or strings.

echo Statement

The echo statement can be used to output one or more strings. The echo statement will usually represent something that can be shown to the user, such as list, numbers, values for variables, results of expressions, etc.

Since echo is not actually a function in a language construct (like if statement), you can use it with or without parentheses e.g. echo() or echo. When you want to pass more than one parameter to echo, however, the parameters should not be enclosed in parentheses.

The following example illustrates how to get the echo command to output text.

PHP echo

Output :
My name is: JordanSmith

print Statement

The print statement (an alternative to echo) can also be used to view output to user. Like echo the print is not a real function, but a language construct. So you can also use it with or without parentheses like: print() or print.

Both the echo and the print statement perform exactly the same way except that the print statement can only output one string, and always returns 1. That’s why the echo statement was considered slightly faster than the print statement because it returns no value. In PHP programmers often use echo statements to display the string and the value of a particular variable but the print statement is used less than echo statement.

The following example demonstrates how the print command can be used to output text.

PHP print

Output :
Parse error: syntax error, unexpected ‘,’ in D\xampp\htdocs\test.php on line 8