PHP echo() outputs text im­me­di­ately without gen­er­at­ing a return value. This is more efficient than functions like print(). You can use echo() in various contexts to display strings in web pages, files, or other formats

What is PHP echo?

PHP echo() is a language construct used to output strings on a web page or in a PHP ap­plic­a­tion. With echo() you can display content on the screen, be it text, HTML tags, or the value of PHP variables. One advantage of echo() is that the code is often shorter and more readable than con­stantly opening and closing PHP tags to switch between PHP and HTML

Tip

Deploy Now by IONOS ensures that your projects are hosted on a reliable and powerful in­fra­struc­ture. Changes to your GitHub re­pos­it­ory are auto­mat­ic­ally committed after the push command. Using modern servers and scalable resources, you can be confident that your web ap­plic­a­tions will run smoothly and are stable.

The syntax of PHP echo()

PHP echo accepts a list of ex­pres­sions as parameter:

echo(strings ...$expressions)
php

Since PHP echo is a language construct and not a real function, the brackets after the keyword are often omitted. The strings can be enclosed with either single or double quotes.

echo 'Hello World'
php

The PHP echo variable also displays stored text:

$var = "red"
echo $var // Output "red"
php

There’s also a shorthand where the ex­pres­sion follows an equal sign and open PHP tag:

<?=$var?>
php

You can find more in­form­a­tion about PHP pro­gram­ming in our PHP tutorial. Check out our com­par­is­ons of PHP vs. Python and PHP vs. JavaS­cript.

IONOS Developer API
Manage your hosting products through our powerful API
  • DNS man­age­ment
  • Easy SSL admin
  • API doc­u­ment­a­tion

Examples for using the PHP echo() function

The echo() function is highly resource-efficient with minimal overhead, offering versatile ap­plic­a­tion pos­sib­il­it­ies.

Con­cat­en­ate strings and variables

PHP operators like the con­cat­en­a­tion operator . allow for combining text and strings stored in variables.

$str1 = 'nice'
$str2 = 'weather'
echo  'What . ' $str1 . ' ' . $str2 .  'today!'
php

You’ll get the following output:

What nice weather today!
php

Output the value of arrays

You can use PHP echo to display array values:

$colors=array("color=>"blue");
echo "The sky is " . $colors['color'] ;
php

The output is ‘The sky is blue’, with ‘blue’ retrieved from the PHP array ‘$colors’ using the ‘color’ key.

Using echo() in a PHP class

Custom PHP classes support dynamic output of strings with echo().

class Person {
    private $name;
    public function __construct($name) {
        $this->name = $name;
    }
    public function sayHello() {
        echo "Hello, I am {$this->name}!";
    }
}
$person = new Person("Alice");
$person->sayHello();
php

In the sayHello() method of the Person class, the echo() function is employed to create a greeting using the name value provided in the object.

Database query with PHP echo()

Fur­ther­more, you can use PHP to retrieve in­form­a­tion from a MySQL database and display the results from the query on the web page with echo().

$sql = "SELECT name, email FROM user WHERE id = 1";
$result = mysqli_query($conn, $sql);
if ($result) {
    $row = mysqli_fetch_assoc($result);
    echo "Name: " . $row['name'] . "<br>";
    echo "E-Mail: " . $row['email'];
} else {
    echo "Error occurred: " . mysqli_error($conn);
}
mysqli_close($conn);
php

In this example we use PHP echo
to separate the output of names and emails with a new line. In case of an error we can link echo() with PHP functions like mysqli_error() and display the error de­scrip­tion.

IONOS Cloud Object Storage
Cloud storage at an un­beat­able price

Cost-effective, scalable storage that in­teg­rates into your ap­plic­a­tion scenarios. Protect your data with highly secure servers and in­di­vidu­al access control.

Go to Main Menu