Click here to get back home

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

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
if ($b[$a]) $a=$b[$a]; More elegant way to write this? Derik 07-04-2008
Get Chitika Premium
Posted by Derik on July 4, 2008, 1:00 pm
Please log in for more thread options
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

Posted by Iván Sánchez on July 4, 2008, 1:37 pm
Please log in for more thread options
Derik wrote:

> 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.

Posted by Thomas Mlynarczyk on July 4, 2008, 2:02 pm
Please log in for more thread options
Iván Sánchez Ortega schrieb:
> Derik wrote:
>
>> 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)

Posted by The Natural Philosopher on July 4, 2008, 2:52 pm
Please log in for more thread options
Thomas Mlynarczyk wrote:
> Iván Sánchez Ortega schrieb:
>> Derik wrote:
>>
>>> 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);

Posted by C. (http://symcbean.blogspot.c on July 6, 2008, 6:57 am
Please log in for more thread options
wrote:
> Iv=E1n S=E1nchez Ortega schrieb:
>
> > Derik wrote:
>
> >> if ($b[$a]) $a=3D$b[$a]
>
> > $a =3D $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 =E0 avoir tort qu'ils ont raison!
> (Coluche)

($a=3D$b[$a]) || ($a=3D'first');

C.

Similar ThreadsPosted
Elegant error reporting, possible? March 13, 2005, 10:10 pm
There must be a better way to write this September 18, 2004, 2:03 pm
XML Write April 15, 2005, 9:58 pm
MOD Re-Write February 15, 2007, 5:29 am
write to log January 10, 2008, 12:28 pm
How do you write this Javascript in PHP? October 24, 2004, 7:47 pm
How to write textfiles with php? August 13, 2005, 7:24 pm
Hello?Anyone who would like to write IM extension? August 8, 2007, 5:58 am
How to use for loop to write to file? July 20, 2004, 8:03 am
should I write $HTTP_POST_VARS[SubmitEmail] January 29, 2005, 8:32 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap