php echo 输出字符串函数详解


Posted in PHP onMay 13, 2010
echo "asd";//字符串 
echo "ads$c";//字符串+变量 
echo 'ads$c';//字符串 asd$c $c不是变量 
echo "sd"."vs"; 
echo "sd","vs"; 
echo $a; 
echo $a.$b; 
echo $a,$b; 
echo $a.$b.$c; 
echo $a,$b,$c; 
echo "kaskd{$c}asd"; 
echo "kakskd{$arr['lo']}"; 
echo "kakskd{$obj->a}"; 
echo "kaskd".$c."kasd"; 
echo "kaskd".$arr['lo']."kasd"; 
echo "kaskd".$obj->a."kasd"; 
echo "kaskd".func($c)."kasd"; 
echo "kaksk".($a+1)."dkkasd"; 
echo $c."jaksd"; 
echo $c,"jaksd"; 
//php多行输出方法 
echo <<<END 
This uses the "here document" syntax to output 
END; 
//输出简写 
<?php echo $a;?> <?=$a?>
<?php 
echo "Hello World"; echo "This spans 
multiple lines. The newlines will be 
output as well"; 
echo "This spans\nmultiple lines. The newlines will be\noutput as well."; 
echo "Escaping characters is done \"Like this\"."; 
// You can use variables inside of an echo statement 
$foo = "foobar"; 
$bar = "barbaz"; 
echo "foo is $foo"; // foo is foobar 
// You can also use arrays 
$baz = array("value" => "foo"); 
echo "this is {$baz['value']} !"; // this is foo ! 
// Using single quotes will print the variable name, not the value 
echo 'foo is $foo'; // foo is $foo 
// If you are not using any other characters, you can just echo variables 
echo $foo; // foobar 
echo $foo,$bar; // foobarbarbaz 
// Some people prefer passing multiple parameters to echo over concatenation. 
echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10); 
echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n"; 
echo <<<END 
This uses the "here document" syntax to output 
multiple lines with $variable interpolation. Note 
that the here document terminator must appear on a 
line with just a semicolon. no extra whitespace! 
END; 
// Because echo does not behave like a function, the following code is invalid. 
($some_var) ? echo 'true' : echo 'false'; 
// However, the following examples will work: 
($some_var) ? print 'true' : print 'false'; // print is also a construct, but 
// it behaves like a function, so 
// it may be used in this context. 
echo $some_var ? 'true': 'false'; // changing the statement around 
?>

以下是官方手册说明:
Definition and Usage
定义和用法
The echo() function outputs one or more strings.
echo()函数的作用是:输出一个或多个字符串。
Syntax
语法
echo(strings)
Parameter参数 Description描述
strings Required. One or more strings to be sent to the output
必要参数。指定一个或多个需要被发送到结果中的字符串
Tips and Notes
提示和注意点
Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.
注意:echo()函数不是一个真正意义上的函数,所以你没有必要一定去使用它。如果你想把多于一个的参数传递给echo()函数,那么使用圆括号“()”将产生错误。
Tip: The echo() function is slightly faster than print().
提示:echo()函数相当于print()函数的简化版本。
Tip: The echo() function has the following shortcut syntax. See example 5.
提示:echo()函数包含下面的简便写法。具体见:案例5。
Example 1
案例1
<?php 
$str = "Who's Kai Jim?"; 
echo $str; 
echo "<br />"; 
echo $str."<br />I don't know!"; 
?>

The output of the code above will be:
上述代码将输出下面的结果:
Who's Kai Jim?Who's Kai Jim?I don't know!

Example 2
案例2

<?php 
echo "This textspans multiplelines."; 
?>

The output of the code above will be:
上述代码将输出下面的结果:
This text spans multiple lines.

Example 3
案例3

<?php 
echo 'This ','string ','was ','made ','with multiple parameters'; 
?>

The output of the code above will be:
上述代码将输出下面的结果:
This string was made with multiple parameters

Example 4
案例4
Difference of single and double quotes. Single quotes will print the variable name, not the value:
区别单引号(')和双引号(”)的不同。单引号将输出变量名,而不是变量的值:

<?php 
$color = "red"; 
echo "Roses are $color"; 
echo "<br />"; 
echo 'Roses are $color'; 
?>

The output of the code above will be:
上述代码将输出下面的结果:
Roses are redRoses are $color

Example 5
案例5
Shortcut syntax:
简写(捷径)语法:

<html><body> 
<?php 
$color = "red"; 
?><p>Roses are <?=$color?></p></body></html>

PHP 相关文章推荐
我的论坛源代码(八)
Oct 09 PHP
用PHP和ACCESS写聊天室(七)
Oct 09 PHP
php+iframe实现隐藏无刷新上传文件
Feb 10 PHP
PHP生成Gif图片验证码
Oct 27 PHP
php按百分比生成缩略图的代码分享
May 10 PHP
ThinkPHP之M方法实例详解
Jun 20 PHP
php页面缓存方法小结
Jan 10 PHP
php+mysql删除指定编号员工信息的方法
Jan 14 PHP
php检查字符串中是否包含7位GSM字符的方法
Mar 17 PHP
php正则表达式验证(邮件地址、Url地址、电话号码、邮政编码)
Mar 14 PHP
PHP文件上传、客户端和服务器端加限制、抓取错误信息、完整步骤解析
Jan 12 PHP
PHP批量修改文件名称的方法分析
Feb 27 PHP
php 图片加水印与上传图片加水印php类
May 12 #PHP
php access 数据连接与读取保存编辑数据的实现代码
May 12 #PHP
简单PHP上传图片、删除图片实现代码
May 12 #PHP
php 删除记录同时删除图片文件的实现代码
May 12 #PHP
Godaddy空间Zend Optimizer升级方法
May 10 #PHP
AMFPHP php远程调用(RPC, Remote Procedure Call)工具 快速入门教程
May 10 #PHP
PHP chmod 函数与批量修改文件目录权限
May 10 #PHP
You might like
php str_replace的替换漏洞
2008/03/15 PHP
java微信开发之上传下载多媒体文件
2016/06/24 PHP
tp5递归 无限级分类详解
2019/10/18 PHP
Laravel 实现Eloquent模型分组查询并返回每个分组的数量 groupBy()
2019/10/23 PHP
将HTML格式的String转化为HTMLElement的实现方法
2014/08/07 Javascript
jQuery中removeProp()方法用法实例
2015/01/05 Javascript
ajax+jQuery实现级联显示地址的方法
2015/05/06 Javascript
html中鼠标滚轮事件onmousewheel的处理方法
2016/11/11 Javascript
js返回顶部实例分享
2016/12/21 Javascript
bootstrap 表单验证使用方法
2017/01/11 Javascript
Vue学习笔记进阶篇之过渡状态详解
2017/07/14 Javascript
js实现一个页面多个倒计时的3种方法
2019/02/25 Javascript
jQuery编写QQ简易聊天框
2020/08/27 jQuery
Vue项目中使用mock.js的完整步骤
2021/01/12 Vue.js
[01:29]2017 DOTA2国际邀请赛官方英雄手办展示
2017/03/18 DOTA
python实现猜数字游戏(无重复数字)示例分享
2014/03/29 Python
Python中在for循环中嵌套使用if和else语句的技巧
2016/06/20 Python
python实现大学人员管理系统
2019/10/25 Python
Python调用graphviz绘制结构化图形网络示例
2019/11/22 Python
python实现FTP文件传输的方法(服务器端和客户端)
2020/03/20 Python
python如何支持并发方法详解
2020/07/25 Python
HTML5教程之html 5 本地数据库(Web Sql Database)
2014/04/03 HTML / CSS
HTML5 Canvas+JS控制电脑或手机上的摄像头实例
2014/05/03 HTML / CSS
Tripadvisor新西兰:阅读评论,比较价格和酒店预订
2018/02/10 全球购物
Magee 1866官网:Donegal粗花呢外套和大衣专家
2019/11/01 全球购物
岗位职责定义及内容
2013/11/08 职场文书
公司周年庆典邀请函
2014/01/12 职场文书
学习委员自我鉴定
2014/01/13 职场文书
妈妈的账单教学反思
2014/02/06 职场文书
国庆宣传标语
2014/06/30 职场文书
2014年单位法制宣传日活动总结
2014/11/01 职场文书
党员证明模板
2015/06/19 职场文书
关于开学的感想
2015/08/10 职场文书
Python中第三方库Faker的使用详解
2022/04/02 Python
Android开发之WECHAT微信小程序路由跳转的两种形式
2022/04/12 Java/Android
JavaScript实现音乐播放器
2022/08/14 Javascript