PHP echo() outputs text immediatelywithout 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 displaycontent on the screen, be it text, HTML tags, or the value ofPHP 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
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 alist of expressions as parameter:
echo(strings ...$expressions)phpSince PHP echo is a language construct and not a real function, thebrackets after the keyword are often omitted. The strings can be enclosed with either single or double quotes.
echo 'Hello World'phpThe PHP echo variable also displays stored text:
$var = "red"echo $var // Output "red"phpThere’s also ashorthand where the expression follows an equal sign and open PHP tag:
<?=$var?>phpYou can find more information about PHP programming in ourPHP tutorial. Check out our comparisons ofPHP vs. Python andPHP vs. JavaScript.
- DNS management
- Easy SSL admin
- API documentation
Examples for using the PHP echo() function
The echo() function is highlyresource-efficient with minimal overhead, offering versatile application possibilities.
Concatenate strings and variables
PHP operators like theconcatenation operator. allow for combining text and strings stored in variables.
$str1 = 'nice'$str2 = 'weather'echo 'What . ' $str1 . ' ' . $str2 . 'today!'phpYou’ll get the following output:
What nice weather today!phpOutput the value of arrays
You can use PHP echo to display array values:
$colors=array("color=>"blue");echo "The sky is " . $colors['color'] ;phpThe output is “The sky is blue”, with “blue” retrieved from thePHP array “$colors” using the “color” key.
Using echo() in a PHP class
CustomPHP classes supportdynamic 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();phpIn thesayHello() 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 canuse PHP to retrieve information from a MySQL database and display theresults 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);phpIn 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() withPHP functions like mysqli_error() and display the error description.
Cost-effective, scalable storage that integrates into your application scenarios. Protect your data with highly secure servers and individual access control.





