PHP Tutorial - PHP print() Function






Definition

The print() function outputs one or more strings for various data types.

The parentheses are not required for print statement.

Syntax

PHP print() Function has the following syntax.

print(strings)

Parameter

ParameterIs RequiredDescription
stringsRequired.One or more strings to be sent to the output

Return

Always returns 1.

Example 1

Write some text to the output:


<?php 
print "Hello world!"; 
$str = "\nHello world!\n";
print $str;

?>

The code above generates the following result.





Example 2

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.

Example 3

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.





Example 4

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.