简体   繁体   中英

PHP returns nothing (not even NULL)

I am writting a code at the moment at call this code like this:

$reduced = is_reduced($pid);
set_sale($pid, $reduced);
echo " - " . $reduced . " - ";

So, the logic behind that should be like: I call the function is_reduced() to know, if a product is reduced. Normally, it should return true or false there. But I don't get anything back. You can see, I'm printing the variable with echo to the console. But I don't see anything. Not even NULL!

But on the console I can see, that it gets "is NOT reduced" gets printed out. So, it should also return someting. Because the echo is followed by the return command in the function is_reduced (Code below).

Some informations to the function: One page can have multiple prices. This is the reason, why I have the while loop. If there is only one price, I get -1 back on the var aktueller_counter . So, I jump into the first if tribe. There it is checking if there is a lower price and if yes, if this price is not 0, not "" and not lower then -1.

And as you can see, it says echo followed by return . But why do I not get any NULL or any boolean value back?

I would be very happy about help! Kind regards!

Here the code:

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;
    }
}

When displaying boolean values to screen, you will have an easier time with, say, var_export() than echo . It is entirely possible that your code is working properly on false returns (or at least some of them).

Consider these occurrences: ( Demo )

var_export(false);
    echo "\n----\n";
echo false;
    echo "\n----\n";
var_export(true);
    echo "\n----\n";
echo true;
    echo "\n----\n";
var_export(null);
    echo "\n----\n";
echo null;
    echo "\n----\n";

Output:

false
----

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

----

What purpose does the_row(); have?

You can replace: $aktueller_counter = $aktueller_counter + 1; with ++$aktueller_count;

Instead of repeating:

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

it would be DRYer to declare these values as defaults before the set of conditionals, and only overwrite them where necessary.

Since you are not returning $price , $price_old , or $aktueller_counter there is no reason to declare them. They will exist only within the scope of the function. (Unless, you have modified your code for this post and you are actually using these variables.)

Also, your conditional statements are rather verbose/numerous. With some careful consideration, you should be able to cut down on total conditional statements and write more concise code.

Boolen Value dont get echo instead of it you can display it as string. Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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