- This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
Viewing 5 posts - 1 through 5 (of 5 total)
- The forum ‘Back End’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
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
}
?>
OMG, such a stupid mistake, thanks for points it out.
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.
}
learned it from javascript, where `==` is arguably useless and completely counterproductive.
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.