The print() function outputs one or more strings for various data types.
The parentheses are not required for print statement.
PHP print() Function has the following syntax.
print(strings)
Parameter | Is Required | Description |
---|---|---|
strings | Required. | One or more strings to be sent to the output |
Always returns 1.
Write some text to the output:
<?php
print "Hello world!";
$str = "\nHello world!\n";
print $str;
?>
The code above generates the following result.
Write the value of the string variable ($str) to the output, including HTML tags:
<?php
$str = "Hello world from java2s.com!";
print $str;
print "<br>What a nice day!";
?>
The code above generates the following result.
Join two string variables together:
<?php
$str1="Hello world from java2s.com!";
$str2="What a nice day!";
print $str1 . " " . $str2;
?>
The code above generates the following result.
Write the value of an array to the output:
<?php
$age=array("PHP"=>"5");
print "PHP is " . $age['PHP'] . " years old.";
?>
The code above generates the following result.