Jump to content

If / Equality / String Fail


chaosxkitten

Recommended Posts

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) 

?>

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.