The two basic output methods of php are "echo" and "print". echo is used to output one or more strings, which can take multiple arguments and have no return value, with the syntax "echo($ str)"; and print is used to output a string that can only take one argument and has a return value, syntax "print($str)".

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

The difference between echo and print

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

Tip: echo is slightly faster than print because it does not return any values.

PHP echo statement

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

2.Display String,The following example shows how to use the echo command to display different strings (and please note that the strings 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("I love php");

echo "<br>";

echo "Hello world!<br>";

echo "I'm about to learn PHP!<br>";

echo "This", " string", " was", " made", " with multiple parameters.";

?>

Effect:

1

2

3

4

5

6

PHP is fun!

123

I love php

Hello world!

I'm about to learn PHP!

This string was made with multiple parameters.

3.Show 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]}";

?>

效果:

1

2

3

Learn PHP

Study PHP at W3School.com.cn

My car is a Volvo

PHP print statement

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

2.To display strings, the following example shows how to use the print command to display different strings (and note that the 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:

1

2

3

PHP is fun!

Hello world!

I'm about to learn PHP!

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

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:

1

2

3

Learn PHP

Study PHP at W3School.com.cn

My car is a Volvo

Related articles

How to display a mess of php images?

php image display a mess of solutions: 1, open the corresponding PHP code file; 2, in the page header plus "header ("Content-Type: image/jpg");" code to declare the image type can be.

php can't load js css

php can not load js css solution: 1, check the correct path to load; 2, prohibit the use of absolute path; 3, use relative path to load the file can be.

php how to check user ip

php query user ip method: 1, get user ip by "$_SERVER["REMOTE_ADDR"]" way; 2, get user ip by "($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];" to get the user ip and so on.

How to invert a string by word in php

Inversion methods: 1. use "explode(' ',$str)" statement to convert string to array; 2. use "array_reverse($arr)" statement to invert the array and return the array in the reverse order of elements; 3. use "implode(' ',$arr)" statement to convert the inver

php get maximum value in several numbers

In php, you can use the max() function to get the maximum value among several numbers. The function works by calculating and returning the maximum value among several specified values, with the syntax "max(value1,value2,...) ;".

How to convert php image to base64

php image to base64 method: 1, get an image address; 2, through the "chunk_split(base64_encode($gambar));" method to convert the image to base64 string format can be.

How to merge arrays in PHP

How to merge arrays in PHP? To do this we need to use the array_merge function and the array_merge_recursive function. Here we will introduce these two functions separately.

How to compare strings in PHP?

String is an important data type in PHP, which how to compare strings is also very common in our development work, there are many kinds of comparison methods to compare strings, here we will introduce the more commonly used several comparison methods.

The difference between jumps and redirects in PHP

Difference: 1. Jump is the current URL request success, re-request a new URL; while redirect is the current URL is invalid, was redirected to a new URL. 2. In the jump, the browser will record the current URL and the new URL to the history; while redirect