Home > Backend Development > PHP Problem > How to use php print function

How to use php print function

藏色散人
Release: 2023-02-23 06:12:01
Original
3918 people have browsed it

php The print function is used to output one or more strings. Its syntax is print(strings). The parameter strings is required and refers to one or more strings sent to the output. The print() function is not actually a function, so you don't have to use parentheses with it.

How to use php print function

#How to use the php print function?

Definition and usage

print() function outputs one or more strings.

Note: The print() function is not actually a function, so you don't have to use parentheses with it.

Tip: The print() function is slightly slower than echo().

Syntax

print(strings)
Copy after login

Parameters

strings Required. One or more strings sent to the output.

Return value: Always returns 1.

PHP Version: 4

Example 1

Output the value of the string variable ($str):

<?php
$str = "I love Shanghai!";
print $str;
?>
Copy after login

Output:

I love Shanghai!
Copy after login
Copy after login

Example 2

Output the value of the string variable ($str), including HTML tags:

<?php
$str = "I love Shanghai!";
print $str;
print "<br>What a nice day!";
?>
Copy after login

Output:

I love Shanghai!
What a nice day!
Copy after login

Example 3

Concatenate two string variables:

<?php
$str1 = "I love Shanghai!";
$str2="What a nice day!";
print $str1 . " " . $str2;
?>
Copy after login

Output:

I love Shanghai! What a nice day!
Copy after login

Example 4

Output the value of the array:

<?php
$age=array("Bill"=>"60");
print "Bill Gates is " . $age[&#39;Bill&#39;] . " years old.";
?>
Copy after login

Output:

Bill Gates is 60 years old.
Copy after login

Example 5

Output text:

<?php
print "This text
spans multiple
lines.";
?>
Copy after login

Output:

This text spans multiple lines.
Copy after login

Example 6

The difference between single quotes and double quotes. Single quotes will output the variable name, not the value:

<?php
$color = "red";
print "Roses are $color";
print "<br>";
print &#39;Roses are $color&#39;;
?>
Copy after login

Output:

Roses are red
Roses are $color
Copy after login

Example 7

Write text to the output:

<?php
print "I love Shanghai!";
?>
Copy after login

Output:

I love Shanghai!
Copy after login
Copy after login

The above is the detailed content of How to use php print function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
curl - php json post print out? (Use URL)
From 1970-01-01 08:00:00
0
0
0
Print sql statement
From 1970-01-01 08:00:00
0
0
0
java - Print electronic form
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template