How to use PHP echo() to output text and variables

PHP echo() outputs text immediately without generating 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 application. 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 constantly 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 infrastructure. Changes to your GitHub repository are automatically committed after the push command. Using modern servers and scalable resources, you can be confident that your web applications will run smoothly and are stable.

The syntax of PHP echo()

PHP echo accepts a list of expressions 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 expression follows an equal sign and open PHP tag:

<?=$var?>
php

You can find more information about PHP programming in our PHP tutorial. Check out our comparisons of PHP vs. Python and PHP vs. JavaScript.

IONOS Developer API
Manage your hosting products through our powerful API
  • DNS management
  • Easy SSL admin
  • API documentation

Examples for using the PHP echo() function

The echo() function is highly resource-efficient with minimal overhead, offering versatile application possibilities.

Concatenate strings and variables

PHP operators like the concatenation 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()

Furthermore, you can use PHP to retrieve information 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 description.

IONOS Object Storage
Secure, affordable storage

Cost-effective, scalable storage that integrates into your application scenarios. Protect your data with highly secure servers and individual access control.

Was this article helpful?
Page top