还能这样定义变量啊?

WBOY
Release: 2016-06-06 20:13:09
Original
907 people have browsed it

但是看着好别扭啊


function show_str( $str ){
    print $str . "
"; } show_str( $my_str = "hello sorn" ); print $my_str; ?>
Copy after login
Copy after login

回复内容:

但是看着好别扭啊


function show_str( $str ){
    print $str . "
"; } show_str( $my_str = "hello sorn" ); print $my_str; ?>
Copy after login
Copy after login

你如果会C语言的话就不奇怪了。这是类似C语言里的赋值表达式。
$my_str = "hello sorn"
这是一个表达式,表达式的值是$my_str赋值后的值,顺便产生了赋值的副作用。

对了,php好多语法都神似C语言。。。

这里$my_str = 'hello sorn'其实是省略了一部操作,赋值的同时传参给函数,你可以理解为给予函数参数一个默认值,很多时候会用到这种办法,尤其是在声名一个函数(并需要传入参数有默认值)的时候

你是说print $str吗

php奇奇怪怪的语法很多 也不会报错 所以很多看起来不是很规范

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!