PHP Print

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP Print

Description

Print is used to display a string.

Syntax

print(string $val)

Parameters

val : The input data.

Return Values

Returns 1, always.

Print() is not actually a real function, it is a language construct like echo. There is some difference between the two, echo is marginally faster compare to print as echo does not return any value. Echo can accept multiple parameters without parentheses but print can accept only one parameter.

Example to display simple string with print()

  1. print 'One line simple string.
    ';  
  2. print 'Two lines simple  
  3. string example
    ';  
  4. print 'Tomorrow I 'll learn PHP global variables.
    ';  
  5. print 'This is a bad command : del c:*.* 
    ';  
  6. ?>  

All the above print commands simply display the corresponding string, here we have used an additional html command
at end of each print statement to generate a line break. 

Output :

One line simple string.
Two lines simple string example
Tomorrow I 'll learn PHP global variables.
This is a bad command : del c:*.*

View the example in the browser

Advance example of PHP print()

  1. // Variables inside an echo statement.  
  2. $abc='We are learning PHP';  
  3. $xyz='w3resource.com';  
  4. print "$abc at $xyz 
    ";  
  5. // Simple variable display  
  6. print $abc;  
  7. print "
    "; // creating a new line  
  8. print $xyz;  
  9. print "
    "; // creating a new line  
  10. // Displaying arrays  
  11. $fruits=array('fruit1'=>'Apple','fruit2'=>'Banana');  
  12. print "Fruits are : {$fruits['fruit1']} and {$fruits['fruit2']}" ;  
  13. ?>   

Output :

We are learning PHP at w3resource.com
We are learning PHP
w3resource.com
Fruits are : Apple and Banana

View example in the browser

Practice here online :