在php if-statement中忘记双等号 [英] Forgetting double equal-sign in php if-statement

查看:126
本文介绍了在php if-statement中忘记双等号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编码时,我有时会在if语句中写出单个=而不是==的错误。所以我可以这样说:

 <?php 
$ name ='piet';
if($ name =='jan'){
print'hello jan';
}

?>

我犯了一个错误,写下来:

 <?php 
$ name ='piet';
if($ name ='jan'){
print'hello jan';
}

?>

当然这不是一个错误,因为它是有效的PHP代码。但是,我从来没有使用这种短手符号,所以如果我错误地输入它,它会打破我的代码的逻辑,而不告诉我为什么。是否有解决方案?我使用的是基于eclipse的编辑器aptana。有什么办法可以根据例如正则表达式将自己的自定义错误添加到编辑器(或php)?还是有任何其他方法来提醒我,当我犯这个错误?

解决方案

只需将顺序更改为:

  if('jan'== $ name){

这是在 Yoda条件





所以如果你现在犯了这个错误:

  if('jan'= $ name){

这将给你一个错误! / p>

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?

解决方案

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!

这篇关于在php if-statement中忘记双等号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆