PHP print() function

Example

Write text to the output:

<?php
print "I love Shanghai!";
?>

Run Instances

Definition and Usage

The print() function outputs one or more strings.

Comment:The print() function is not actually a function, so you do not need to use parentheses with it.

Tip:print() function is slower than echo() Slower.

Syntax

print(strings)
Parameters Description
strings Required. Send one or more strings to the output.

Technical Details

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

More Examples

Example 1

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

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

Run Instances

Example 2

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

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

Run Instances

Example 3

Concatenate two string variables:

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

Run Instances

Example 4

Output the value of an array:

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

Run Instances

Example 5

Output text:

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

Run Instances

Example 6

Difference between single quotes and double quotes. Single quotes output the variable name instead of the value:

<?php
$color = 'red';
print 'Roses are $color';
print '<br>';
print 'Roses are $color';
?>

Run Instances