How to use echo to print data in PHP

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.

Syntax

The general syntax of echo is:

<?php echo (value);?>

Examples

Let’s take a look at some examples to see how echo works.

1. Printing a string

The following code prints a string on the browser:

<?php echo 'Hello World';?>

2. Printing an integer variable

Note: In order to separate two or more parameters, a comma , or a full stop . may be used.

Where $x = 5
<?php
$x = 5;
echo 'Value of the variable is: ' , $x;
echo ("\n");
echo 'Value of the variable is: ' . $x;?>

3. Printing value from an array

The following code will demonstrate how we can print an element from an array:

Where $day[0] = 'Monday'
<?php
$day = array("Monday", "Tuesday", "Wednesday");
echo $day[0] ; ?>
Copyright ©2024 Educative, Inc. All rights reserved