简体   繁体   English

很简单,但是似乎对if语句有麻烦

[英]simple it seems yet having trouble with an if statement

I have this class of code and one piece of code just stops the whole thing from working and i am unable to figure out why. 我有此类代码,而一段代码只是阻止了整个工作,而我无法弄清原因。

Scenario: The method will send a json as a reply when called via ajax, the code works fine except for the if condition. 场景:通过ajax调用时,该方法将发送json作为答复,除if条件外,代码工作正常。

  if(false) // this stops the script { $menu[] = new jsTreeNode('node','nodeid'); } $menu[] = new jsTreeNode('node'); 

Actual if condition is a comparision of two values and not just false, if i put true or when the actual condition is true it works fine but false just stops the whole script. 如果condition是两个值的比较而不是false,则为实际,如果我输入true或当实际条件为true时,它可以正常工作,但false会停止整个脚本。 Any ideas?? 有任何想法吗??

Edit: 编辑:

public function __construct($title, $nodeID = NULL)
{
    $this->data = new stdClass();
    $this->data->title = $title;
    $this->data->icon = 'assets/img/icons/'.strtolower($title).'.png';
    $this->attr = new stdClass();
    $this->attr->id = ($nodeID == NULL) ? strtolower($title) : strtolower($nodeID);

}

public function addChild($title, $nodeID = NULL)
{
    $this->children[] = new jsTreeNode($title, $nodeID);    
}

error_reporting throws no error. error_reporting不会引发任何错误。

Edit: after looking at your code you should always use the build in function of php to test values like null. 编辑:查看代码后,您应该始终使用php的内置函数来测试诸如null的值。

Try this: 尝试这个:

$menu[] = (is_null($condition))? new jsTreeNode('node','nodeid') : new jsTreeNode('node');

This is from the php manual: http://php.net/manual/en/control-structures.if.php 这来自php手册: http : //php.net/manual/zh/control-structures.if.php

If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it. 如果expression的计算结果为TRUE,PHP将执行语句,如果它的计算结果为FALSE,它将忽略它。

Your expression by design evaluates to false and so PHP ignores it. 根据设计,您的表达式的计算结果为false,因此PHP会将其忽略。

My guess is that using something like if(!true) would work better because if the condition you are giving the if statement is false, then the if statement would evaluate it as true. 我的猜测是,使用类似if(!true)会更好,因为如果给定if语句的条件为false,则if语句会将其评估为true。

Thanks to everyone who tried to help me out but i figured the problem out it seems there is an addchild call going to the $menu[] of agent node which was causing the bug. 感谢所有尝试帮助我的人,但是我发现了问题,看来似乎有一个addchild调用转到了代理节点的$ menu []上,从而导致了该错误。 So when it was false the if condition worked fine but due to the missing agent node the script just stopped loading. 因此,当它为false时,if条件可以正常工作,但是由于缺少代理节点,该脚本刚刚停止加载。 Since it was an ajax call i only figured it out now. 由于这是一个ajax调用,所以我现在才知道。 Thanks all the same to everyone! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM