只有if / elseif / else语句中的第一个elseif才有效

时间:2014-02-02 13:32:12

标签: php

只有第37行的elseif (isset($foo['read']))正常工作,if和第一个elseif正常工作,当我尝试调用第二个elseif时,它会返回第一个elseif 1}}语句输出。我甚至交换了两个elseif语句,它在调用第二个elseif语句时仍然只返回第一个elseif语句输出。

<?php

$nh = "true";
$foo = file_get_contents("php://input");
//$stuff = json_decode($foo, true);


function savetxt($sttext)
{
    $filename = 'test.txt';
    $somecontent = $sttext;

    if (is_writable($filename)) { 
        if (!$handle = fopen($filename, 'a')) {
             echo "Cannot open file ($filename)";
             exit;
        }
        if (fwrite($handle, $somecontent) === FALSE) {
            echo "Cannot write to file ($filename)";
            exit;
        }

        echo "Success, wrote ($somecontent) to file ($filename)";

        fclose($handle);

    }
    else {
        echo "The file $filename is not writable";
    }
}

if (strpos($foo, 'write:') !== false) {
    echo 'Attempting to write!';
    $stext = substr($foo, 6);
    savetxt($stext); 
} 
elseif ($foo == "read"){
    echo file_get_contents( "test.txt" );
} 
elseif ($foo == "test){
    echo 'Working!';
} 
else {
    echo 'Didn't get test.';
}
?>

1 个答案:

答案 0 :(得分:0)

试试这个(你必须用else替换else if)

else if ($foo && $foo=='read')

else if ($foo && $foo=='test')

并在结束时

else if (!$foo || $foo=='')