Getting Started with PHP Output (echo and print)


Outputting Data in PHP

In PHP, you can display output using either echo or print. Although they work similarly, echo is generally faster, while print returns a value of 1, which means it can be used in expressions.

Using echo

echo can output strings, variables, and HTML. Here’s an example:

<?php
$name = "Anam";
echo "Hello, " . $name . "! Welcome to PHP.<br>";
echo "This is an example of using <strong>echo</strong> for output.";
?>

Output:

Hello, Anam! Welcome to PHP.

This is an example of using echo for output.

Using print

print works similarly but has a slightly different syntax. Here’s an example:

<?php
$age = 30;
print "Your age is " . $age . " years old.<br>";
print "This is an example of using <strong>print</strong> for output.";
?>

Output:

Your age is 30 years old.

This is an example of using print for output.

Both echo and print are useful for displaying content in PHP, and which one to use depends on personal preference and specific requirements.