PHP echo() Function
Definition and Usage
The echo() function outputs one or more strings.
Note:The echo() function is not actually a function, so you do not need to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will cause a syntax error.
Tip:The echo() function is faster than print() Slightly faster.
Tip:The echo() function also has a shorthand syntax. Before PHP 5.4.0, this syntax was only applicable if the short_open_tag configuration setting was enabled.
Syntax
echo(strings)
Parameters | Description |
---|---|
strings | Required. One or more strings to be sent to the output. |
Technical Details
Return value: | No return value. |
PHP Version: | 4+ |
More examples
Example 1
Write the value of the string variable ($str) to the output:
<?php $str = "Hello world!"; echo $str; ?>
Example 2
Write the value of the string variable ($str) to output, including HTML tags:
<?php $str = "Hello world!"; echo $str; echo "<br>What a nice day!"; ?>
Example 3
Concatenate two string variables:
<?php $str1="Hello world!"; $str2="What a nice day!"; echo $str1 . " " . $str2; ?>
Example 4
Write array values to output:
<?php $age=array("Peter"=>"35"); echo "Peter is " . $age['Peter'] . " years old."; ?>
Example 5
Write text to output:
<?php echo "This text spans multiple lines. ?>
Example 6
How to use multiple parameters:
<?php echo 'This ','string ','was ','made ','with multiple parameters.'; ?>
Example 7
Difference between single quotes and double quotes. Single quotes will output the variable name instead of the value:
<?php $color = "red"; echo "Roses are $color"; echo "<br>"; echo 'Roses are $color'; ?>
Example 8
Simplified Syntax (only applicable when the short_open_tag configuration setting is enabled):
<?php $color = "red"; ?> <p>Roses are <?=$color?></p>