Difference between echo and print in PHP
PHP echo and print both are PHP Statement.
PHP echo and print both are PHP Statement.
Both are used to display the output in PHP.
echo is a statement i.e used to display the output. It can be used with
parentheses echo( ) or without parentheses echo.
echo is faster then print
Using echo can pass multiple string seperated as ( , )
echo doesn’t return any value
Examples
//echo
<?php
$name="Ravi";
echo $name;
//or
echo ($name);
?>
Output: Ravi
//print
<?php
$name="Ravi";
print $name;
//or
print ($name);
?>
Output: Ravi
Pass multiple argument
//echo
<?php
$name = "Ravi ";
$profile = "PHP Developer";
$age = 25;
echo $name , $profile , $age, " years old";
?>
Output: Ravi
PHP Developer 25 years old
//print
<?php
$name = "Ravi ";
$profile = "PHP Developer";
$age = 25;
print $name , $profile , $age, " years old";
?>
Output: Parse error: syntax error
Check return type
//echo
<?php
$name = "Ravi ";
$ret = echo $name;
echo $ret;
?>
Output: Parse error: syntax
error, unexpected T_ECHO
//print
<?php
$name = "Ravi ";
$ret = print $name; //To test it returns or not
print $ret;
?>
Output: Ravi
Comments in php
A comment is non-executable lines. comment is used to
write description for your own understanding.
Browser doesn’t read the comments.
There are two types
of comments used in php:-
1. Single line comments
2. Multi-lines comments
Single line comment
Single line comment
used for short explanations.Declaration of Single line
comment are two types. Either Begin with(#) Or backslash(//)
Example:-
<?php
# This is the
single line comment
# This is the
next line comment
// This is also a
single line comment.
?>
In
the above Example
First and second line comments begin with hash(#).
The third one is begin with(//).
If we check the
output of the given example.
Browser show blank page. Beacuse comments are always non-executable..
Browser show blank page. Beacuse comments are always non-executable..
Multi lines comments
Multi lines
comments used to comment multiple lines. Here we can give comments in
bulk. The bulk comments are enclose within (/*…..*/)
Example:-
<?php
/* This is a comment with multiline
Company :HCL-CDC
view: Multiline
Comments Demo
*/
?>
The all lines which
is define in php environment are
Multiline comments, it is non-executable. Because it enclose with Multiline
comments statement.