PHP Echo
In PHP, echo
is a language construct used to output data to the browser. It can be used to display text, variables, or even HTML content. Here are some examples to illustrate how echo
works:
Basic Usage
<?php
echo "Hello, World!";
?>
Output:
Hello, World!
Outputting Variables
<?php
$name = "John";
echo "Hello, " . $name . "!";
?>
Output:
Hello, John!
Concatenation with Strings
<?php
$firstName = "Jane";
$lastName = "Doe";
echo "Full Name: " . $firstName . " " . $lastName;
?>
Output:
Full Name: Jane Doe
Outputting HTML Content
<?php
echo "<h1>Welcome to My Website</h1>";
echo "<p>This is a paragraph.</p>";
?>
Output:
Welcome to My Website
This is a paragraph.
Multiple Parameters
echo
can take multiple parameters separated by commas, which will be output in sequence:
<?php
echo "The date is ", date('Y-m-d'), " and the time is ", date('H:i:s');
?>
Output:
The date is 2024-07-25 and the time is 14:30:00
Using Short Tags
In some PHP configurations, you can use short tags to echo data:
<?php echo "Hello, Short Tags!"; ?>
Output:
Hello, Short Tags!
Performance Consideration
echo
is slightly faster than print
because it does not return a value, whereas print
returns 1. However, the difference in performance is usually negligible for most applications.
Feel free to ask if you have any specific scenarios or additional questions about echo
!
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.