1

I have a Product class which retrieves database data by the product name. The URL would look like this:

/products/product-name

My class works fine and retrieves product data when this method is function is executed:

$library->get('Router')->map('GET|POST', '/products/[*:product]', function($prod) use ($library){
    require_once(_DIR_ . 'lib/Product.php');
    $p = new Product($prod);
    $state = $p->prepare();
    require_once(_DIR_ . $state ? "views/product.php" : "views/404.php");
});

Only, when the $state is false, the required file is the product.php file not the 404.php file.

I have debugged this and the $state IS false when there is no product so my method is returning the correct bool, only, the ternary operator seems to run the TRUE each time.

If I change the expression to this:

require_once(_DIR_ . $state === TRUE ? "views/product.php" : "views/404.php");

It works fine, can someone explain why this is happening? If I was to do:

$example = false;
echo ($example) ? 'yes' : 'no';

It works, so why is it behaving this way?

Also,

require_once(_DIR_ . $p->prepare() === TRUE ? "views/product.php" : "views/404.php");

Does not work, this runs false everytime...

Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • 1
    Possible duplicate of [Ternary operator left associativity](http://stackoverflow.com/questions/20559150/ternary-operator-left-associativity) – u_mulder Oct 23 '16 at 18:36
  • Oh, that actually makes so much sense now... the `_DIR_` needs to be separate from the expression – Jaquarh Oct 23 '16 at 18:38

3 Answers3

3

Because . has precedence over the ternary operator, you need to use parentheses:

require_once(_DIR_ . ($state ? "views/product.php" : "views/404.php"));
trincot
  • 317,000
  • 35
  • 244
  • 286
2

In the first require_once(_DIR_ . $state ? "views/product.php" : "views/404.php"); you don't eval a condition so the ternary not return the proper value

In the second

require_once(_DIR_ . $state === TRUE ? "views/product.php" : "views/404.php");

you clearly test a valid condition and the ternary operator can work correctly

 _DIR_ . $state 

is not a condition is an assertion

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
2

Only, when the $state is false, the required file is the product.php file not the 404.php file.

But you're not testing just $state, you're testing the result of the concatenation of _DIR_ and $state.

Since _DIR_ will always return a truthy value, the collective result is never false.

This is not your intention, of course; as @trincot notes, the _DIR_ reference is part of the require path, not the condition. So use parentheses to distinguish the two:

require_once(_DIR_.($state ? "views/product.php" : "views/404.php"));
Mitya
  • 33,629
  • 9
  • 60
  • 107