Home > Backend Development > PHP Problem > How to use php echo function

How to use php echo function

藏色散人
Release: 2023-02-22 19:08:02
Original
2897 people have browsed it

php The echo function is used to output one or more strings. Its syntax is echo(strings). The parameter strings is required and refers to one or more strings to be sent to the output.

How to use php echo function

#How to use the php echo function?

Definition and usage

echo() function outputs one or more strings.

Note: The echo() function is not actually a function, so you don't have to use parentheses with 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 if the short_open_tag configuration setting was enabled.

Syntax

echo(strings)
Copy after login

Parameters

strings Required. One or more strings to send to the output.

Return value: No return value.

PHP version: 4

Instance 1

Output the value of the string variable ($str):

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

Example 2

Output the value of the string variable ($str), including HTML tags:

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

Example 3

Connect two string variables:

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

Example 4

Output the value of the array:

<?php
$age=array("Peter"=>"35");
echo "Peter is " . $age[&#39;Peter&#39;] . " years old.";
?>
Copy after login

Example 5

Output some text:

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

Example 6

##How to use multiple parameters:

<?php
echo &#39;This &#39;,&#39;string &#39;,&#39;was &#39;,&#39;made &#39;,&#39;with multiple parameters.&#39;;
?>
Copy after login

Example 7

The difference between single quotes and double quotes. Single quotes will output the variable name, not the value:

<?php
$color = "red";
echo "Roses are $color";
echo "<br>";
echo &#39;Roses are $color&#39;;
?>
Copy after login

Example 8
##Simplified syntax (only applies if the short_open_tag configuration setting is enabled):

<?php
$color = "red";
?>
<p>Roses are <?=$color?></p>
Copy after login

The above is the detailed content of How to use php echo function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template