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 //w w  w  .  j a v  a  2  s. c o  m
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/*  w w w.  j a  v  a2  s .  c om*/
$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/*w  w w.  jav a 2s  .  c om*/
$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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions