• Register

Php has different statements or constructs to output the data on the screen. Echo is mostly used to display data, variable and string in more convenient way. While print_r() is a function suitable to return 1 value and it is mostly used with expressions.

Php also has print statement to print output on screen. Print has a return value of 1 so it is the ideal choice for expressions.

What is Echo or Echo () in php?

In php we use echo to show output on screen. It works with strings, variables and almost all data types. Echo is highly appreciated way of displaying output. Echo statement has flexibility of syntax means you can use them with or without parenthesis.

Syntax and examples:

//how strings display output
echo "<h2>PHP is very easy to learn</h2>";
echo "php is servide side or backend language";

What is Print_r() function used for?

Print_r() is a built-in function that works with expressions and returns 1 value. Print_r function prints a variable output in a very efficient and human-readable form. This function returns the value for integer, float, or other data types but imputes array it will print array keys along with elements.

syntax: how can we write the Print_r function?

Print_r(variable, return);

Here variable parameter is the variable you want to print and it is required you must provide the parameter variable, whereas the return parameter is optional even if you ignore it will not affect your program performance.

Example: for Echo, print, and print_r function

   $Car = array( "toyata", "Suzuki", "GMC", "Honda");
   echo "Array...\n";
//we will print array using echo 
   foreach( $Car as $value ) {
      echo "Value = $value \n";
   echo "\n output of Array Values by using print...\n";
   foreach( $Car as $value ) {
      print( "Value = $value \n");
   echo "\noutput of Array Values by using print_r fucntion ..\n";
//this will print keys and values 

Echo and print are statements for displaying the output in PHP, Echo has a void return type whereas print returns 1 value and print_r is used to display a veriable’s info i a format easily understandable by human.



4,670 points
9 5 1