PHP Print
Like PHP echo, PHP print is a language construct, so you don't need to use parenthesis with the argument list. Print statement can be used with or without parentheses: print and print().
Unlike echo, it always returns 1.
syntax :
int print(string $arg)
PHP print statement can be used to print the string, multi-line strings, escaping characters, variable, array, etc.
Some important points that you must know about the echo statement are:
- print
is a statement, used as an alternative to echo at many times to display
the output.
- print
can be used with or without parentheses.
- print
always returns an integer value, which is 1.
- Using
print, we cannot pass multiple arguments.
- print
is slower than the echo statement.
Print
statements to print:
Practice Assignments
Example 1:
Single line:
<? php print
( “ Hello World”); ?> |
Output:
Hello
World
Example 2:
Used multi line string
<? Php print “Hello by PHP print this is multi line text printed by PHP print statement ; ?> |
Output:
Hello by PHP print this is multi line text printed by PHP print
statement
Example 3:
Printing escaping characters
<?
php print "Hello escape \"sequence\" characters by PHP print"; ?> |
Output:
Hello escape "sequence" characters by PHP print
Example 4:
Printing variable Value
<?php $msg="Hello print() in PHP"; print "Message is: $msg"; ?> |
Output:
Message is: Hello print() in PHP
**************