• PHP loop error (unexpected = expected ;)
    6 replies, posted
I'm getting this error: Parse error: syntax error, unexpected '=', expecting ';' on line 27 Heres the code: [code] 25 words = explode(" ", $sentence); 26 27 for ($i=0; $i<=$sentenceLength; $i++) 28 { 29 wordLength = strlen($words[$i]); 30 $isdoibroi = false; 31 32 if ($wordLength <5) 33 { 34 isdoibroi = true; 35 echo "DOI"; 35 } 36 } [/code] Its driving me crazy and I just know its probably just a stupid mistake...
you need to put $ in front of all your variables, not just the ones you feel like
Oh jesus of derps... [editline]22nd February 2012[/editline] Weird, still getting the same error: [code] 27 for ($i=0; $i<=$sentenceLength; $i++) 28 { 29 $wordLength = strlen($words[$i]); 30 $isdoibroi = false; 31 32 if ($wordLength <5) 33 { 34 $isdoibroi = true; 35 echo "DOI"; 36 } 37 } [/code]
Did you remember the variable above your for statement? Also you should use foreach to iterate arrays in PHP, its easier to read and is sometimes faster. [PHP] foreach($words as $i => $word){ $wordLength = strlen($word); [...] } [/PHP] Optionally you can reference the variable, wich is pretty useful in many situations. But you can read all about that here: [URL]http://dk.php.net/manual/en/control-structures.foreach.php[/URL]
Yeah that variable is fine now but still getting that error.
Tried to move { to the same line as the for statement?
Turns out I accidentally uploaded two versions under different names... Sorry to waste time it was an error I fixed then didn't upload the correct version. Should this thread be closed?
Sorry, you need to Log In to post a reply to this thread.