Connecting Tech Pros Worldwide Forums | Help | Site Map

if ($b[$a]) $a=$b[$a]; More elegant way to write this?

Derik
Guest
 
Posts: n/a
#1: Jul 4 '08
Okay, I'm using this construction a LOT lately:
if ($b[$a]) $a=$b[$a]
(The IF is actually usually an isset() or != '' ... bu you get the
picture.)

I do it a lot, and it's really... awkward, both in terms of code and
the logical flops involved. I'm clearing info being passed to DB
queries, so a typical example might be:

$a = 'first'; //A value passed through a query string;

$b = array( 'first' ='first_name',
'last' ='last_name' ); //A conversion array. Some of
these are extremely complicated and swap out dependent on
circumstances, so I'm pretty happy with this as a way of double-
checking my queris befroe the hit the $sql;

if ( $b[$a] != '' ) $a = $b[$a]; //Get 'first_name';

Lookid how logn that was! And I had to repeat the array nest twice!
It's not that big a deal- except the ACTUAl queries I'm working on are
a bit more like...

if ( isset($this->sort_order[$extra['sort_order']]) )
$extra['sort_order'] = $this->sort_order[$extra['sort_order']];

....yeah. Is there a simple way to perform this operation WITHOUT
writing a custom function that I'm missing?

-Derik

=?UTF-8?B?SXbDoW4gU8OhbmNoZXogT3J0ZWdh?=
Guest
 
Posts: n/a
#2: Jul 4 '08

re: if ($b[$a]) $a=$b[$a]; More elegant way to write this?


Derik wrote:
Quote:
if ($b[$a]) $a=$b[$a]
$a = $b[$a] || $a;


Cheers,
--
----------------------------------
Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Las ideas no duran mucho. Hay que hacer algo con ellas.- Ramon y Cajal.
Thomas Mlynarczyk
Guest
 
Posts: n/a
#3: Jul 4 '08

re: if ($b[$a]) $a=$b[$a]; More elegant way to write this?


Iván Sánchez Ortega schrieb:
Quote:
Derik wrote:
>
Quote:
> if ($b[$a]) $a=$b[$a]
>
$a = $b[$a] || $a;
Nope. This would return a Boolean in PHP. (In JavaScript or Python it
would work, though.)

Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux Ã* avoir tort qu'ils ont raison!
(Coluche)
The Natural Philosopher
Guest
 
Posts: n/a
#4: Jul 4 '08

re: if ($b[$a]) $a=$b[$a]; More elegant way to write this?


Thomas Mlynarczyk wrote:
Quote:
Iván Sánchez Ortega schrieb:
Quote:
>Derik wrote:
>>
Quote:
>> if ($b[$a]) $a=$b[$a]
>>
>$a = $b[$a] || $a;
>
Nope. This would return a Boolean in PHP. (In JavaScript or Python it
would work, though.)
>
Greetings,
Thomas
>
$a=(($b[$a])? $b[$a]:$a);
C. (http://symcbean.blogspot.com/)
Guest
 
Posts: n/a
#5: Jul 6 '08

re: if ($b[$a]) $a=$b[$a]; More elegant way to write this?


On Jul 4, 7:02 pm, Thomas Mlynarczyk <tho...@mlynarczyk-webdesign.de>
wrote:
Quote:
Iván Sánchez Ortega schrieb:
>
Quote:
Derik wrote:
>
Quote:
Quote:
if ($b[$a]) $a=$b[$a]
>
Quote:
$a = $b[$a] || $a;
>
Nope. This would return a Boolean in PHP. (In JavaScript or Python it
would work, though.)
>
Greetings,
Thomas
>
--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
($a=$b[$a]) || ($a='first');

C.
Closed Thread