# What's wrong with this PHP code? (playing with the === and ? operators.

**URL:** <https://boards.straightdope.com/t/whats-wrong-with-this-php-code-playing-with-the-and-operators/461041>\
**Category:** Factual Questions\
**Created:** [August 23, 2008, 7:36pm UTC](https://boards.straightdope.com/t/whats-wrong-with-this-php-code-playing-with-the-and-operators/461041 "2008-08-23T19:36:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [August 23, 2008, 7:36pm UTC](https://boards.straightdope.com/t/whats-wrong-with-this-php-code-playing-with-the-and-operators/461041/1 "2008-08-23T19:36:13Z")

</div>

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?

```php

$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)

---

<div class="post-metadata">

**Author:** ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)\
**Post date:** [August 23, 2008, 8:23pm UTC](https://boards.straightdope.com/t/whats-wrong-with-this-php-code-playing-with-the-and-operators/461041/2 "2008-08-23T20:23:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [August 23, 2008, 8:36pm UTC](https://boards.straightdope.com/t/whats-wrong-with-this-php-code-playing-with-the-and-operators/461041/3 "2008-08-23T20:36:42Z")

</div>

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’.

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [August 23, 2008, 8:41pm UTC](https://boards.straightdope.com/t/whats-wrong-with-this-php-code-playing-with-the-and-operators/461041/4 "2008-08-23T20:41:00Z")

</div>

It works if I seperate the ternary part from the first part of the message…

```php
$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>';

```

---

<div class="post-metadata">

**Author:** ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)\
**Post date:** [August 23, 2008, 8:41pm UTC](https://boards.straightdope.com/t/whats-wrong-with-this-php-code-playing-with-the-and-operators/461041/5 "2008-08-23T20:41:58Z")

</div>

Ah. Well then try putting the trinary operator in parentheses.

"Blah blah " . ($test ? “Yup” : “Nop”) . “!\<br /\>”;

---

<div class="post-metadata">

**Author:** ![Terminus\_Est](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/terminus_est/32/3087_2.png) [@Terminus\_Est](https://boards.straightdope.com/u/Terminus_Est)\
**Post date:** [August 23, 2008, 8:57pm UTC](https://boards.straightdope.com/t/whats-wrong-with-this-php-code-playing-with-the-and-operators/461041/6 "2008-08-23T20:57:18Z")

</div>

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’.

---

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [August 23, 2008, 9:04pm UTC](https://boards.straightdope.com/t/whats-wrong-with-this-php-code-playing-with-the-and-operators/461041/7 "2008-08-23T21:04:23Z")

</div>

[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…

```php
$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…

> [@localhost](#):
>
> Is $var1 the same as $var2 ? Yes
> 
> Is $var1 the same as $var2 and of the same type? No

---

<div class="post-metadata">

**Author:** ![CookingWithGas](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cookingwithgas/32/485_2.png) [@CookingWithGas](https://boards.straightdope.com/u/CookingWithGas)\
**Post date:** [August 23, 2008, 9:41pm UTC](https://boards.straightdope.com/t/whats-wrong-with-this-php-code-playing-with-the-and-operators/461041/8 "2008-08-23T21:41:20Z")

</div>

I am inferring from this that the concatenation operator has higher precedence than the ? operator. However, [this source](http://aspn.activestate.com/ASPN/docs/PHP/language.operators.precedence.html) 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…

---

<div class="post-metadata">

**Author:** ![Terminus\_Est](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/terminus_est/32/3087_2.png) [@Terminus\_Est](https://boards.straightdope.com/u/Terminus_Est)\
**Post date:** [August 23, 2008, 9:47pm UTC](https://boards.straightdope.com/t/whats-wrong-with-this-php-code-playing-with-the-and-operators/461041/9 "2008-08-23T21:47:32Z")

</div>

[QUOTE=CookingWithGas]  
I am inferring from this that the concatenation operator has higher precedence than the ? operator. However, [this source](http://aspn.activestate.com/ASPN/docs/PHP/language.operators.precedence.html) 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.

---

<div class="post-metadata">

**Author:** ![CookingWithGas](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cookingwithgas/32/485_2.png) [@CookingWithGas](https://boards.straightdope.com/u/CookingWithGas)\
**Post date:** [August 24, 2008, 2:49pm UTC](https://boards.straightdope.com/t/whats-wrong-with-this-php-code-playing-with-the-and-operators/461041/10 "2008-08-24T14:49:57Z")

</div>

[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
