Detailed explanation of two basic output parties and examples of php

 In this article, the editor has compiled a detailed explanation of the two basic output parties and examples of php for you. Friends who need it can follow the learning and reference.

In PHP, there are two basic output methods: echo and print .

Difference between echo and print

  • echo - output one or more strings, can take multiple arguments and return no value
  • print - prints only one string, accepts only one argument and returns a value, and always returns 1

Tip: echo is slightly faster than print because it returns no value.

PHP echo statement

1. echo is a language construct that can be used with or without parentheses: echo or echo();

2. Display the string, the following example shows how to use the echo command to display different strings (at the same time, please note that the string can contain HTML tags)

1
2
3
4
5
6
7
8
9
10
<?php
echo "<h2>PHP is fun!</h2>";
echo(123);
echo "<br>";
echo("php");
echo "<br>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This", " string", " was", " made", " with multiple parameters.";
?>

Effect:

PHP is fun!
123
php
Hello world!
I'm about to learn PHP!
This string was made with multiple parameters.

3. Display variables, the following example shows how to use the echo command to display strings and variables;

1
2
3
4
5
6
7
8
9
10
<?php
$txt1="Learn PHP";
$txt2="W3School.com.cn";
$cars=array("Volvo","BMW","SAAB");
 
echo $txt1;
echo "<br>";
echo "Study PHP at $txt2 <br />";
echo "My car is a {$cars[0]}";
?>

Effect:

Learn PHP
Study PHP at W3School.com.cn
My car is a Volvo

PHP print statement

1. Print is also a language structure, and can be used with or without parentheses: print or print().

2. Display strings. The following example shows how to use the print command to display different strings (at the same time, please note that strings can contain HTML tags):

1
2
3
4
5
<?php
print "<h2>PHP is fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>

Effect:

PHP is fun!
Hello world!
I'm about to learn PHP!

3. Display variables, the following example shows how to use the print command to display strings and variables:

1
2
3
4
5
6
7
8
9
10
<?php
$txt1="Learn PHP";
$txt2="W3School.com.cn";
$cars=array("Volvo","BMW","SAAB");
 
print $txt1;
print "<br>";
print "Study PHP at $txt2 <br>";
print "My car is a {$cars[0]}";
?>

Effect:

Learn PHP
Study PHP at W3School.com.cn
My car is a Volvo

So far, this article about the two basic output methods of php and the detailed explanation of the examples is introduced here. For more information about the two basic output methods of php, please search for the previous articles of Script Home or continue to browse the related topics below. The article hopes that everyone will support Script Home in the future!



Post a Comment

Previous Post Next Post