PHP Print

PHP print statement is used to print the string, multi-line strings, escaping characters, variable, array, number etc like echo. PHP echo and print both are used to display output data in web browser.  You should know below points for print statement.

  • The print statement can be used with or without parentheses like  print or print().
  • Always print statement returns an integer value 1.
  • Print displays output data slower than the echo statement.
  • We cannot pass multiple arguments using print statement

PHP Syntax:

print("");

print '';

PHP Code Example 1:

<?php
print 'Learn Online Web Tutorials by aryatechno!<br/>';  //used single quote for string
print "Learn Online Web Tutorials by aryatechno!<br/>";  //used double quote for string
print("Learn Online Web Tutorials by aryatechno!<br/>")  //print with parentheses()
?>

Output:
Learn Online Web Tutorials by aryatechno!
Learn Online Web Tutorials by aryatechno!
Learn Online Web Tutorials by aryatechno!

PHP print - printing variable value and variable name.

PHP Code Example 2:

Look at below example. You can see that variable $str inside single quote('') shows variable name and variable $str inside double quote ("") shows variable value.

<?php
$str= "Learn Online Web Tutorials by aryatechno!";
print '$str<br/>'; //used single quote for variable $str
print "$str<br/>"; //used double quote for variable $str
print('$str<br/>'); //print with parentheses() with single quote for variable $str
print("$str<br/>"); //print with parentheses() with double quote for variable $str
?>

Output:

$str
Learn Online Web Tutorials by aryatechno!
$str
Learn Online Web Tutorials by aryatechno!

Print statement returns an integer value 1

PHP Code Example 3:

<?php
echo print "";
?>

Output:
1

PHP print - printing multi line string using NOW doc and Heredoc representation

Heredoc and Nowdoc are two methods for defining a string

Nowdocs are used to show single-quoted strings and Heredoc are used to show double-quoted strings.

<?php
print <<<"EOF"
Heredoc representation<br>
Good morning!!!<br>
How are you??<br>
EOF;

print <<<'NOW'
NOWdoc representation<br>
Good morning!!!<br>
How are you??<br>
NOW;
?>

Output:

Heredoc representation
Good morning!!!
How are you??
NOWdoc representation
Good morning!!!
How are you??

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

83768