欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php echo 输出字符串函数详解

程序员文章站 2023-04-07 16:06:36
复制代码 代码如下:echo "asd";//字符串 echo "ads$c";//字符串+变量 echo 'ads$c';//字符串 asd$c $c不是变量 echo...
复制代码 代码如下:

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>