hah, why does name show value 1?
['name' => ($profile->getNickname() || $profile->getName()),]
a => b || c if b is empty use c isn't it?
p.s. I know I can do normal if else shorthand (ternary), but its long and unreadable, I don't like it.
hah, why does name show value 1?
['name' => ($profile->getNickname() || $profile->getName()),]
a => b || c if b is empty use c isn't it?
p.s. I know I can do normal if else shorthand (ternary), but its long and unreadable, I don't like it.
In javascript this is a common thing to write, but PHP will cast to booleans.
This question will answer yours: Best way to give a variable a default value (simulate Perl ||, ||= )
$name = $profile->getNickname() ?: $profile->getName();
In PHP, the boolean operators || and && always produce a boolean value; the operands are coerced to boolean if necessary, and the original value discarded. This is different from, for example, JavaScript, where the operands are evaluated for "truthiness" but their original values retained.
The 1 you see is just because you echoed a boolean, which in turn coerces the true to a string 1.
PHP does have a shorthand operator for what you want, though: ?:
($profile->getNickname() || $profile->getName()) is just a condition which comes true (1) that's why its showing 1.
it should be as follows:
// if nick name is set and not empty then show it otherwise show name
[ 'name' => ((isset($profile->getNickname()) && ($profile->getNickname() != "")) ? $profile->getNickname() : $profile->getName())]
return the boolean value from function if isset(data)
$profile->getNickname() and
$profile->getName()
to decision a condition.