Alice
Alice

Reputation: 424

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>";
}
}

Upvotes: 1

Views: 94

Answers (5)

Kichu
Kichu

Reputation: 3267

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).

Upvotes: 0

slartibartfast
slartibartfast

Reputation: 4428

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

Upvotes: 1

joshuahealy
joshuahealy

Reputation: 3569

Change

if($i = $derp){

to

if($i == $derp){

As you are currently assigning it, not comparing.

Upvotes: 6

Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

if($i = $derp) should be if($i == $derp)

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

= is assignment. == is comparison.

Upvotes: 3

Related Questions