问题
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';
}
?>
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?
回答1:
Just change the order to this:
if('jan' == $name) {
This is known under Yoda conditions:
So if you now make the mistake:
if('jan' = $name) {
This will give you an error!
来源:https://stackoverflow.com/questions/28840376/forgetting-double-equal-sign-in-php-if-statement