This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list.
It used to be hard to find questions about operators and other syntax tokens.¹
The main idea is to have links to existing questions on Stack Overflow, so it's easier for us to reference them, not to copy over content from …
什么是最好的,首选的写作方式,如速记单线,如:
expression ? $foo : $bar
Run Code Online (Sandbox Code Playgroud)
情节扭曲:我需要echo $foo
或echo $bar
.任何疯狂的伎俩?:)
例:
<?php $formElement->display()?>
Run Code Online (Sandbox Code Playgroud)
这样没事,或者我应该提供一个; ?好吧,我想PHP解释器非常聪明,可以看到该行已完成并且由于末尾的?>而完成了表达式.对?
这个符号在PHP中意味着<?=
什么?
用法示例:
<h2>Manage Role: > (<?= $myACL->getRoleNameFromID($_GET['roleID']); ?>)</h2>
Run Code Online (Sandbox Code Playgroud) 什么是这条线的含义
<input type=text name="name" value="<?= $name ?>
Run Code Online (Sandbox Code Playgroud)
如果我们要声明为PHP我们不应该写 <?php instead of <?=
谢谢
我有一个存储在变量中的php嵌套数组$myArray
,下面是var在将其转储到浏览器之后的样子(它不是一个完整的输出).
<?php var_dump($myArray); ?>
Run Code Online (Sandbox Code Playgroud)
输出:
array (size=4)
'id' => string '162' (length=3)
'content' => string 'Test content' (length=12)
'children' =>
array (size=16)
0 =>
array (size=4)
'id' => string '29208' (length=5)
'content' => string 'Test content 1' (length=14)
'children' =>
array (size=3)
...
1 =>
array (size=4)
'id' => string '29215' (length=5)
'content' => string 'Test content 2' (length=14)
'children' =>
array (size=1)
...
2 =>
array (size=3)
'id' => string '29220' (length=5)
'content' => string 'Test Content …
Run Code Online (Sandbox Code Playgroud) php ×7
symbols ×2
arguments ×1
echo ×1
if-statement ×1
javascript ×1
json ×1
operators ×1
shorthand ×1
tags ×1