PHP Echo and Print Statement
Delivering text as output
In PHP, there are two language constructs used for printing anything like string, variable etc.
Important point about PHP print
- Print statement delivers only one string or variable to the output screen (Browser).
- Print always return one.
- You cannot pass the multiple arguments in case of the print.
- It's slower than an echo.
PHP Print Example
<?php print " Hello PHP ! "; print " Welcome to the PHP
"; print " use of print statement
"; $number=15; print($number); // print the value of $number on screen ?>
Print is not a function, it is a construct so there is no requirement to use parentheses. But if you want you can use parentheses.
PHP Multiple argument in print
<?php $name_of_website = "pTutorial"; $roll_of_student = "John"; print $name_of_website, $roll_of_student; ?>
In the above example, there are two variables and both variables have some value. Now pass both values into the print construct separated by comma it will give some error like shown above.
- Echo statement delivers one or more than one string or variable to the output.
- You can pass multiple arguments in the case of the echo.
- It's faster than print statement.
PHP echo syntax
void echo ( string $variable [, string $... ] )
Any variable or string (It may be single-quoted or double-quoted).
The PHP echo statement is used in the following manner
<?php echo 'welcome to the PHP
'; echo "2nd method of echo "."
"; echo (" parentheses
"); $var_a= 12; $var_a1= 15; echo "variable display
"; echo ($var_a.$vara1); //using concatenation ?>
PHP echo multiple argument
<?php $name_of_website = "pTutorial"; $roll_of_student = "John"; echo $name_of_website, $roll_of_student; ?>
In the case of echo construct you can pass any number of arguments separated by a comma, it will run perfectly.
In above example, $name_of_website is a string variable that holds "pTutorial" and $roll_of_student is another variable that holds "John", pass both variables into the echo statement. It will run properly and it would give the result as shown above.
PHP string operator
PHP used dot (.) operator to concatenate of its right and left arguments.
- It concatenates variables and string.
- It concatenates variable and variable.
- It concatenates HTML tags and string.
PHP concatenate Example
<?php $a = "welcome to"; $b = $a . "php"; // string and string example echo $b."
"; $var = "wel "; $var .= "come!"; //string echo $var."
"; echo("i love my "."india"); //without using variable ?>
In this example, $a is a string variable that hold "welcome to" and $b is also a string variable that concatenates the string $a.
In the same way, $var is an string variable that holds "wel" and again it concatenate with new string "come", the final result will be "welcome".
You can also concatenate two strings within the echo construct by using dot (.) operator.
PHP string operator example
<?php $aa=7; $bb=9; echo($aa.$bb)."
"; //two integer variables its 79 echo (52 . 7)."
"; //prints the string "527" ?>
You can also concatenate two number or number variable as shown above.