PHP print()Functions

PHP String 参考手册PHP String Reference Manual

Examples

Output some text:

<?php
print "Hello world!";
?>

Run Example »

Definition and Usage

The print() function outputs one or more strings.

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

Tip:The print() function is slightly slower thanecho()in speed.


Syntax

print(strings)

Parameters Description
strings Required. One or more strings to be sent to the output.

Technical Details

Return value: Always returns 1.
PHP Version: 4+


More Examples

Example 1

Output the string variable ($str)'s value:

<?php
$str = "Hello world!";
print $str;
?>

Run Example »

Example 2

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

<?php
$str = "Hello world!";
print $str;
print "<br>What a nice day!";
?>

Run Example »

Example 3

Concatenate two string variables:

<?php
$str1="Hello world!";
$str2="What a nice day!";
print $str1 . " " . $str2;
?>

Run Example »

Example 4

Output the value of the array:

<?php
$age=array("Peter"=>"35");
print "Peter is " . $age['Peter'] . " years old.";
?>

Run Example »

Example 5

Output some text:

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

Run Example »

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 'Roses are $color';
?>

Run Example »


PHP String 参考手册PHP String Reference Manual Other Extensions