(学习笔记)PHP的输出echo和print

时间:2021-10-26 05:40:43

echo 和 print 之间的差异:

echo - 能够输出一个以上的字符串

print - 只能输出一个字符串,并始终返回 1

echo语句

echo 或 echo()均可。

输出换行

 echo "<br>";

输出字符串

   $str = "Hello!";
echo "多","个","字","符","串";/*输出多个字符串*/

输出HTML语句

echo "<h1>HTML<h1>";

输出多个变量

$str1="第一个";
$str2="第二个";
echo $str1 , "中间" .$str2,"<br>";
echo $str1.$str2,"<br>";
echo "{$str1}中间{$str2}<br>";

输出数组

/*定义数组*/
$a = array(
"1" => "A",
"2" => "B"
);
/*输出一个值*/
echo $a[1], "<br>";
/*输出所有值*/
foreach ($a as $b) {
echo $b, "<br>";
}
/*输出所有键、值*/
foreach ($a as $key => $value) {
echo $key . "=>" . $value, "<br>";
}

单引号和双引号区别

单引号将输出变量名称,而不是值

$color = "red";
echo "$color", "<br>";
echo '$color', "<br>";