chaosxkitten Posted February 6, 2011 Share Posted February 6, 2011 I'm adding html tags with an "if" statement. $fw = "word" if ($fw == "a"){ echo "something"; }elseif ($fw == "b"){ echo "something else"; }else{ echo "this"; } So why would everything skip right to the "else" and echo that, even if $fw equals "a" or "b".... Quote Link to comment https://forums.phpfreaks.com/topic/226851-if-equality-string-fail/ Share on other sites More sharing options...
Pikachu2000 Posted February 6, 2011 Share Posted February 6, 2011 It shouldn't. I just pasted your code locally and ran it, and it works as intended. Post the actual code you're having problems with. Quote Link to comment https://forums.phpfreaks.com/topic/226851-if-equality-string-fail/#findComment-1170532 Share on other sites More sharing options...
chaosxkitten Posted February 6, 2011 Author Share Posted February 6, 2011 Here's all of it, maybe the error is somewhere else. I added a check that would print the $fw variable, and everything prints as it should, but my webpage is here and not working well http://easlnx01.eas.muohio.edu/~meadecm/project02.php <?php //Returns the first word of a paragraph function getFirstWord($p,$x) { $str = $p[$x]; $word = explode(" ",$str); return $word[0]; } // Opens the constitution text file, writes to a variable, explodes. $data = file_get_contents('constitution.txt'); $paragraphs = explode("&",$data); // Formats the Preamble. echo "<p><em>$paragraphs[0]</em></p>"; // Runs through some if statements to determine the tags to use on remander. $n = 1; do { $fw = getFirstWord($paragraphs,$n); if ($fw == 'Article'){ echo "<h2>$paragraphs[$n]</h2>"; } elseif ($fw == 'Section'){ echo "<h3>$paragraphs[$n]</h3>"; } elseif ($fw == 'Amendment'){ echo "<h3>$paragraphs[$n]</h3>"; } else{ echo "<p>$paragraphs[$n]</p>"; } $n += 1; echo "\$fw=$fw\n"; } while (count($paragraphs) >= $n) ?> Quote Link to comment https://forums.phpfreaks.com/topic/226851-if-equality-string-fail/#findComment-1170536 Share on other sites More sharing options...
Pikachu2000 Posted February 6, 2011 Share Posted February 6, 2011 Without having the text file, All I can really do is make an educated guess, but it may be that there's whitespace causing the comparison to evaluate to false. Try trimming the values before comparing them. if ( trim($fw) == 'Article' ) { // ETC. Quote Link to comment https://forums.phpfreaks.com/topic/226851-if-equality-string-fail/#findComment-1170539 Share on other sites More sharing options...
chaosxkitten Posted February 6, 2011 Author Share Posted February 6, 2011 ohhh that worked!! Quote Link to comment https://forums.phpfreaks.com/topic/226851-if-equality-string-fail/#findComment-1170541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.