三元运算符和比较运算符在PHP中如何工作?
我对Laravel框架(即一个由Symfony库驱动的框架)是新手。我在查看Symfony的代码时,遇到了一些捷径,我认为可以使用这些捷径来提高我的整洁编码能力 我想知道这些操作员的行为?(?:等) //这是否意味着创建历史类的对象并存储在$this->History中三元运算符和比较运算符在PHP中如何工作?,php,operators,Php,Operators,我对Laravel框架(即一个由Symfony库驱动的框架)是新手。我在查看Symfony的代码时,遇到了一些捷径,我认为可以使用这些捷径来提高我的整洁编码能力 我想知道这些操作员的行为?(?:等) //这是否意味着创建历史类的对象并存储在$this->History中 2- $this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects; 3- $this->followRedirects = -1 !
2- $this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects;
3- $this->followRedirects = -1 != $this->maxRedirects;
2-$this->maxrirects=$maxrirects<0-1:$maxRedirects;
3-$this->followRedirects=-1!=$这将重定向;
//不确定操作符的行为如何?我知道这与正则表达式有关,但我想知道逻辑
如果有人能像上面那样发布一个关于php中regex编程的教程链接,我将不胜感激。if-else速记格式如下:
<condition> ? <condition is met> : <condition is not met>
与此相同:
if($age >= 21){
echo 'Have a beer';
}else{
echo 'Too Young! No beer for you!';
}
您的示例#1,从PHP 5.3开始,只需省略速记的第一个条件,并且仅当该条件不满足时才执行new History()
:
$this->history = $history ?: new History();
注意,如果愿意,也可以省略第二个条件。此外,如果满足速记中省略的部分,则会返回
1
,因为没有给出其他指令。if-else速记遵循以下格式:
<condition> ? <condition is met> : <condition is not met>
与此相同:
if($age >= 21){
echo 'Have a beer';
}else{
echo 'Too Young! No beer for you!';
}
您的示例#1,从PHP 5.3开始,只需省略速记的第一个条件,并且仅当该条件不满足时才执行new History()
:
$this->history = $history ?: new History();
注意,如果愿意,也可以省略第二个条件。此外,如果满足速记中省略的部分,则返回
1
,因为没有给出其他指令。第一个和第二个使用三值运算符:
condition ? code_if_true : code_if_false
请注意,code_if_true或code_if_false可以为空
第三个将测试结果分配给变量:-1!=$此->最大重定向
所以,对还是错
1)
$this->history=$history?:新建历史()代码>
如果$history
的值等于false,则将history属性设置为类history的新实例。如果$history
的值等于true,则历史属性将设置为$history
值
2) $this->maxrirects=$maxrirects<0-1:$maxRedirects代码>
如果$maxrirects
为负值,则maxrirects属性设置为-1,否则设置为$maxrirects
3) $this->followRedirects=-1!=$这将重定向代码>
如果$this->maxRedirects
与-1
不同,$this->followRedirects
设置为true
,否则false
第一个和第二个使用三元运算符:
condition ? code_if_true : code_if_false
请注意,code_if_true或code_if_false可以为空
第三个将测试结果分配给变量:-1!=$此->最大重定向
所以,对还是错
1) $this->history=$history?:新建历史()代码>
如果$history
的值等于false,则将history属性设置为类history的新实例。如果$history
的值等于true,则历史属性将设置为$history
值
2) $this->maxrirects=$maxrirects<0-1:$maxRedirects代码>
如果$maxrirects
为负值,则maxrirects属性设置为-1,否则设置为$maxrirects
3) $this->followRedirects=-1!=$这将重定向代码>
如果$this->maxRedirects
与-1
不同,$this->followRedirects
设置为true
,否则false
1)和3)称为三值。即
echo $isFoo ? "Is foo" : "No foo for you!";
如果foo为真,则会回显“Is foo”,否则会回显“No foo for you!”
由于PHP 5.3,您可以省略中间部分:
echo $fooLabel ?: "Default foo label";
如果为true,则显示$傻瓜标签,否则显示“Default foo label”
最后(3)
这只是计算-1!=$此->最大重定向
。如果maxrirects
不等于-1,则为真。结果随后存储在$this->followRedirects
1)和3)中,称为三值。即
echo $isFoo ? "Is foo" : "No foo for you!";
如果foo为真,则会回显“Is foo”,否则会回显“No foo for you!”
由于PHP 5.3,您可以省略中间部分:
echo $fooLabel ?: "Default foo label";
如果为true,则显示$傻瓜标签,否则显示“Default foo label”
最后(3)
这只是计算-1!=$此->最大重定向
。如果maxrirects
不等于-1,则为真。然后将结果存储在$this->followRedirects
中。我认为最好通过示例说明这些运算符的工作原理:
1)
等于
if ($history) {
$this->history = $history;
} else {
$this->history = new History();
}
if ($maxRedirects < 0) {
$this->maxRedirects = -1;
} else {
$this->maxRedirects = $maxRedirects;
}
if (-1 != $this->maxRedirects) {
$this->followRedirects = true;
} else {
$this->followRedirects = false;
}
2)
等于
if ($history) {
$this->history = $history;
} else {
$this->history = new History();
}
if ($maxRedirects < 0) {
$this->maxRedirects = -1;
} else {
$this->maxRedirects = $maxRedirects;
}
if (-1 != $this->maxRedirects) {
$this->followRedirects = true;
} else {
$this->followRedirects = false;
}
等于
if ($history) {
$this->history = $history;
} else {
$this->history = new History();
}
if ($maxRedirects < 0) {
$this->maxRedirects = -1;
} else {
$this->maxRedirects = $maxRedirects;
}
if (-1 != $this->maxRedirects) {
$this->followRedirects = true;
} else {
$this->followRedirects = false;
}
我认为最好通过示例来说明这些操作符是如何工作的:
1)
等于
if ($history) {
$this->history = $history;
} else {
$this->history = new History();
}
if ($maxRedirects < 0) {
$this->maxRedirects = -1;
} else {
$this->maxRedirects = $maxRedirects;
}
if (-1 != $this->maxRedirects) {
$this->followRedirects = true;
} else {
$this->followRedirects = false;
}
2)
等于
if ($history) {
$this->history = $history;
} else {
$this->history = new History();
}
if ($maxRedirects < 0) {
$this->maxRedirects = -1;
} else {
$this->maxRedirects = $maxRedirects;
}
if (-1 != $this->maxRedirects) {
$this->followRedirects = true;
} else {
$this->followRedirects = false;
}
等于
if ($history) {
$this->history = $history;
} else {
$this->history = new History();
}
if ($maxRedirects < 0) {
$this->maxRedirects = -1;
} else {
$this->maxRedirects = $maxRedirects;
}
if (-1 != $this->maxRedirects) {
$this->followRedirects = true;
} else {
$this->followRedirects = false;
}
理解它的最好方法是只看一页:
我认为,要与Symfony合作,您需要有更丰富的开发经验。首先尝试学习PHP,然后您可以尝试学习如何使用用PHP编写的框架。我还认为,在开始学习框架之前,你应该阅读一些关于设计模式的书籍。理解它的最好方法是只看一页:
我认为,要与Symfony合作,您需要有更丰富的开发经验。首先尝试学习PHP,然后您可以尝试学习如何使用用PHP编写的框架。另外,我认为在开始学习框架之前,你应该阅读一些关于设计模式的书籍。但是这段代码($this->history=$history?:new history();)是由Symfony开发人员(Fabien Poffier)编写的,它是Symfony$this->history=$history?:new history()的一部分;