2

Not sure what im doing wrong here, but the out come is always null. The script should output "you did not select an answer" only if no answer was selected but otherwise it should output the answer given:

I have updated the script as mentioned but still getting the empty output even when answer is given :/

Thanks for all the help so far guys, but even the below code doesnt work, it now just outputs as blank if no anwser, but if you do fill it in, it correctly echos the answer.

if (empty( $a1 )) {
echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
    . "<p>You did not select an answer</p>\n"
    . "</li>\n";

}

else {

echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
    . "<p><strong>" . $q1[$a1] . ":</strong></p>\n"
    . "<p>" . $r1[$a1] . "</p>\n"
    . "</li>\n";

}

Completely forgot to show this part!!

// get local copies of single answers
$a1 = trim(isset($_POST['a1'])?$_POST['a1']:99);
$a3 = trim(isset($_POST['a3'])?$_POST['a3']:99);
$a4 = trim(isset($_POST['a4'])?$_POST['a4']:99);
$a5 = trim(isset($_POST['a5'])?$_POST['a5']:99);
0

6 Answers 6

6

Don't use if($a1 == null) use if(empty($a1)) or if(isset($a1))

Sign up to request clarification or add additional context in comments.

2 Comments

Of course, that is why I said OR but thanks for pointing that out.
See edited question above, still no joy, must be something else, maybe in my answers array?
1

An empty string is not null

    $a1 = '';
    if ($a1 == null)   // is wrong

should be

$a1 = '';
if ($a1 === '')

or

if (empty($a1))

3 Comments

$a = ''; does equal null when not using strict comparisons
Updated but still getting the empty output when selecting an answer
If there is no output then look for syntax errors in your remaining part of code.
0

an empty is not the same as null try

if ($a === '') this respects also the type which is better for code quality

Comments

0
if (empty( $a1 )) {
    echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
    . "<p>You did not select an answer</p>\n"
    . "</li>\n";
}
else {
    echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
    . "<p><strong>" . $q1[$a1] . ":</strong></p>\n"
    . "<p>" . $r1[$a1] . "</p>\n"
    . "</li>\n";
}

Use empty instead of null checking

Comments

0

'null' is not same as false or ''.'null' is an object.

Comments

0

In PHP, empty string ($a) & empty array ($b) will return true if you test following express:

$a = ''; $b = array();

$a == null -> TRUE $b == null -> TRUE

also,

$a == 0 -> TRUE

So you should use '===' to test, or there's always unexpected result in your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.