In PHP to print output echo statement is use.
PHP echo is use to print string, escaping characters, variable values.
The echo statement can be used with or without parentheses: echo or echo().
Syntax for echo:
1 |
echo " //statements "; |
Example
1 2 3 4 5 |
<?php $a = 10; $b = 20; echo $a + $b; ?> |
output:
1 |
30 |
Example:
1 2 3 4 5 6 7 8 9 |
<?php $a = 10; $str1 = "hi"; $str2 = 'hello'; echo "$a"; //10 echo '$a'; //$a echo "$str1"; //hi echo "$str2"; //hello ?> |
In above program we see the echo statements inside double quotes (” “) will print value of variable.
Whereas, echo statements inside single quotes(‘ ‘) will print as it is.
echo is used to print variable, variable value, text, statement, multi-line statements, escping characters etc.
PHP echo: print variable value
1 2 3 4 |
<?php $a = 10; echo $a; //10 ?> |
Output:
1 |
10 |
PHP echo: print statement
1 2 3 |
<?php echo "Hello everyone"; ?> |
Output:
1 |
Hello everyone |
PHP echo: print multi-line statements
1 2 3 4 5 6 |
<?php echo "Hello everyone welcome to webEncyclop you are learning PHP "; ?> |
Output:
1 |
Hello everyone welcome to webEncyclop you are learning PHP |