PHP – Output

The main php output functions are listed below

  • echo
  • print
  • printf
  • print_r
  • var_dump

Generally all output functions are used to display data but there is some difference between each and every function. Based on this difference the developer can decide which one is best fit for their situation.

Echo

  • Echo is simple output function used to display some variables, variable operations and text documents in php.
  • Compared with other functions echo is the fastest output function in php language.
  • The function echo is represented with or without parentheses(   echo  or   echo() ).

Example shows how many ways used to display the outputs in echo.

Example

Output

In the above example more than one outputs display in a single line without any space. There are some functions used to add space, new line or list.

  1. &nbsp  for space.
  2. <br />  for new line
  3. <pre>  for list array values.

Example 1

Output

 

Example 2

Output

 

Example 3

Output

 

Print

The second function in the above list is print so next discuss about print function.

  • Basically both print  and echo  are same, the only difference is print  produce a return value 1 and echo  has no return value.
  •  The function print is represented with or without parentheses (   print  or print()  ).

Example

Output

 

Printf

  • printf  is an output function same as that of   print  function.
  • The string produced from printf  is a formatted string and the string produced from the print  function is not a formatted string. This is the main difference between print  and printf  function.
  • Length of output is the return value of printf.

Note: The return values are not displayed with output.

Example

Output

 

Print_r

The function   print_  is used to read values inside an array. echo , print , printf are not capable of reading these types of values.

  • Generally says the print_r  displays information about a variable that can be readable by humans. So we can say that print_r  function is a human readable function.
  • The print_r  read the values such as integer, float and string from an array with its index.

 

Output

 

Var_dump

The function var_dump is used to read values inside an array and the function also read full information of the input variable. The  print_r  function do not read the type of variable, number of characters present in the variable but the var_dump  read these information too.

  • var_dump  is capable of read all public, private and protected values.

 

Example

Output

 

Note

There some special characters that are affect the output displaying process that are single quotes and double quotes

  • The values inside single quotes are just displayed without any evaluation. But in case of double quotes the values are first evaluate and after completing the evaluation process display the value.
  • The single quoted are represented as ' ' and double quoted are represented as " " .

Example for single quotes

Output

Example for double quotes

Output

 

Leave a Reply