In PHP, echo()
is used to output one or more strings in a web browser. However, echo
is not a function, rather, a language construct and can, therefore, be used with or without the parenthesis.
However, if multiple parameters are to be passed, they must not be enclosed in parenthesis as that will generate an error.
The general syntax of echo
is:
<?php echo (value);?>
Let’s take a look at some examples to see how echo
works.
The following code prints a string on the browser:
<?php echo 'Hello World';?>
Note: In order to separate two or more parameters, a comma
,
or a full stop.
may be used.
<?php$x = 5;echo 'Value of the variable is: ' , $x;echo ("\n");echo 'Value of the variable is: ' . $x;?>
The following code will demonstrate how we can print an element from an array:
<?php$day = array("Monday", "Tuesday", "Wednesday");echo $day[0] ; ?>