PHP variable declaration shorthand (similar to JavaScript) -


in javascript, can this:

var somevar = {     propertytwo : false,     propertythree : "hello!" }  var test = somevar.propertyone || somevar.propertytwo || somevar.propertythree  alert(test); //displays "hello!" 

is there similar functionality in php this? haven't been able find online.

i tried this, php treats comparison , echo's 1 (true)

$somevar = array(     'propertytwo' => false,     'propertythree' => "hello!" );  $test = $somevar['propertyone'] || $somevar['propertytwo'] || $somevar['propertythree'];  echo $test; //displays '1' 

not big deal if there isn't, figured of bells , whistles provided in php 5.x, there kind of shorthand assigning first true value in list of values single variable that.

i suppose write function.

edit :

as suspected, php doesn't have same quirk.

quick function wrote

function assign_list($list){ foreach($list $v)     if(isset($v) && $v) return $v; return false; } 

just pass array of stuff

the following work in php >= 5.3, still receive notice error because propertyone not defined.

<?php $somevar = array(     'propertytwo' => false,     'propertythree' => "hello!" );  $test = $somevar['propertyone'] ?: $somevar['propertytwo'] ?: $somevar['propertythree'];  echo $test; //displays 'hello!' 

you work around supressing variables, highly unrecommended:

$test = @$somevar['propertyone'] ?: @$somevar['propertytwo'] ?: @$somevar['propertythree']; 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -