there is unexpected echo on $_SESSION['correctAnswer'] == echo $jawaban[$i]; ? " correct" : " incorrect";
$_SESSION['correctAnswer'] == echo $jawaban[$i]; ? " correct" : " incorrect";
line. please help
this is my code:
<?php
session_start();
$soal = $_SESSION['soal'];
$no = $_SESSION['no'];
if(isset($_POST['next'])){
$_SESSION['jawab'][] = $_POST['option'];
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['correctAnswer'] = $soal[$no-2]['kunci']; // add sthing like this
}
}
if(isset($soal[$no-1])){
?>
<!DOCTYPE html>
<html>
<head>
<title>Latihan Soal</title>
</head>
<body>
<a href="index.php">Kembali ke soal 1</a>
<form action="" method="POST">
<p>
<?php
echo $no.". "; $_SESSION['no']++;
echo $soal[$no-1]['soal'];
$jawaban = $_SESSION['option'][$no-1];
shuffle($jawaban);
?>
</p>
<?php
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
$_SESSION['correctAnswer'] == echo $jawaban[$i]; ? " correct" : " incorrect";
}
?>
<input type="submit" name="next" value="next">
</form>
</body>
</html>
<?php
}else{
header("location:result.php");
}
?>
There's a syntax error
in your ternary operator:
$_SESSION['correctAnswer'] == echo $jawaban[$i]; ? " correct" : " incorrect";
You should put echo before the ternary operator like this
echo $_SESSION['correctAnswer'] == $jawaban[$i] ? " correct" : " incorrect";
Replace this
$_SESSION['correctAnswer'] == echo $jawaban[$i]; ? " correct" : " incorrect";
with
echo (($_SESSION['correctAnswer'] == $jawaban[$i]) ? " correct" : " incorrect");
from horrible PHP manual ( http://php.net/manual/en/function.echo.php ):
echo is not actually a function (it is a language construct)
So, it does not return anything.
And as it is a language construct - you got your error here. As if you have an unexpected 'if' or 'while' there. It doesn't make sense to PHP interpreter.
Remove that echo. as @splash58 said.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.