Forgetting double equal-sign in php if-statement

我的未来我决定 提交于 2019-12-31 04:09:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!