Latest web development tutorials

PHP echo () function

PHP String Reference PHP String Reference

Examples

Output some text:

<?php
echo "Hello world!";
?>

Running instance »

Definition and Usage

echo () function outputs one or more strings.

NOTE: echo () function is not actually a function, so you do not have to use it in parentheses.However, if you want to pass more than one parameter to echo (), using parentheses will generate a parse error.

Tip: echo () function than the print () is slightly faster.

Tip: echo () function also has simplified syntax.Prior to PHP 5.4.0 version, this syntax applies only to short_open_tag configuration settings enabled.


grammar

echo( strings )

参数 描述
strings 必需。一个或多个要发送到输出的字符串。

technical details

return value: No return value.
PHP version: 4+


More examples

Example 1

Output string variable ($ str) values:

<?php
$str = "Hello world!";
echo $str;
?>

Running instance »

Example 2

Output string variable ($ str) the value that contains HTML tags:

<?php
$str = "Hello world!";
echo $str;
echo "<br>What a nice day!";
?>

Running instance »

Example 3

Connecting two string variables:

<?php
$str1="Hello world!";
$str2="What a nice day!";
echo $str1 . " " . $str2;
?>

Running instance »

Example 4

Output array of values:

<?php
$age=array("Peter"=>"35");
echo "Peter is " . $age['Peter'] . " years old.";
?>

Running instance »

Example 5

Output some text:

<?php
echo "This text
spans multiple
lines.";
?>

Running instance »

Example 6

How to use multiple parameters:

<?php
echo 'This ','string ','was ','made ','with multiple parameters.';
?>

Running instance »

Example 7

The difference between the single and double quotes. The output variable name in single quotes instead of value:

<?php
$color = "red";
echo "Roses are $color";
echo "<br>";
echo 'Roses are $color';
?>

Running instance »

Example 8

Simplified syntax (applies only to short_open_tag configuration settings enabled):

<?php
$color = "red";
?>

<p>Roses are <?=$color?></p>

Running instance »


PHP String Reference PHP String Reference