PHP echo() 函数
PHP echo() 函数是一个重要的字符串函数。它用于显示一个或多个字符串。换句话说,我们可以说 echo() 函数输出一个或多个字符串。
注意:echo() 函数比 print() 函数稍快。
语法
void echo ( string arg1 [, string... ] )
参数 | 描述 | 必填/可选 |
---|---|---|
strings | 指定一个或多个字符串 | 必填 |
示例1
<?php
str ="Hello JavaTpoint";
echo "By using 'echo()' function your string is :".str;
?>
输出:
By using 'echo()' function your string is :Hello JavaTpoint
示例2
<?php
str1 ="Hello JAVA";str2 ="Hello JavaTpoint";
echo "By using 'echo()' function your string is :".str1."<br>".str2 ;
?>
输出:
By using 'echo()' function your string is :Hello JAVA
Hello JavaTpoint
示例3
<!DOCTYPE html>
<html>
<head>
<title>PHP 'echo()' function </title>
</head>
<body>
<?php
str ="Red";
?>
<p>Your Shirt color is : <?=str?></p>
</body>
</html>
输出:
Your Shirt color is : Red
注意:只有在启用了short_open_tag配置设置的情况下,快捷语法才有效。
示例4
<?php
echo "Your number value is: (1 + 2)"."<br>";
echo "By using 'echo()' function: Addition value is: ".(1 + 2)."<br>";
?>
输出:
Your number value is: (1 + 2)
By using 'echo()' function: Addition value is: 3
示例5
<?php
echo 'Hello ' . (isset(name) ?name : 'John Doe') . '!';
?>
输出:
Hello John Doe!
示例6
<?php
echo "Your string is : 'This ','string ','was ','made ','with multiple parameters.'"."<br>";
echo 'This ','string ','was ','made ','with multiple parameters.';
?>
输出:
Your string is : 'This ','string ','was ','made ','with multiple parameters.'
This string was made with multiple parameters.
示例7
<?php
age=array("John"=>"35");
echo "John is " .age['John'] . " years old.";
?>
输出:
Peter is 35 years old.