Discuz教程网

PHP echo 输出字符串函数详解

[复制链接]
authicon dly 发表于 2011-9-5 16:37:09 | 显示全部楼层 |阅读模式
代码如下:

  1. echo "asd";//字符串
  2. echo "ads$c";//字符串+变量
  3. echo 'ads$c';//字符串 asd$c $c不是变量
  4. echo "sd"."vs";
  5. echo "sd","vs";
  6. echo $a;
  7. echo $a.$b;
  8. echo $a,$b;
  9. echo $a.$b.$c;
  10. echo $a,$b,$c;
  11. echo "kaskd{$c}asd";
  12. echo "kakskd{$arr['lo']}";
  13. echo "kakskd{$obj->a}";
  14. echo "kaskd".$c."kasd";
  15. echo "kaskd".$arr['lo']."kasd";
  16. echo "kaskd".$obj->a."kasd";
  17. echo "kaskd".func($c)."kasd";
  18. echo "kaksk".($a+1)."dkkasd";
  19. echo $c."jaksd";
  20. echo $c,"jaksd";
  21. //php多行输出方法
  22. echo <<<END
  23. This uses the "here document" syntax to output
  24. END;
  25. //输出简写
  26. <?php echo $a;?>   <?=$a?>
复制代码





代码如下:

  1. <?php
  2. echo "Hello World";

  3. echo "This spans
  4. multiple lines. The newlines will be
  5. output as well";

  6. echo "This spans\nmultiple lines. The newlines will be\noutput as well.";

  7. echo "Escaping characters is done "Like this".";

  8. // You can use variables inside of an echo statement
  9. $foo = "foobar";
  10. $bar = "barbaz";

  11. echo "foo is $foo"; // foo is foobar

  12. // You can also use arrays
  13. $baz = array("value" => "foo");

  14. echo "this is {$baz['value']} !"; // this is foo !

  15. // Using single quotes will print the variable name, not the value
  16. echo 'foo is $foo'; // foo is $foo

  17. // If you are not using any other characters, you can just echo variables
  18. echo $foo; // foobar
  19. echo $foo,$bar; // foobarbarbaz

  20. // Some people prefer passing multiple parameters to echo over concatenation.
  21. echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);
  22. echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";

  23. echo <<<END
  24. This uses the "here document" syntax to output
  25. multiple lines with $variable interpolation. Note
  26. that the here document terminator must appear on a
  27. line with just a semicolon. no extra whitespace!
  28. END;

  29. // Because echo does not behave like a function, the following code is invalid.
  30. ($some_var) ? echo 'true' : echo 'false';

  31. // However, the following examples will work:
  32. ($some_var) ? print 'true' : print 'false'; // print is also a construct, but
  33. // it behaves like a function, so
  34. // it may be used in this context.
  35. echo $some_var ? 'true': 'false'; // changing the statement around
  36. ?>
复制代码




以下是官方手册说明:
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
代码如下:

  1. <?php
  2. $str = "Who's Kai Jim?";
  3. echo $str;
  4. echo "<br />";
  5. echo $str."<br />I don't know!";
  6. ?>
复制代码




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

Example 2
案例2
代码如下:

  1. <?php
  2. echo "This textspans multiplelines.";
  3. ?>
复制代码




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

Example 3
案例3
代码如下:

  1. <?php
  2. echo 'This ','string ','was ','made ','with multiple parameters';
  3. ?>
复制代码




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:
区别单引号(')和双引号(”)的不同。单引号将输出变量名,而不是变量的值:
代码如下:

  1. <?php
  2. $color = "red";
  3. echo "Roses are $color";
  4. echo "<br />";
  5. echo 'Roses are $color';
  6. ?>
复制代码




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

Example 5
案例5
Shortcut syntax:
简写(捷径)语法:
代码如下:

  1. <html><body>
  2. <?php
  3. $color = "red";
  4. ?><p>Roses are <?=$color?></p></body></html>
复制代码








上一篇:论坛无法安装插件了,有人能来帮忙看下吗?
下一篇:PHP 函数使用方法与函数定义方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 07:28

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表