Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP echo and print statements


May 10, 2021 PHP


Table of contents


PHP echo and print statements

PHP is the dynamic output of HTML content through print and echo statements, and although the functions of print and echo statements are almost exactly the same, there is a difference.


There are two basic output methods in PHP: echo and print.

In this section, we'll discuss in detail how to use both statements and demonstrate how to use echo and print in an example.


PHP echo, print, and print_r statements

The difference between echo, print print_r and the file:

  • echo - You can output one or more strings
  • print - You can only output the values of simple type variables, such as int, string
  • print_r - You can output values for complex type variables, such as arrays, objects

Tip: Echo outputs faster than print, echo is a PHP statement, no return value, print and print_r are PHP functions, functions have a return value.

The print return value is 1 (int type), print_r return value is true (bool type).


PHP echo statement

Echo is a language structure that can be used without parentheses or with parentheses: echo or echo().

The string is displayed

The following example shows how to output a string using the echo command (strings can contain HTML tags):

<?php
echo ""lt;h2-pPT is very interesting!
echo "Hello world!<br>";
Echo I'm going to learn PHP!
echo "This is a", "string," "use," "multiple," "parameters."
?>

Try it out . . .

The variable is displayed

The following example shows how to output variables and strings using the echo command:

<?php
$txt 1 is "Learning PHP";
$txt2="w3cschool.cn";
$cars=array("Volvo","BMW","Toyota");

echo $txt1;
echo "<br>";
echo "Learn PHP $txt 2 in the "";
echo "The brand of my car is $cars the "0";
?>

Try it out . . .


PHP print statement

Print is also a language structure that can use parentheses or not: print or print().

The string is displayed

The following example shows how to output a string using the print command (strings can contain HTML tags):

<?php
print "-lt;h2-pPT is very interesting!
print "Hello world!<br>";
print "I want to learn PHP!"
?>

Try it out . . .

The variable is displayed

The following example shows how to output variables and strings using the print command:

<?php
$txt 1 is "Learning PHP";
$txt2="w3cschool.cn";
$cars=array("Volvo","BMW","Toyota");

print $txt1;
print "<br>";
print "Learn PHP $txt 2 in the "";
print "The brand of my car is $cars the "0";
?>

Try it out . . .


PhP print_r statement

print_r displays easy-to-understand information about a variable, and if a string, integer, or float is given, the variable value itself is printed.

If array is given, keys and elements are displayed in a certain format. ject is similar to an array.

When using, you must put parentheses: print_r () .

Tip: The print_r () moves the pointer to the array to the last edge. U se reset() to get the pointer back to the beginning.

The string is displayed

The following example shows how to use print_r command output string(strings can contain HTML tags):

<?php
print_r("Hello World!");
print_r("Goodbye World!");
?>

Try it out . . .

The variable is displayed

The following example shows how to use print_r command to output variables and strings:

<?php
$txt1=" Learn PHP ";
$cars=array("Volvo","BMW","Toyota");

print_r($txt1);
print_r($cars);
?>

Try it out . . .

In the next section, we'll look at the types of data in PHP.