I downloaded wamp so now I am back learning PHP 
I’m trying to test the === operator (which tests for equality of value and type)
I’m also trying to use the ? (ternary) operator to display appropriate results.
I want to display “Is $var1 the same as $var2 ? Yes” but all It displays is ‘Yes’
Here is the code… what am I doing wrong?
$var1 = 14;
$var2 = '14';
$result = ($var1 == $var2);
echo '<p>Is $var1 the same as $var2 ?' . $result ? 'Yes' : 'No' . '</P>';
$result = ($var1 === $var2);
echo '<p>Is $var1 the same as $var2 and of the same type?' . $result ? 'Yes' : 'No' . '</P>';
edit: The reason I’m using single quotes instead of double quotes is so that it displays ‘$var1’ and ‘$var2’ instead of displaying what’s IN those variables (which is what would happen if I used double quotes)
Is there a better way of doing that? (displaying the name of a variable instead of its contents when using double quotes)
Just making a guess, but it’s decently likely that what is happening is that when you do your first $var1 == $var2, the internal type of $var2 is changed to being an int, which then makes your test for type equality come out true.
The testing works (the former sets $result as true, and the latter sets it as false.
The problem I’m having is that the code isn’t displaying the whole message. It only displays ‘yes’.
It works if I seperate the ternary part from the first part of the message…
$var1 = 14;
$var2 = '14';
$result = ($var1 == $var2);
echo '<p>Is $var1 the same as $var2 ?';
echo $result ? 'Yes' : 'No' . '</P>';
$result = ($var1 === $var2);
echo '<p>Is $var1 the same as $var2 and of the same type?';
echo $result ? 'Yes' : 'No' . '</P>';
Ah. Well then try putting the trinary operator in parentheses.
"Blah blah " . ($test ? “Yup” : “Nop”) . “!<br />”;
The ternary operator evaluates the result of the expression before the ‘?’ and returns the expression before ‘:’ if true and the expression after ‘:’ if false. This expression:
‘<p>Is $var1 the same as $var2 ?’ . $result
is always true since it is a non-empty string, regardless of the value of $result. Hence the ternary operation
‘<p>Is $var1 the same as $var2 ?’ . $result ? ‘Yes’ : ‘No’
will always return ‘Yes’.
[QUOTE=Terminus Est]
The ternary operator evaluates the result of the expression before the ‘?’ and returns the expression before ‘:’ if true and the expression after ‘:’ if false. This expression:
‘<p>Is $var1 the same as $var2 ?’ . $result
is always true since it is a non-empty string, regardless of the value of $result. Hence the ternary operation
‘<p>Is $var1 the same as $var2 ?’ . $result ? ‘Yes’ : ‘No’
will always return ‘Yes’.
[/QUOTE]
Ah, I understand!. Thanks 
So I’ve taken Sage Rat’s advice and put it in parentheses…
$var1 = 14;
$var2 = '14';
$result = ($var1 == $var2);
echo '<p>Is $var1 the same as $var2 ? ' . ($result ? 'Yes' : 'No') . '</P>';
$result = ($var1 === $var2);
echo '<p>Is $var1 the same as $var2 and of the same type? ' . ( $result ? 'Yes' : 'No') . '</P>';
Displays…
I am inferring from this that the concatenation operator has higher precedence than the ? operator. However, this source says different, I dunno.
It’s always a good idea to use parens to avoid any ambiguity or doubt about precedence. You probably didn’t even think of “.” as an operator…
[QUOTE=CookingWithGas]
I am inferring from this that the concatenation operator has higher precedence than the ? operator. However, this source says different, I dunno.
It’s always a good idea to use parens to avoid any ambiguity or doubt about precedence. You probably didn’t even think of “.” as an operator…
[/QUOTE]
The cited table indeed shows that the concatenation operator has a higher precedence than the ternary operator.
[QUOTE=Terminus Est]
The cited table indeed shows that the concatenation operator has a higher precedence than the ternary operator.
[/QUOTE]
My bad, I must have been reading it upside down :o