PHP echo and print Statements



PHP echo and print Statements


In PHP, the echo and print statements are used to output text and variables to the browser. Here's a brief overview of how they work:echo statement

The echo statement is used to output text or variables to the browser. It can be used with or without parentheses.

In PHP, the echo and print statements are used to output text and variables to the browser. Here's a brief overview of how they work:echo statement

The echo statement is used to output text or variables to the browser. It can be used with or without parentheses.

<?php
echo "Hello, world!";
echo "<br>";
$name = "John";
echo "My name is $name";
?>

In the example above, the echo statement is used to output the text "Hello, world!" and a line break, followed by the value of the $name variable.print statement

The print statement is similar to echo, but it can only be used to output a single value at a time. It always returns a value of 1.

<?php
print "Hello, world!";
print "<br>";
$name = "John";
print "My name is $name";
?>


In the example above, the print statement is used to output the text "Hello, world!" and a line break, followed by the value of the $name variable.

Both echo and print statements are commonly used in PHP to output text and variables to the browser. They are both simple and easy to use, and can be combined with HTML to create dynamic web pages. However, echo is generally faster than print, so it is often the preferred choice for outputting text in PHP.




Post a Comment

Previous Post Next Post