Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP不返回任何内容(甚至不返回NULL)_Php_Function_Return_Boolean - Fatal编程技术网

PHP不返回任何内容(甚至不返回NULL)

PHP不返回任何内容(甚至不返回NULL),php,function,return,boolean,Php,Function,Return,Boolean,我现在正在写一个代码调用这个代码如下: $reduced = is_reduced($pid); set_sale($pid, $reduced); echo " - " . $reduced . " - "; 因此,这背后的逻辑应该是: 我调用函数is_reduced来知道,如果一个产品是reduced。通常,它应该在那里返回true或false。但我什么也没得到。您可以看到,我正在将带有echo的变量打印到控制台。但我什么也没看到。甚至不是空的 但是在控制台上我可以看到,它没有被减少,而是

我现在正在写一个代码调用这个代码如下:

$reduced = is_reduced($pid);
set_sale($pid, $reduced);
echo " - " . $reduced . " - ";
因此,这背后的逻辑应该是: 我调用函数is_reduced来知道,如果一个产品是reduced。通常,它应该在那里返回true或false。但我什么也没得到。您可以看到,我正在将带有echo的变量打印到控制台。但我什么也没看到。甚至不是空的

但是在控制台上我可以看到,它没有被减少,而是被打印出来。所以,它也应该返回一些信息。由于echo后面跟着return命令,因此函数中的代码如下所示

有关该功能的一些信息: 一页可以有多个价格。这就是我使用while循环的原因。如果只有一个价格,我在var aktueller_计数器上得到-1。所以,我跳进了第一个if部落。它正在检查是否有较低的价格,如果是,如果该价格不是0,则不低于-1

正如你所看到的,上面写着echo,然后是return。但是为什么我不能得到任何空值或布尔值呢

我会很高兴得到帮助的! 亲切的问候

代码如下:

function is_reduced($pid){
    $aktueller_counter = -1;
    while(have_rows('product_shops', $pid)): the_row();
        $aktueller_counter = $aktueller_counter + 1;
    endwhile;

    if($aktueller_counter == -1){
        $price = get_field("product_shops_0_price", $pid);
        $price_old = get_field("product_shops_0_price_old", $pid);

        if($price_old < $price) {
            if ($price_old != "") {
                if ($price_old != 0) {
                    if ($price_old > -1) {
                        $price = null;
                        $price_old = null;
                        $aktueller_counter = null;

                        echo ", is \"reduced\"";

                        return true;
                    } else {
                        $price = null;
                        $price_old = null;
                        $aktueller_counter = null;

                        echo ", is NOT \"reduced\"";

                        return false;
                    }
                } else {
                    $price = null;
                    $price_old = null;
                    $aktueller_counter = null;

                    echo ", is NOT \"reduced\"";

                    return false;
                }
            } else {
                $price = null;
                $price_old = null;
                $aktueller_counter = null;

                echo ", is NOT \"reduced\"";

                return false;
            }
        } else {
            $price = null;
            $price_old = null;
            $aktueller_counter = null;

            echo ", is NOT \"reduced\"";

            return false;
        }
    }else{
        for($i = 0; $i <= $aktueller_counter; $i++){
            $price = get_field("product_shops_".$i."_price", $pid);
            $price_old = get_field("product_shops_".$i."_price_old", $pid);

            if($price_old < $price){
                if($price_old != ""){
                    if($price_old != 0){
                        if($price_old > -1) {
                            $price = null;
                            $price_old = null;
                            $aktueller_counter = null;

                            echo ", is \"reduced\"";

                            return true;
                        }
                    }else {
                        $price_old = null;
                        $price = null;
                    }
                }else {
                    $price_old = null;
                    $price = null;
                }
            }else {
                $price_old = null;
                $price = null;
            }
        }


        $price = null;
        $price_old = null;
        $aktueller_counter = null;

        echo ", is NOT \"reduced\"";

        return false;
    }
}

Boolen值不获取echo,而可以将其显示为字符串。试试这个:

$reduced = is_reduced($pid);
set_sale($pid, $reduced);
$reduced_str = $reduced ? 'true' : 'false';
echo " - " . $reduced_str . " - ";

当在屏幕上显示布尔值时,您将比echo更容易使用var_导出。完全有可能您的代码在错误返回上正常工作,或者至少在其中一些错误返回上正常工作

考虑以下情况:

输出:

false
----

----
true
----
1
----
NULL
----

----
_行的目的是什么;有吗

您可以替换:$aktueller_计数器=$aktueller_计数器+1;使用++$aktueller_计数

不要重复:

$price = null;
$price_old = null;
$aktueller_counter = null;
最好在条件集之前将这些值声明为默认值,并仅在必要时覆盖它们

由于您没有返回$price、$price\u old或$aktueller\u计数器,因此没有理由声明它们。它们将仅存在于功能范围内。除非,您已经修改了本文的代码,并且实际上正在使用这些变量


此外,您的条件语句相当冗长/大量。经过仔细考虑,您应该能够减少条件语句的总量,并编写更简洁的代码。

当遇到这些问题时,我所做的就是在这里编写一个echo;行并在代码中从if移动到next if,几分钟内,您将看到代码的工作方式和问题。函数中的回声正常工作?我已经用未减少的代码做了。所以,它应该是返回的东西…你所说的写入是什么意思?请尝试我下面的答案,我将使用var_导出进行尝试:
$price = null;
$price_old = null;
$aktueller_counter = null;