有没有更好的方法用PHP输出数据到HTML页面?
如果我想用php中的一些var创建一个div,我会写这样的东西
print ('<div>'.$var.'</div>');
Run Code Online (Sandbox Code Playgroud)
要么
echo "'<div>'.$var.'</div>'";
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?
或者更好的方法,填写$tempvar并打印一次?像那样:
$tempvar = '<div>'.$var.'</div>'
print ($tempvar);
Run Code Online (Sandbox Code Playgroud)
事实上,在现实生活中,var将会充满更多!
Asa*_*aph 30
PHP echo和printPHP 之间有2个不同之处:
print返回一个值.它总是返回1.
echo 可以使用逗号分隔的参数列表来输出.
总是返回1并不是特别有用.并且可以使用多个调用或字符串连接来模拟以逗号分隔的参数列表.所以之间的选择echo和print几乎归结为风格.我见过的大多数PHP代码都使用了echo.
printf()是c的直接类比printf().如果你对这个习语感到满意,你可以使用printf().然而,年轻一代的很多人发现printf()特殊字符语法的可读性低于等效echo代码.
有可能是之间的性能差异echo,print以及printf,但我不会因为在一个数据库驱动的Web应用程序(PHP的典型域)太挂在他们身上,打印字符串,客户端几乎可以肯定不是你的瓶颈.最重要的是,3个中的任何一个都将完成工作,而另一个并不比另一个好.这只是一种风格问题.
你甚至可以写
$var = "hello";
echo "Some Text $var some other text";
// output:
// Some Text hello some other text
Run Code Online (Sandbox Code Playgroud)
要么
print("Some Text $var some other text");
// output:
// Some Text hello some other text
Run Code Online (Sandbox Code Playgroud)
并没有太大的区别.这仅适用于双引号.单引号却没有.例:
$var = "hello";
echo 'Some Text $var some other text'; // Note the single quotes!
// output:
// Some Text $var some other text
Run Code Online (Sandbox Code Playgroud)
要么
print('Some Text $var some other text'); // Note the single quotes!
// output:
// Some Text $var some other text
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
44247 次 |
| 最近记录: |