Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - print() Function



The PHP print() function is used to output a string (text) or any other data. It is similar to the echo statement, but the difference is that print() always returns 1, which cannot be seen but indicates that the operation was performed successfully.

The print() function is not actually a function, so not required to use parentheses with it.

Syntax

Following is the syntax of the PHP print() function −

print(string $exp): int

Parameters

Following is the parameter of this function −

  • arg − It is an input data or expression to be output.

Return value

This function always returns 1.

Example 1: Printing statement

The following is the basic example of the PHP print() function −

<?php
   #print("Welcome to Tutoralspoint!"), or
   print "Welcome to Tutoralspoint!"; 
?>

Output

The above program produces the following output −

Welcome to Tutoralspoint!

Example 2: Statement along with Argument

This example shows how to print an statement along with an output using print() function −

<?php
   $name = "Sriansh";
   echo "Data: $name";
   //statement along with an argument
   print "\nHey!, my name is $name"; 
?>

Output

After executing the above program, the following output will be displayed −

Data: Sriansh
Hey!, my name is Sriansh

Example 3: Statement Based on Condition

In the example below, we use the print() function to print a statement based on a specified condition −

<?php
   $person1 = "Sriansh";
   $person2 = "Aman";
   $age = 18;
   echo "Persons: $person1, and $person2";
   //statement based on condition
   print "\nA person who us eligible for voting: ";
   print $age > 18 ? $person1 : $person2;
?>

Output

Following is the output of the above code −

Persons: Sriansh, and Aman
A person who us eligible for voting: Aman
php_function_reference.htm
Advertisements