忘记php if语句中的双等号 [英] Forgetting double equal-sign in php if-statement
问题描述
在编码时,我有时会犯在 if 语句中编写单个 = 而不是 == 的错误.所以可以说我有这个:
While coding, i sometimes make the error of writing a single = instead of == in an if-statement. So lets say i have this:
<?php
$name = 'piet';
if($name == 'jan'){
print 'hello jan';
}
?>
我犯了一个错误,改写了这个:
And i make a mistake and write this instead:
<?php
$name = 'piet';
if($name = 'jan'){
print 'hello jan';
}
?>
这当然不会抛出错误,因为它是有效的 php 代码.但是,我从不使用这种简写符号,所以如果我错误地输入它,它会破坏我的代码逻辑而不告诉我原因.有解决方案吗?我使用的是基于 eclipse 的编辑器 aptana.有什么方法可以根据例如正则表达式将我自己的自定义错误添加到编辑器(或 php)中?或者有没有其他方法可以在我犯这个错误时提醒我?
This won't throw an error of course, since it is valid php code. However, i never use this short-hand notation, so if i enter it by mistake it will break the logic of my code without telling me why. Is there a solution for this? I am using aptana which is an editor based on eclipse. Is there any way i can add my own custom errors to an editor (or php) based on for example regular expressions? Or are there any other approaches to alert me whenever i make this mistake?
推荐答案
把顺序改成这样:
if('jan' == $name) {
这是已知的 Yoda conditions
:
This is known under Yoda conditions
:
所以如果你现在犯了错误:
So if you now make the mistake:
if('jan' = $name) {
这会给你一个错误!
这篇关于忘记php if语句中的双等号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!