Output some text:
<?php echo "Hello world!"; ?>The echo() function outputs one or more strings.
Note: The echo() function is not actually a function, so you don't have to use parentheses around it. However, if you want to pass more than one argument to echo(), using parentheses will generate a parsing error.
Tip: The echo() function is slightly faster than print().
Tip: The echo() function also has simplified syntax. Prior to PHP 5.4.0, this syntax only worked when the short_open_tag configuration setting was enabled.
echo( strings )
parameter | describe |
---|---|
strings | Required. One or more strings to send to the output. |
Return value: | There is no return value. |
---|---|
PHP version: | 4+ |
Output the value of a string variable ( $str ):
<?php$str = "Hello world!";echo $str;?>Output the value of a string variable ( $str ), including HTML tags:
<?php$str = "Hello world!";echo $str;echo "<br>What a nice day!";?>Concatenate two string variables:
<?php$str1="Hello world!";$str2="What a nice day!";echo $str1 . " " . $str2;?>Output the values of the array:
<?php$age=array("Peter"=>"35");echo "Peter is " . $age['Peter'] . " years old.";?>Output some text:
<?phpecho "This textspans multiplelines.";?>How to use multiple parameters:
<?phpecho 'This ','string ','was ','made ','with multiple parameters.';?>The difference between single quotes and double quotes. Single quotes will print the variable name, not the value:
<?php$color = "red";echo "Roses are $color";echo "<br>";echo 'Roses are $color';?>Simplified syntax (only applies if the short_open_tag configuration setting is enabled):
<?php$color = "red";?><p>Roses are <?=$color?></p>