PHP print() function
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; ?>
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!'; ?>
Example 3
Concatenate two string variables:
<?php $str1 = "I love Shanghai!"; $str2="What a nice day!"; print $str1 . ' ' . $str2; ?>
Example 4
Output the value of an array:
<?php $age=array("Bill"=>"60"); print 'Bill Gates is ' . $age['Bill'] . ' years old.'; ?>
Example 5
Output text:
<?php print 'This text spans multiple lines."; ?>
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'; ?>