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
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)
phpSince 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'
phpThe PHP echo variable also displays stored text:
$var = "red"
echo $var // Output "red"
phpThere’s also a shorthand where the expression follows an equal sign and open PHP tag:
<?=$var?>
phpYou can find more information about PHP programming in our PHP tutorial. Check out our comparisons of PHP vs. Python and PHP vs. JavaScript.
- 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!'
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 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();
phpIn 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);
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() with PHP 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.