echo is the tool you will use the most. It sends text to the output, which is the browser or the terminal.
<?php
echo "Hello, world!";
Hello, world!
Print more than text
echo can print strings, numbers and variables.
<?php
$name = "Maya";
$age = 28;
echo "Name: " . $name . "\n";
echo "Age: " . $age;
Name: Maya
Age: 28
The dot (.) joins strings together. The \n is a new line character. It makes a line break in the terminal. In a web page, a browser ignores a plain new line, so you use <br> or HTML tags instead, which the next example shows.
Print HTML
echo can print HTML. The browser reads it like any other HTML.
<?php
echo "<h2>Latest posts</h2>";
echo "<p>Nothing here yet.</p>";
<h2>Latest posts</h2><p>Nothing here yet.</p>
Several values in one echo
You can give echo several values separated by commas. No joining is needed.
<?php
$city = "Pune";
echo "I live in ", $city, ".";
I live in Pune.
echo is not a function
echo is a language construct, so you write it without parentheses. echo("Hi") also works, but the brackets do nothing. They just wrap one value.
The short echo tag
In templates, <?= $value ?> is a short form of <?php echo $value; ?>. You saw it in PHP Tags.
Use echo for output
echo or print. The next lesson compares them. In day-to-day PHP, nearly everyone writes echo.Exercise: a price tag
Store the number 499 in a variable called $price, then use one echo to print Price: 499 INR.
Show a possible solution
<?php
$price = 499;
echo "Price: " . $price . " INR";
Price: 499 INR