简体   繁体   中英

What's wrong with my PHP for loop?

What's wrong with my PHP loop? It just loops until it eventually times out.

$max = 7;
$derp = $a / 5;
for($i = 1; $i < $max; $i++){
if($i = $derp){
echo"<option value='$derp' selected='selected'>$derp</option>";
}else{
echo"<option value='$i'>$i</option>";
}
}

Change

if($i = $derp){

to

if($i == $derp){

As you are currently assigning it, not comparing.

= is assignment. == is comparison.

if($i = $derp)应该是if($i == $derp)

= assigns a value to a variable. == compares for equality.

Try this:

$max = 7;
$derp = $a / 5;
for($i = 1; $i < $max; $i++){
if($i == $derp){
echo"<option value='$derp' selected='selected'>$derp</option>";
}else{
echo"<option value='$i'>$i</option>";
}
}

In your code you are trying to assign a $derp to $i .If you want to compare it change it as if($i == $derp) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM