Skip to content
Advertisement

FOR and WHile loop is working in laravel controller

Strangely FOR and While loop not working in my controller function.

function information(Request $request){
    $exp=0;
    while($exp=0){
        echo 'While loop<br>';
        $exp++;
    }
    for($exp=0; $exp > 5; $exp++){
        echo 'For loop<br>';
    } 
}

Everything seems fine in loop. Not getting what causing it.

Advertisement

Answer

Use below code

    function information(Request $request){
        $exp=0;
        // = to ==
        while($exp==0){
            echo 'While loop<br>';
            $exp++;
        }

        // change $exp greater than 5 then it works
        for($exp=10; $exp > 5; $exp--){
            echo 'For loop<br>';
        } 
    }

https://www.php.net/manual/en/language.operators.assignment.php

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement