wordpress - PHP return output based on zero, empty or value -
i have simple requirement proving difficult. i'm using wordpress store meta value 'session_capacity', can have following possible values
- 0
- integer e.g. 99
- nothing, left blank
the problem when value stored zero, can't figure out setup use echo different statement based on whether it's 0 or empty.
this desired output...
if (zero) // elseif (integer e.g. 99) // elseif (blank) // something
actual function...
function get_session_capacity($post_id){ // total capacity of session $session_capacity = get_post_meta($post_id, 'session_capacity', true); // meta value stored string converting integer $session_capacity = intval($session_capacity); if($session_capacity === 0) return 'you cannot book session'; elseif(empty($session_capacity)) return default_session_capacity(); else return $session_capacity; }
you can check if $session_capacity
set or not, , set accordingly
$session_capacity = isset($session_capacity)? intval($session_capacity) : null;
or better
$session_capacity = (isset($session_capacity) && !empty($session_capacity))? intval($session_capacity) : null;
the latter works when variable set empty string. doesn't give 0
when $session_capacity
not set (blank)
Comments
Post a Comment