PHP Print
The PHP print statement is also a language construct and It is similar to the echo statement and can be used alternative to echo many times.
The print statement can be used with or without parentheses: print or print().
Important Points for Print
- Echo does not behave like a function whereas print behaves like a function.
- The print statement can have only one argument at a time and thus can print a single string.
- The print statement always returns a value of 1.
- Print is slower than the echo statement.
Below are some examples of using print statements in PHP:
1. Display String:
Example
<?php
print "Hello Vaidikalayans\n";
print ("Hello Vaidikalayans");
?>
2. Display Multi-Line String:
Example
<?php
print "Hello This is
Multi-Line String";
?>
Hello This is
Multi-Line String
Multi-Line String
3. Display Escaping Characters:
Example
<?php
print "PHP stands for \"Hypertext Preprocessor\" ";
?>
PHP stands for "Hypertext Preprocessor"
4. Display Variables:
Example
<?php
$text = "Hello Vaidikalayans";
$num1 = 6;
$num2 = 3;
print $text."\n";
print $num1 + $num2;
?>
Hello Vaidikalayans
9
Print Use Cases:
1. The print statement always returns a value of 1.
Example
<?php
$res=print "Hello Vaidikalayans\n";
print "Return Value: ".$res;
?>
2. Multi-Argument Strings not allowed in Print
Example
<?php
print "One ","Two ","Three";
//OR
print ("One ","Two ","Three");
?>