PHP echo
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.
What is a language construct?
A language construct is
accepted/executed by the PHP parser as it is, in other words the PHP parser
doesn't have to parse and modify a language construct to execute it, as it is
ready for execution by default. Hence, language constructs are faster than any
built-in functions.
syntax :
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.
Echo
statements to display:
Practice Assignments
1. Single line:
<?
php echo
“ Hello World”; ?> |
Output:
Hello World
Example 2.
Used multiple string by using comma
<? php echo
‘this’, ‘is’, ’a’, ’multiline ‘, ‘statement’; ?> |
Output:
This
is a multiline statement.
3. Example 3:
Printing a multiline text(string)
<? php echo
“This is a multiple
text example; ?> |
Output:
This is a multiple text
example.
Example 4:
4.
To
print a string variable:
<?
Php $str=”String
variable”; echo
$str; ?> |
Output:
String
variable
Example 5:
5.
To
print a string variable with some text:
<? php $str=”string
variable”; echo “I am
$str”; echo ‘ I am
$str’; ?> |
Output:
I
am String variable.
I
am $str.
Example 6:
6. Printing escaping
characters
<?php echo "Hello escape \"sequence\" characters"; ?> |
Output:
Hello escape
"sequence" characters