Back to index

php5  5.3.10
023-1.inc
Go to the documentation of this file.
00001 <html>
00002 <head>
00003 <?php
00004 /* the point of this file is to intensively test various aspects of
00005  * the parser.  right now, each test focuses in one aspect only
00006  * (e.g. variable aliasing, arithemtic operator, various control
00007  * structures), while trying to combine code from other parts of the
00008  * parser as well.
00009  */
00010 ?>
00011 
00012 *** Testing assignments and variable aliasing: ***<br>
00013 <?php 
00014   /* This test tests assignments to variables using other variables as variable-names */
00015   $a = "b"; 
00016   $$a = "test"; 
00017   $$$a = "blah"; 
00018   ${$$$a}["associative arrays work too"] = "this is nifty";
00019 ?>
00020 This should read "blah": <?php echo "$test<br>\n"; ?>
00021 This should read "this is nifty": <?php echo $blah[$test="associative arrays work too"]."<br>\n"; ?>
00022 *************************************************<br>
00023 
00024 *** Testing integer operators ***<br>
00025 <?php 
00026   /* test just about any operator possible on $i and $j (ints) */
00027   $i = 5;
00028   $j = 3;
00029 ?>
00030 Correct result - 8:  <?php echo $i+$j; ?><br>
00031 Correct result - 8:  <?php echo $i+$j; ?><br>
00032 Correct result - 2:  <?php echo $i-$j; ?><br>
00033 Correct result - -2:  <?php echo $j-$i; ?><br>
00034 Correct result - 15:  <?php echo $i*$j; ?><br>
00035 Correct result - 15:  <?php echo $j*$i; ?><br>
00036 Correct result - 2:  <?php echo $i%$j; ?><br>
00037 Correct result - 3:  <?php echo $j%$i; ?><br>
00038 *********************************<br>
00039 
00040 *** Testing real operators ***<br>
00041 <?php 
00042   /* test just about any operator possible on $i and $j (floats) */
00043   $i = 5.0;
00044   $j = 3.0;
00045 ?>
00046 Correct result - 8:  <?php echo $i+$j; ?><br>
00047 Correct result - 8:  <?php echo $i+$j; ?><br>
00048 Correct result - 2:  <?php echo $i-$j; ?><br>
00049 Correct result - -2:  <?php echo $j-$i; ?><br>
00050 Correct result - 15:  <?php echo $i*$j; ?><br>
00051 Correct result - 15:  <?php echo $j*$i; ?><br>
00052 Correct result - 2:  <?php echo $i%$j; ?><br>
00053 Correct result - 3:  <?php echo $j%$i; ?><br>
00054 *********************************<br>
00055 
00056 *** Testing if/elseif/else control ***<br>
00057 
00058 <?php 
00059 /* sick if/elseif/else test by Andi :) */
00060 $a = 5;
00061 if ($a == "4") {
00062        echo "This "." does "."  not "." work<br>\n";
00063 } elseif ($a == "5") {
00064        echo "This "." works<br>\n";
00065        $a = 6;
00066        if ("andi" == ($test = "andi")) {
00067               echo "this_still_works<br>\n";
00068        } elseif (1) {
00069               echo "should_not_print<br>\n";
00070        } else {
00071               echo "should_not_print<br>\n";
00072        }
00073         if (44 == 43) {
00074               echo "should_not_print<br>\n";
00075        } else {
00076               echo "should_print<br>\n";
00077        }
00078 } elseif ($a == 6) {
00079        echo "this "."broken<br>\n";
00080        if (0) {
00081               echo "this_should_not_print<br>\n";
00082        } else {
00083               echo "TestingDanglingElse_This_Should_not_print<br>\n";
00084        }
00085 } else {
00086        echo "This "."does "." not"." work<br>\n";
00087 }
00088 ?>
00089 
00090 
00091 *** Seriously nested if's test ***<br>
00092 ** spelling correction by kluzz **
00093 <?php 
00094 /* yet another sick if/elseif/else test by Zeev */
00095 $i=$j=0;
00096 echo "Only two lines of text should follow:<br>\n";
00097 if (0) { /* this code is not supposed to be executed */
00098   echo "hmm, this shouldn't be displayed #1<br>\n";
00099   $j++;
00100   if (1) {
00101     $i 
00102 +=
00103  $j;
00104     if (0) {
00105       $j = ++$i;
00106       if (1) {
00107         $j *= $i;
00108         echo "damn, this shouldn't be displayed<br>\n";
00109       } else {
00110         $j /= $i;
00111         ++$j;
00112         echo "this shouldn't be displayed either<br>\n";
00113       }
00114     } elseif (1) {
00115       $i++; $j++;
00116       echo "this isn't supposed to be displayed<br>\n";
00117     }
00118   } elseif (0) {
00119     $i++;
00120     echo "this definitely shouldn't be displayed<br>\n";
00121   } else {
00122     --$j;
00123     echo "and this too shouldn't be displayed<br>\n";
00124     while ($j>0) {
00125       $j--;
00126     }
00127   }
00128 } elseif (2-2) {  /* as long as 2-2==0, this isn't supposed to be executed either */
00129   $i = ++$j;
00130   echo "hmm, this shouldn't be displayed #2<br>\n";
00131   if (1) { 
00132     $j = ++$i;
00133     if (0) {
00134       $j = $i*2+$j*($i++);
00135       if (1) {
00136         $i++;
00137         echo "damn, this shouldn't be displayed<br>\n";
00138       } else {
00139         $j++;
00140         echo "this shouldn't be displayed either<br>\n";
00141       }
00142     } else if (1) {
00143       ++$j;
00144       echo "this isn't supposed to be displayed<br>\n";
00145     }
00146   } elseif (0) {
00147     $j++;
00148     echo "this definitely shouldn't be displayed<br>\n";
00149   } else {
00150     $i++;
00151     echo "and this too shouldn't be displayed<br>\n";
00152   }
00153 } else {
00154   $j=$i++;  /* this should set $i to 1, but shouldn't change $j (it's assigned $i's previous values, zero) */
00155   echo "this should be displayed. should be:  \$i=1, \$j=0.  is:  \$i=$i, \$j=$j<br>\n";
00156   if (1) {
00157     $j += ++$i;  /* ++$i --> $i==2,  $j += 2 --> $j==2 */
00158     if (0) {
00159       $j += 40;
00160       if (1) {
00161         $i += 50;
00162         echo "damn, this shouldn't be displayed<br>\n";
00163       } else {
00164         $j += 20;
00165         echo "this shouldn't be displayed either<br>\n";
00166       }
00167     } else if (1) {
00168       $j *= $i;  /* $j *= 2  --> $j == 4 */
00169       echo "this is supposed to be displayed. should be:  \$i=2, \$j=4.  is:  \$i=$i, \$j=$j<br>\n";
00170       echo "3 loop iterations should follow:<br>\n";
00171       while ($i<=$j) {
00172         echo $i++." $j<br>\n";
00173       }
00174     }
00175   } elseif (0) {
00176     echo "this definitely shouldn't be displayed<br>\n";
00177   } else {
00178     echo "and this too shouldn't be displayed<br>\n";
00179   }
00180   echo "**********************************<br>\n";
00181 }
00182 ?>
00183 
00184 *** C-style else-if's ***<br>
00185 <?php 
00186   /* looks like without we even tried, C-style else-if structure works fine! */
00187   if ($a=0) {
00188     echo "This shouldn't be displayed<br>\n";
00189   } else if ($a++) {
00190     echo "This shouldn't be displayed either<br>\n";
00191   } else if (--$a) {
00192     echo "No, this neither<br>\n";
00193   } else if (++$a) {
00194     echo "This should be displayed<br>\n";
00195   } else {
00196     echo "This shouldn't be displayed at all<br>\n";
00197   }
00198 ?>
00199 *************************<br>
00200 
00201 *** WHILE tests ***<br>
00202 <?php 
00203 $i=0;
00204 $j=20;
00205 while ($i<(2*$j)) {
00206   if ($i>$j) {
00207     echo "$i is greater than $j<br>\n";
00208   } else if ($i==$j) {
00209     echo "$i equals $j<br>\n";
00210   } else {
00211     echo "$i is smaller than $j<br>\n";
00212   }
00213   $i++;
00214 }
00215 ?>
00216 *******************<br>
00217 
00218 
00219 *** Nested WHILEs ***<br>
00220 <?php 
00221 $arr_len=3;
00222 
00223 $i=0;
00224 while ($i<$arr_len) {
00225   $j=0;
00226   while ($j<$arr_len) {
00227     $k=0;
00228     while ($k<$arr_len) {
00229       ${"test$i$j"}[$k] = $i+$j+$k;
00230       $k++;
00231     }
00232     $j++;
00233   }
00234   $i++;
00235 }
00236 
00237 echo "Each array variable should be equal to the sum of its indices:<br>\n";
00238 
00239 $i=0;
00240 while ($i<$arr_len) {
00241   $j=0;
00242   while ($j<$arr_len) {
00243     $k=0;
00244     while ($k<$arr_len) {
00245       echo "\${test$i$j}[$k] = ".${"test$i$j"}[$k]."<br>\n";
00246       $k++;
00247     }
00248     $j++;
00249   }
00250   $i++;
00251 }
00252 ?>
00253 *********************<br>
00254 
00255 *** hash test... ***<br>
00256 <?php 
00257 /*
00258 $i=0;
00259 
00260 while ($i<10000) {
00261   $arr[$i]=$i;
00262   $i++;
00263 }
00264 
00265 $i=0;
00266 while ($i<10000) {
00267   echo $arr[$i++]."<br>\n";
00268 }
00269 */
00270 echo "commented out...";
00271 ?>
00272 
00273 **************************<br>
00274 
00275 *** Hash resizing test ***<br>
00276 <?php 
00277 $i = 10;
00278 $a = 'b';
00279 while ($i > 0) {
00280        $a = $a . 'a';
00281        echo "$a<br>\n";
00282        $resize[$a] = $i;
00283        $i--;
00284 }
00285 $i = 10;
00286 $a = 'b';
00287 while ($i > 0) {
00288        $a = $a . 'a';
00289        echo "$a<br>\n";
00290        echo $resize[$a]."<br>\n";
00291        $i--;
00292 }
00293 ?>
00294 **************************<br>
00295 
00296 
00297 *** break/continue test ***<br>
00298 <?php 
00299 $i=0;
00300 
00301 echo "\$i should go from 0 to 2<br>\n";
00302 while ($i<5) {
00303   if ($i>2) {
00304     break;
00305   }
00306   $j=0;
00307   echo "\$j should go from 3 to 4, and \$q should go from 3 to 4<br>\n";
00308   while ($j<5) {
00309     if ($j<=2) {
00310       $j++;
00311       continue;
00312     }
00313     echo "  \$j=$j<br>\n";
00314     for ($q=0; $q<=10; $q++) {
00315       if ($q<3) {
00316         continue;
00317       }
00318       if ($q>4) {
00319         break;
00320       }
00321       echo "    \$q=$q<br>\n";
00322     }
00323     $j++;
00324   }
00325   $j=0;
00326   echo "\$j should go from 0 to 2<br>\n";
00327   while ($j<5) {
00328     if ($j>2) {
00329       $k=0;
00330       echo "\$k should go from 0 to 2<br>\n";
00331       while ($k<5) {
00332         if ($k>2) {
00333           break 2;
00334         }
00335         echo "    \$k=$k<br>\n";
00336         $k++;
00337       }
00338     }
00339     echo "  \$j=$j<br>\n";
00340     $j++;
00341   }
00342   echo "\$i=$i<br>\n";
00343   $i++;
00344 }
00345 ?>
00346 ***********************<br>
00347 
00348 *** Nested file include test ***<br>
00349 <?php include("023-2.inc"); ?>
00350 ********************************<br>
00351 
00352 <?php 
00353 {
00354   echo "Tests completed.<br>\n";  # testing some PHP style comment...
00355 }
00356 ?>