if ($b[$a]) $a=$b[$a]; More elegant way to write this? 
July 4th, 2008, 06:05 PM
| | | |
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 | 
July 4th, 2008, 06:45 PM
| | | | re: if ($b[$a]) $a=$b[$a]; More elegant way to write this?
Derik wrote: $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. | 
July 4th, 2008, 07:05 PM
| | | | re: if ($b[$a]) $a=$b[$a]; More elegant way to write this?
Iván Sánchez Ortega schrieb: Quote:
Derik wrote:
>>
$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) | 
July 4th, 2008, 07:55 PM
| | | | 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:
>>>>
>$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); | 
July 6th, 2008, 12:05 PM
| | | | 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:
>>>>
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. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|