php比较字符串下标

时间:2012-09-14 17:34:33

标签: php html

您好,我在PHP中有以下脚本:

$correct = array("a", "a", "a", "a");
$totalValue = "First Name: " . $fname . "\n";
$totalValue .= "About Our Mission | Question 01: " . $S1Q1 . "    " . (compare $correct[0] with $S1Q1 choice) ? "Correct" : "Wrong" . "\n";

$ S1Q1是我使用单选按钮从表单接受的变量:

    <input type="radio" name="S1Q1" value="a" /> True
    <input type="radio" name="S1Q1" value="b" /> False

(比较$ correct [0]和$ S1Q1选项)? “正确”:“错误”&lt;我如何在PHP代码中实现它。

对于下一个电台选择,我会将 $ correct [1]与$ S1Q2 进行比较......等等。

3 个答案:

答案 0 :(得分:2)

这将检查单选按钮中输入的值是否为=以正确答案

$S1Q1 = $_GET['S1Q1'];
($S1Q1 == $correct[0]) ? 'correct' : 'wrong'

答案 1 :(得分:1)

主要机制:

$S1Q1 = $_GET['S1Q1']; // or $_POST['S1Q1']
($correct[0] == $S1Q1 ) ? 'Correct' : 'Wrong'

使用循环:

if( isset($_POST) && !empty($_POST) ){
   foreach($_POST as $key => $val ) {
       $index = (int) str_replace('S1Q', '', $key ); // 1, 2, 3..
       $result = $val == $correct[ $index - 1 ] ? 'Correct' : 'Wrong';
   }
}

在代码中使用$result

答案 2 :(得分:1)

您可以使用$ _POST和/或$ _GET

访问var
 $S1Q1 = $_POST['S1Q1']
相关问题