php整数没有响应条件

时间:2011-08-24 15:08:54

标签: php if-statement integer conditional

答案

/* Thanks everyone :)
my guess: the while loop is running through every line of HTML code that 
has a value (class="" type="" id="" etc. – many of them are blank), but I only 
need it to count "1" if it has run through the switch up to case 5, at which 
point it grabs the last bit of the weather for a particular day.  On case 5 is 
when I need to trigger $weatherIterations-- */
$i = 0;
$weatherIterations = 3;

$htmlWeather = "";
if (empty($htmlText)) {
$htmlWeather = "<p>Weather information is not currently available.<p>";
} else {

 $parser = new HtmlParser($htmlText);

while ( $parser->parse() && ($weatherIterations > 0) ) {
    if( trim($parser->iNodeValue) != '' ) { 
        // hey, no laughing at the newbie
            switch ($i) {
                case 0:
                    $htmlWeather .= "<div id='weather'><span class='weather-title'>".$parser->iNodeValue."</span><br />";
                    break;
                case 1:
                    $htmlWeather .= "<span class='weather-hi'>".$parser->iNodeValue."<br />";
                    break;
                case 2:
                    $htmlWeather .= $parser->iNodeValue."</span><br />";
                    break;
                case 3:
                    $htmlWeather .= "<span class='weather-lo'>".$parser->iNodeValue."<br />";
                    break;
                case 4:
                    $htmlWeather .= $parser->iNodeValue."</span><br />";
                    break;
                case 5:
                    $htmlWeather .= "<span class='weather-desc'>".$parser->iNodeValue."</span></div>";
                    $weatherIterations--;
                    break;
                default:
                    $htmlWeather = "Oops! Houston, we have a problem.";
                    print $htmlWeather;
                    exit;
                } // switch
            $i<5 ? $i++: $i=0;
        } // if trim
} // while
print $htmlWeather;
} // if empty html

令我感到困惑。 while()循环中没有条件执行。如果我将$weatherIterations > 0移出while循环,并将其置于自己的条件中,则该变量为2,1,0,-1,-2 ...- 150并且不是由条件if($weatherIterations<=0){}注册为整数。请指教。

更新

如果我从while循环中删除“&amp;&amp;($ weatherIterations&gt; 0)”,并在减少它之前回显$ weatherIterations,则会导致

3
2
1
0
-1
...
-150
title= Wednesday
Hi= Hi
Temp= 29 / 85 
Lo= Lo
Temp= 24 / 76 
Desc= Sunny Periods, showers
title= Thursday
 ...
 Desc= Sunny Periods

因此它在执行任何IF()条件之前会产生一堆while循环。想法?

问题

$i = 0;
$weatherIterations = 3;
$htmlWeather = "";

   ...

while ( $parser->parse() && ($weatherIterations > 0) ) {
$weatherIterations--;
    if ($parser->iNodeType == NODE_TYPE_TEXT || $parser->iNodeType == NODE_TYPE_COMMENT) {
    if( trim($parser->iNodeValue) != '' ) { 
        // hey, no laughing at the newbie
            switch ($i) {
                case 0:
                    $htmlWeather .= "<div id='weather'><span class='weather-title'>title= ".$parser->iNodeValue."</span><br>";
                    break;
                case 1:
                    $htmlWeather .= "<span class='weather-hi'>Hi= ".$parser->iNodeValue."<br>";
                    break;
                case 2:
                    $htmlWeather .= "Temp= ".$parser->iNodeValue."</span><br>";
                    break;
                case 3:
                    $htmlWeather .= "<span class='weather-lo'>Lo= ".$parser->iNodeValue."<br>";
                    break;
                case 4:
                    $htmlWeather .= "Temp= ".$parser->iNodeValue."</span><br>";
                    break;
                case 5:
                    $htmlWeather .= "<span class='weather-desc'>Desc= ".$parser->iNodeValue."</span></div>";
                    break;
                } // switch
            $i<5 ? $i++: $i=0;

        } // if trim
        } // if parser
} // while
print $htmlWeather;

0 个答案:

没有答案