echo and print Statements in PHP:

In PHP how you get output on browser so what you will do that. In PHP you have two ways to get outputs.

  • echo
  • print

The echo statement in PHP:

In PHP echo is not a function but it is a language construct means you don’t have to need add parentheses to echo statement but you can use it like “echo ()” or “echo”. The echo statement can give output of one or more strings but it does not return any value.

Example:

In this example we are displaying different strings using echo statements

Example:

In this example we are adding variables with string and displaying it using echo statement

The print statement in PHP:

The print statement is also not a function but it is a language construct and it can be used with parentheses or without parentheses like “print” or “print ()”. The limitation is that the print statement can give output of only one string and it always return 1.

Example:

In following example we are displaying only string using print statement

Example:

In following example we are displaying variables and string using print statement

Difference between echo and print:

  • In PHP echo and print both statements are use to get output on browser but the main difference is that echo can print multiple expression where as print can not do that.
  • Another main difference is that print can return values when execution is successfully done which is can not do by echo
  • The third difference between echo and print is echo is much faster the print. The reason is that echo does not return any value.

Leave a Reply

Your email address will not be published. Required fields are marked *