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.
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.";
?>
Hello, Anam! Welcome to PHP.
This is an example of using echo for output.
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.";
?>
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.