6

I was expecting the output would be:

http://domain.dev/category/123

But the actual output is: ""

$condition = true;
$categoryId = 123;
$result = 'http://domain.dev/category' . empty($condition) ? '' : '/' . $categoryId;

var_dump($result);

From what I understand - it check if empty($condition) is empty - if true then append http://domain.dev/category with '' OR else /$categoryId

What did I do wrong?

I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213
  • 2
    What you're actually checking there is whether `'http://domain.dev/category . empty($condition)` is "truthy" - which it always is - so the empty string is returned. – CD001 Jan 19 '17 at 11:32
  • @CD001 Ahhh now I get it :) missed the first part. – I'll-Be-Back Jan 19 '17 at 11:55

1 Answers1

9

just put () around statement:

$result = 'http://domain.dev/category' . (empty($condition) ? '' : '/' . $categoryId);

so it's treated as operator

itzmukeshy7
  • 2,669
  • 1
  • 21
  • 29
Bostjan
  • 774
  • 6
  • 11