PHP Echo

When we want to display data on a web page, we need to use function echo ().

Let's say we need to display Hello W3docs ! on a web page, for that we need to put string in quotes after operator echo .

<?php

   echo  'Hello W3docs !';

   //Output a string  Hello W3docs


?>

The echo () function is used to output the given argument. It can output all types of data and multiple outputs can be made with only one echo () command.

Actually echo is not a function (it is a language construct), so you are not have to use brackets with it.

If you want to pass more than one parameter to echo, the parameters must not be enclosed within brackets.

For example.

<?php

   echo  'Hello W3docs !';

   //Output a string  Hello W3docs


   echo  ('Hello W3docs !');

   //Output a string  Hello W3docs


   echo  $variable;

   //Output a value of $variable


   echo 'string ' . $variable. ' other string' ;
 
   //Output a string, then a variable, then a string


?>

For output of data we can use single and double quotes.

If we use single quotes, data date between the quotes always will be displayed as string !

<?php

   $variable = 'Hello W3docs' ;

   echo  '$variable';

   //Output a string  $variable


   echo  $variable;

   //Output a string  Hello W3docs


   // For the number we can not use quotes


   echo '3' . "3" . 3;
   
  //Output 3  3  3

   

?>

Also we can use function echo in formatting web pages.

<?php

   echo "<strong>This is bold text</strong>";

   echo "<i>This is italic  text</i>";

?>

// in output we get

This is bold text


This is italic text

There is another function for text output print() .

Тhe difference between print() and echo().

  • print() has returned value (1)
  • we cant use multiple parameters for print()
  • print() is processed slower than echo()

<?php

   print  'Hello';

   //Output a string  hello


   print  $variable;

   //Output a value of $variable


?>