I'm using a shorthand if statement to validate if a value is assigned to a variable, my question is should I assign the statement to a variable, because it's working if I assigned it or not.
statement not assigned to variable | working
!empty($name) ? $name : $name="";
statement assigned to variable | also working
$is_name = (!empty($name)) ? $name : $name="" ;
Both statements are working, but which one is correct or better to be used.
this may be better. you don't need a extra ! operaiton and variable assignment.
$is_name = empty($name) ? "" : $name;
or
$is_name = isset($name) ? $name : ""; //recomended this one
In php 5.3+ you can use this:
$is_name = $name ?: "";
Note: In your case this is probably not the best idea, since it produces a warning, if $name is not set. Here I would still recommend the normal way
$is_name = isset($name) ? $name : "";
But the question is why to have a uninitialized variable anyway ?
If you just have the case that $name can be an empty string and you want to give it a default value in this case, the ?: operator is your fit.
$is_name = $name ?: "default value";