PHP if statement and ob_flush

234

If they are same do nothing, if they are different do the bottom and make them same. Whole script is in a loop but the loop is only doing else. Like it is not checking for conditions. What am I doing wrong?

for ($int = 0; $int < 10, $int+1){
        $array = $getarray();
    foreach ($array as $array){
        $var1 = $array->id;
        $var2 = null;

        if ($var1 == $var2){
            echo ('skip');
        }else{
            echo '<br>'.$var1.'<br>';
            ob_flush();
            $var2 = $var1;
        }
}
}

Initially var2 is null and var1 gets string value. So it performs else and var2 gets the var1 string. Next Loop if var1 has not changed, it will equal var2. So it does nothing and skips

22

Answer

Solution:

I don't understand the logic of these loops but this is how you should do it:

for ($int = 0; $int < 10, $int++) {
    /*
     * I suppose getarray() is a function.
     * If you have a variable $getarray containing the name of another function
     * then you should change getarray() to $getarray()
     */
    foreach (getarray() as $array) {
        if (is_null($array->id)) {
            echo ('skip');
        } else {
            echo "<br />{$array->id}<br />";
            ob_flush();
        }
    }
}
109

Answer

Solution:

Your code does not work properly, as how many times your loop will execute, your $var2 will become null, so not a single time you will be reached at the point where your $var1 & $var2 values are same. So instead please try the following code.

for ($int = 0; $int = 10; $int+1){
    $array = $getarray();
    $var2 = null;
    foreach ($array as $array){
        $var1 = $array->id;

        if ($var1 == var2){
            echo ('skip');
        }else{
            echo '<br>'.$var1.'<br>';
            ob_flush();
            $var2 = $var1;
        }
    }
}

People are also looking for solutions to the problem: arrays - PHP - Certain GET Usage

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.