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 相关文章推荐
PHP设计聊天室步步通
Oct 09 PHP
PHP获取163、gmail、126等邮箱联系人地址【已测试2009.10.10】
Oct 11 PHP
发款php蜘蛛统计插件只要有mysql就可用
Oct 12 PHP
用PHP实现Ftp用户的在线管理
Feb 16 PHP
ThinkPHP做文字水印时提示call an undefined function exif_imagetype()解决方法
Oct 30 PHP
php生成html文件方法总结
Dec 01 PHP
在Nginx上部署ThinkPHP项目教程
Feb 02 PHP
我整理的PHP 7.0主要新特性
Jan 07 PHP
PHP缩略图生成和图片水印制作
Jan 07 PHP
PHP实现限制IP访问及提交次数的方法详解
Jul 17 PHP
PHP编译configure时常见错误的总结
Aug 17 PHP
PHP单例模式实例分析【防继承,防克隆操作】
May 22 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
仿服务器端脚本方式的JS模板实现方法
2007/04/27 Javascript
Javascript 构造函数 实例分析
2008/11/26 Javascript
基于Jquery的文字自动截取(提供源代码)
2011/08/09 Javascript
扩展Jquery插件处理mouseover时内部有子元素时发生样式闪烁
2011/12/08 Javascript
js函数的延迟加载实现代码
2012/10/11 Javascript
浅谈jQuery页面的滚动位置scrollTop、scrollLeft
2015/05/19 Javascript
全面解析Bootstrap图片轮播效果
2015/12/03 Javascript
Svg.js实例教程及使用手册详解(一)
2016/05/16 Javascript
Jquery和JS获取ul中li标签的实现方法
2016/06/02 Javascript
jQuery ui autocomplete选择列表被Bootstrap模态窗遮挡的完美解决方法
2016/09/23 Javascript
JS/jQuery判断DOM节点是否存在的简单方法
2016/11/24 Javascript
Angular2 父子组件通信方式的示例
2018/01/29 Javascript
微信小程序实现导航栏选项卡效果
2020/06/19 Javascript
Angular 组件之间的交互的示例代码
2018/03/24 Javascript
小程序实现日历左右滑动效果
2019/10/21 Javascript
uni-app使用微信小程序云函数的步骤示例
2020/05/22 Javascript
js实现随机圆与矩形功能
2020/10/29 Javascript
小程序实现上下切换位置
2020/11/16 Javascript
[46:47]2014 DOTA2国际邀请赛中国区预选赛 DT VS HGT
2014/05/22 DOTA
python解决Fedora解压zip时中文乱码的方法
2016/09/18 Python
单利模式及python实现方式详解
2018/03/20 Python
Python 多线程其他属性以及继承Thread类详解
2019/08/28 Python
使用python动态生成波形曲线的实现
2019/12/04 Python
Django-rest-framework中过滤器的定制实例
2020/04/01 Python
完美解决pycharm 不显示代码提示问题
2020/06/02 Python
python实现凯撒密码、凯撒加解密算法
2020/06/11 Python
奥地利婴儿用品和玩具购物网站:baby-markt.at
2020/01/26 全球购物
农林环境专业求职信
2014/03/13 职场文书
老师对学生的评语
2014/04/18 职场文书
董事长年会致辞
2015/07/29 职场文书
应收账款管理制度
2015/08/06 职场文书
护士业务学习心得体会
2016/01/25 职场文书
使用CSS实现小三角边框原理解析
2021/11/07 HTML / CSS
各国货币符号大全
2022/02/17 杂记
Python实现提取PDF简历信息并存入Excel
2022/04/02 Python
Windows server 2022创建创建林、域树、子域的步骤
2022/06/25 Servers