The problem with the ternary operator

I expected the output to be:

http://domain.dev/category/123

But the actual conclusion: ""

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

var_dump($result);

From what I understand - it checks if it is empty($condition)empty - if true, add http://domain.dev/categoryusing ''OR else/$categoryId

What have I done wrong?

+6
source share
1 answer

just enter ()around the instructions:

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

therefore it was considered as an operator

+9
source

Source: https://habr.com/ru/post/1014213/


All Articles