Grow your CSS skills. Land your dream job.

Why does it give an infinite loop(beginner question)?

  • # May 14, 2013 at 2:28 am

    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?

    < ?php
    //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
    }
    ?>

    # May 14, 2013 at 3:22 am

    `$i=1` is not comparison, you want `$i == 1`

    # May 14, 2013 at 3:29 am

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

    # May 14, 2013 at 10:46 pm

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

    < ?php
    $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.
    }

    # May 14, 2013 at 11:33 pm

    If I could “like” your post, I would. Good example to explain things further.

    # May 15, 2013 at 12:47 am

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

    # May 15, 2013 at 1:54 am

    I’m not sure why I’m not more in the habit of using `===` in PHP (outside of actual true/false statements).

    # May 15, 2013 at 2:48 am

    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 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".