PHP Echo / Print

In PHP, there are two primary ways to produce output: echo and print.

Tutorials dojo strip

These two statements are frequently used in PHP scripts to display information.




Similarities and Differences

Both echo and print are used to output data to the screen. However, there are subtle differences between them:

  • echo does not return a value, while print returns 1, allowing it to be used in expressions.
  • echo can take multiple parameters, though this is rarely used, whereas print can only take one argument.
  • echo is slightly faster than print.




The echo Statement

The echo statement can be used with or without parentheses: echo or echo().

Example:

PHP

Displaying Text:

The following example demonstrates how to output text using the echo command (notice that the text can contain HTML tags):

PHP

Displaying Variables:

The following example shows how to output text and variables with the echo statement:

PHP




Using Single Quotes

Strings can be surrounded by quotes, and there is a difference between using single and double quotes in PHP.

When using double quotes, variables can be embedded within the string directly. With single quotes, variables must be concatenated using the . operator.

Example:

PHP




The print Statement

The print statement can also be used with or without parentheses: print or print().

Example:

PHP

Displaying Text:

The following example demonstrates how to output text using the print command (notice that the text can contain HTML tags):

PHP

Displaying Variables:

The following example shows how to output text and variables with the print statement:

PHP




Using Single Quotes

When using double quotes, variables can be included directly within the string. However, with single quotes, variables must be concatenated using the . operator.

Example:

PHP
Tutorials dojo strip