2015-06-01 73 views
-1

我使用PHP來幫助創建網頁表,並且似乎無法找到此while循環崩潰瀏覽器的位置。使用while循環崩潰我的瀏覽器,PHP

<?php 
    $counter1 = 1; 


    $weekDay = date('l'); 
    echo "Today is $weekDay " . date("Y/m/d") . "<br/>"; 
    echo "<h1>Table Construction</h1>"; 
    echo "<table><tr>"; 
    $dateNow = date("Y/m/d"); 
    $dayStore = array(); 
    while ($counter1 < 8) { 
     $stringCheck = "+" . $counter1 . " day"; 
     $dateMod = strtotime($stringCheck, strtotime($dateNow)); 
     $weekDay = date ('D',$dateMod); 
     echo "<td id=\"bolder\">" . date ('l',$dateMod) . " " . date("Y/m/d", $dateMod). "</td>"; 
     $dayStore[$counter1]=$weekDay; 
     $counter1++; 
    } 
    echo "</tr>";//"<tr>"; 
    $counter1=1; 
    while ($counter1<8){ 
     echo "<td><form name=\"timeslots\">"; 
     $dayVar = $dayStore[$counter1]; 
     echo $dayVar. " "; 
     $counterHours = 0; 
     if ($dayVar == "Mon"||$dayVar == "Tue"||$dayVar == "Wed"||$dayVar == "Thu"||$dayVar == "Fri") { 
      $startTime = 9; 
      $counterHours = 0; 
      while ($counterHours<3) { 
       $timeString = $startTime . ":00 am"; 
       echo "<input type=\"radio\" name=\"$dayStore[$counter1]\" value=\"$startTime\">$timeString</input><br/>"; 
       $startTime = $startTime+1; 
       $counterHours++; 
      } 
      while (startTime<12 && $counterHours>=3) { 
       $timeString = ($startTime-12) . ":00 pm"; 
       echo "<input type=\"radio\" name=\"$dayStore[$counter1]\" value=\"$startTime\">$timeString</input>"; 
       $startTime = $startTime+1; 
       $counterHours++; 
      } 
     } 
     else if ($dayVar == "Sat") { 
      $startTime = 9; 
      $counterHours = 0; 
      while ($counterHours<3) { 
       $timeString = $startTime . ":00 am"; 
       echo "<input type=\"radio\" name=\"$dayStore[$counter1]\" value=\"$startTime\">$timeString</input>"; 
       $startTime = $startTime+1; 
       $counterHours++; 
      } 
      while ($counterHours<12 && $counterHours>=3) { 
       $timeString = ($startTime-12) . ":00 pm"; 
       echo "<input type=\"radio\" name=\"$dayStore[$counter1]\" value=\"$startTime\">$timeString</input>"; 
       $startTime = $startTime+1; 
       $counterHours++; 
      } 
     } 
     else if ($dayVar == "Sun") { 
      $startTime = 11; 
      $counterHours = 0; 
      while ($counterHours<3) { 
       $timeString = $startTime . ":00 am"; 
       echo "<input type=\"radio\" name=\"$dayStore[$counter1]\" value=\"$startTime\">$timeString</input>"; 
       $startTime = $startTime+1; 
       $counterHours++; 
      } 
      while ($counterHours<12 && $counterHours>=3) { 
       $timeString = ($startTime-12) . ":00 pm"; 
       echo "<input type=\"radio\" name=\"$dayStore[$counter1]\" value=\"$startTime\">$timeString</input>"; 
       $startTime = $startTime+1; 
       $counterHours++; 
      } 
     } 
     else { 
      echo "displaying a proper message"; 
     } 
     echo "</form></td>"; 
     $counter1++; 
    } 
    echo "</tr></table>"; 
    ?> 

當我在主頁面上傳到我的學校的服務器並運行它時,沒有任何加載和瀏覽器崩潰。隨着我選擇的變量範圍,我不認爲它應該做的是...

+3

你有什麼錯誤嗎? (1.你的常量'startTime'定義在哪裏?100塊錢,你沒有定義這個常量,你想用你的變量名稱) – Rizier123

+0

我得到錯誤說'undefined constant startTime'請定義它 – spiderman

+3

當看到包含「Crashing my browser」的標題的「Run code snippet」時,有沒有其他人感到不安? ;) –

回答

1

錯別字:

while (startTime<12 && $counterHours>=3) { 
      ^^^^^^^^^ missing $ 

未定義的常量,因此被評價爲0,使表達

while (0 < 12 && $counterHours>=3) { 
    while (true && true) { 
    while (true) { 

因此是一個無限循環。

如果您在啓用調試選項的情況下運行(例如display_errorserror_reporting),您會被告知有關未定義的常量。在開發/調試系統上,這些設置應該是從不

+1

之前的HTML和JS代碼片段我現在得到我的100美元:http://stackoverflow.com/questions/30583617/crashing-my-browser-with-while-loop- php#comment49237036_30583617 :)? – Rizier123

+0

也許值得一提的是,常數只是轉換成一個字符串與名稱本身,但由於它是在數字上下文它被轉換爲一個數字,這裏是0 – Rizier123

+0

哇,非常好的解釋,+1 – spiderman