2

I have this variable: $logged_in_person_rsvp - its value = -1

Then I run this code:

$yes_checked = ($logged_in_person_rsvp===1) ? "checked" : "";
$maybe_checked = ($logged_in_person_rsvp===-1) ? "checked" : "";
$no_checked = ($logged_in_person_rsvp===0) ? "checked" : "";

echo '<p>logged_in_person_rsvp: '.$maybe_checked.'</p>';

And I get output as nothing. But I was expecting the output to be -1

Anyone understand why? This is weird syntax I inherited :)

6 Answers 6

3

Try using two equals signs like "==" instead of 3 and see if that makes a difference. If so, your strict comparison is probably messing up your intended result.

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

1 Comment

I'm thinking the same -- in your code you may have $logged_in_person_rsvp to be a string or float (more likely) instead of expected int. Try var_dump($logged_in_person_rsvp); and see the output.
3

you have echoed $maybe_checked

and $maybe_checked would be either "checked" or "", how would you expect it to be -1?

echoing $logged_in_person_rsvp should output -1

===================

Respond to your comment:

then try use operator == instead

=== is strict comparison between two variables

1 Comment

Yes you are right. It was my typo. I was expecting the value to be "checked"
1

The value of each one of these variables will be either "checked" or "", depending on the result of the operation in the brackets.

($logged_in_person_rsvp===-1) ? "checked" : "";

basically, what this does is check whether $logged_in_person_rsvp===-1 is true or false. If it is true, the variable will be assigned "checked", if it is false, the variable will be assigned "".

Comments

1

The ternary syntax is often confusing for newcomers. This is an alternative way using an array-map to express your code:

$checked = array(FALSE => "", TRUE => "checked");

$yes_checked =   $checked[($logged_in_person_rsvp===1)];
$maybe_checked = $checked[($logged_in_person_rsvp===-1)];
$no_checked =    $checked[($logged_in_person_rsvp===0)];

echo '<p>logged_in_person_rsvp: '.$maybe_checked.'</p>';

The === is just a strict version of the normal equal == operator.

2 Comments

Just tried to do what you did there, but it didn't output "checked" - any idea why not?
You can try to use the less strict comparison == operator. If you say the variable $logged_in_person_rsvp contains a -1 beforehand, then that -1 could be a string instead of an integer. In hindsight the === type-equality operator might be totally unneeded here.
1

Ternary operations should be enclosed in parentheses.

$maybe_checked = ($logged_in_person_rsvp===-1 ? "checked" : "");

That should do the trick.

EDIT: Also, make sure that your $logged_in_person_rsvp is -1 the integer, not '-1' the string. Or try using == instead of ===.

Comments

0

Have you tried var_dump($logged_in_person_rsvp); (or inspecting the value in xdebug, or firephp, or your choice of debugger) to determine what type $logged_in_person_rsvp is?

Using the snippet you provided, if I set:

$logged_in_person_rsvp = -1;

everything works fine, and I get <p>logged_in_person_rsvp: checked</p> but if I set:

$logged_in_person_rsvp = "-1";

then I get <p>logged_in_person_rsvp: </p>

As it has been pointed out by others, == will try to cast the two sides of the comparison to the same type, but === will check type as well and fail if one is a string and one is an int/float.

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.