print – PHP String Functions

Syntax :

print ( strings );

Description :

The print() function outputs one or more strings.

It is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list.

The major differences to echo() are that print only accepts a single argument and always returns 1.

Note : The print() function is not actually a function, so you are not required to use parentheses with it.
Tip: The print() function is slightly slower than echo().

Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|lef|lef|left”]
Name, Required /Optional, Value Type, Description
strings , Required, String , It is the string for which is to be parsed.
[/table]


Output :

print() always Returns 1.


Related articles  :  echo(), printf(), flush(), sprintf(), fprintf()number_format().


print() – PHP Functions Example 1 : Write some text to output.
<?php
print "Hi to all!"; // with parantheses
print "\n";
print ("Hi to all!");  // without parantheses
print "\n";
// multiple lines text

print "This text spans
in multiple lines. This text 
spans in multiple lines.";
print "\n";
//using new line character

print "This text \nspans in multiple lines. This text spans in \nmultiple lines.
This spans\nmultiple lines. The newlines will be\noutput as well.";
print "\n";
//using escaping character
print "\n";
print "escaping characters is done \"Like this\".";
?>

See below is the output of above code in Web browser.

Hi to all!
Hi to all!
This text spans
in multiple lines. This text
spans in multiple lines.
This text
spans in multiple lines. This text spans in
multiple lines.
This spans
multiple lines. The newlines will be
output as well.

escaping characters is done “Like this”.


print() – PHP Functions Example 2 :  You can use variables inside a print statement
<?php
$strName = "Name";
$strValue = "Test";
print "$strName is $strValue";
?>

See below is the output of above code in Web browser.

Name is Test


print() – PHP Functions Example 3 : Arrays example
<?php
$age=array("Arnav"=>"6");
print "Arnav is " . $age['Arnav'] . " years old.";
?>

See below is the output of above code in Web browser.

Arnav is 6 years old.

print() – PHP Functions Example 4 : Single quotes will print the variable name, not the value
<?php
$strName = "Arnav";
print 'Name is $strName';   
?>

See below is the output of above code in Web browser.

Name is $strName


print() – PHP Functions Example 5 : Double quotes will print the  value
<?php
$strName = "Arnav";
print "Name is $strName"; 

// If you are not using any other characters, you can just print variables 
print "\n"; 
print $strName; 
?>

See below is the output of above code in Web browser.

Name is Arnav
Arnav

You may also like...