Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Why does it give an infinite loop(beginner question)?

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #44758
    ghafirsayed
    Member

    Hi, I m trying to get 4 sets of number which should be like this seperated in horizontal line
    1 2 3 4| 5 6 7 8 | 9 10 11 12| 13 14 15 16 and I wrote a for loop for it but why does if give me an infinite loop?

    //to get four sets
    for($i=1; $i<=4; $i++){ if($i=1){
    $n=1;
    }else{
    $n= 2*$i+1; //to increment the number
    }

    for($j=$n; $j<=$n+2; $j++){
    echo $j. ‘
    ‘ ;
    }
    echo ‘


    ‘; //this is hr tag but isn’t displaying on csstricks
    }
    ?>

    #134986
    ghafirsayed
    Member

    OMG, such a stupid mistake, thanks for points it out.

    #135110
    __
    Participant

    Not critical here, but on a related note, I always recommend using `===` for comparison except in situations where you *specifically want* type-juggling. Example:

    $i = 0;
    // == does type coercion. FALSE and 0 are equivalent…
    if( $i == false ){ /* true! */ }
    // …but NOT identical.
    if( $i === false ){ /* false! */ }

    // real-world example:
    $str = “ABCDEFG”;
    // you hate the letter “A”.
    // make sure $str DOES NOT HAVE the letter “A” in it!
    if( strpos( $str,”A” ) == false ){
    // this compares as TRUE, even though “A” is in the string!
    // this is because the *first* position in the string is position *0*.
    // 0 == false.
    }

    // same example, but using ===
    if( strpos( $str,”A” ) === false ){
    // this compares as FALSE, as expected.
    // 0 !== false.
    }

    #135115
    __
    Participant

    learned it from javascript, where `==` is arguably useless and completely counterproductive.

    #135123
    __
    Participant

    almost no one offers any explanation about how or why it’s different (especially for beginners); and it follows that there are even fewer examples in code or tutorials.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Back End’ is closed to new topics and replies.