PHP echo is a language construct, not a function. Therefore, you don’t need to use parenthesis with it. But if you want to use more than one parameter, it is required to use parenthesis.
The syntax of PHP echo is given below:
void echo ( string $arg1 [, string $… ] )
PHP echo 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:
- echo is a statement, which is used to display the output.
- echo can be used with or without parentheses: echo(), and echo.
- echo does not return any value.
- We can pass multiple strings separated by a comma (,) in echo.
- echo is faster than the print statement.
PHP echo: printing string
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>echo</title> </head> <body> <?php echo "Hello Dharmendra Yadav"; ?> </body> </html> |
Output:
Hello Dharmendra Yadav
PHP echo: printing escaping characters
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>echo</title> </head> <body> <?php echo "Hello escape \"sequence\" characters"; ?> </body> </html> |
Output:
Hello escape “sequence” characters
PHP echo: printing variable value
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>echo</title> </head> <body> <?php $msg="Hello Dharmendra Yadav"; echo "Message is: $msg"; ?> </body> </html> |
Output:
Message is: Hello Dharmendra Yadav