Welcome! Log In Create A New Profile

Advanced

[PHP] Basic php code question

Posted by Dave 
Dave
[PHP] Basic php code question
January 31, 2018 12:10AM
Hi all,

I could use a bit of help making sense of this inherited code...

$_premises = array(
'APARTMENTS'=>'APARTMENT','APT',
'APTS'=>'APT',
'ROOMS'=>'RM','RMS','RM',
'#'=>'APT#','APT #','SUITE#','SUITE #','OFFICE#','OFFICE #','Suite');


Q: when i see something like:

'APARTMENTS'=>'APARTMENT','APT',

does that mean...

'APARTMENTS' can equal/match 'APARTMENT' ?and/or match? can equal 'APT'

or what does this mean?

also

'#'=>'APT#','APT #','SUITE#','SUITE #','OFFICE#','OFFICE #','Suite'

does that mean...

'#' can equal 'APT' ?and/or match? can equal 'SUITE#' ?and/or match? can
equal ----these also 'OFFICE#','OFFICE #','Suite'



Thanks

--
Thanks - Dave
[db-18]
David Harkness
Re: [PHP] Basic php code question
January 31, 2018 12:30AM
On Tue, Jan 30, 2018 at 3:07 PM, Dave <[email protected]> wrote:

> I could use a bit of help making sense of this inherited code...
>
> $_premises = array(
> 'APARTMENTS'=>'APARTMENT','APT',
> 'APTS'=>'APT',
> 'ROOMS'=>'RM','RMS','RM',
> '#'=>'APT#','APT #','SUITE#','SUITE #','OFFICE#','OFFICE #','Suite');
>
>
> Q: when i see something like:
>
> 'APARTMENTS'=>'APARTMENT','APT',
>
> does that mean...
>
> 'APARTMENTS' can equal/match 'APARTMENT' ?and/or match? can equal 'APT'
>
> or what does this mean?
>

No, not without writing code to do the mapping. This array mixes integer
and string keys which PHP allows but causes confusion. Reformatting gives

$_premises = array(
'APARTMENTS' => 'APARTMENT',
'APT',
'APTS' => 'APT',
'ROOMS' => 'RM',
'RMS',
'RM',
'#' => 'APT#',
'APT #',
'SUITE#',
'SUITE #',
'OFFICE#',
'OFFICE #',
'Suite',
);

The cases without a => will receive integer keys starting from zero.

0 => 'APT',
1 => 'RMS',
etc.

Likely it's used to validate/map a premise to a normalized form like this:

if (!in_array($premise, $_premises)) { // valid premise?
if (isset($_premises[$premise]) { // no, maps to a valid
premise?
$premis = $_premises[$premise]; // yes, map it
}
else {
throw new InvalidArgumentException("Premise $premise is
invalid");
}
}
// $premise is valid...

Cheers!
David
Sorry, only registered users may post in this forum.

Click here to login