Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a php equivalent to javascripts a = b && b.c || d

is there any way i can mimic javascripts loose variable handling in php? for example, in php i have to write

$instituteID = ( isset( $p['regInstituteName'] ) && isset( $p['regInstituteName']['ID'] ) ) ? $p['regInstituteName']['ID'] : null;

whereas in javascript this would condense to

instituteID = p.regInstituteName && p.regInstituteName.id || null;

doesnt seem like THAT much of a difference but it adds up

like image 826
lordvlad Avatar asked Dec 13 '12 11:12

lordvlad


2 Answers

Basically.. no. There have been some proposals in the past, but they have been rejected.

Edit: You can optimize it in the case you are happy throwing E_NOTICE errors. But I'd recommend against that.

like image 168
Evert Avatar answered Oct 06 '22 15:10

Evert


You only need one isset with your case.

Because if $p['regInstituteName']['ID'] is set, then $p['regInstituteName'] is always set.

$instituteID = isset($p['regInstituteName']['ID']) ? $p['regInstituteName']['ID'] : null;
like image 44
xdazz Avatar answered Oct 06 '22 16:10

xdazz