1:echo:是语句不是函数,没有返回值,可输出多个变量值,不需要圆括号。不能输出数组和对象,只能打印简单类型(如int,string)。
2:print:是语句不是函数,有返回值 1 ,只能输出一个变量,不需要圆括号。不能输出数组和对象,只能打印简单类型(如int,string)。
输出字符串
echo "<h2>PHP is fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This", " string", " was", " made", " with multiple parameters.";
print "<h2>PHP is fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
输出变量
echo "Study PHP at $txt2"; //php 双引号内部可包含变量
echo "My car is a {$cars[0]}"; //用大括号 显式的指定这是变量
print "Study PHP at $txt2";
print "My car is a {$cars[0]}";
3:print_r:是函数,可以打印复合类型,例如:stirng、int、float、array、object等,输出array时会用结构表示,而且可以通过print_r($str,true)来使print_r不输出而返回print_r处理后的值
4:printf:是函数,有返回值,返回值是打印内容的长度,把文字格式化以后输出(参看C语言)
5:sprintf:是函数,跟 printf 相似,但不打印,而是返回格式化后的文字(该函数把格式化的字符串写写入一个变量中,而不是输出来),其 他的与 printf 一样。
例如:
$str = "Hello";
$number = 123;
$txt = sprintf("%s world. Day number %u",$str,$number);
//输出: Hello world. Day number 123
6:var_dump():函数,输出变量的内容、类型或字符串的内容、类型、长度。常用来调试。
可以通过function_exists(‘函数名称’)进行测试
var_dump(function_exists('print')); //bool(false)
var_dump(function_exists('echo')); //bool(false)
var_dump(function_exists('print_r')); //bool(true)
以上就是本文的全部内容,感谢大家支持JScript之家——编程学习者社区!
|