Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old July 4th, 2008, 06:05 PM
Derik
Guest
 
Posts: n/a
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
  #2  
Old July 4th, 2008, 06:45 PM
=?UTF-8?B?SXbDoW4gU8OhbmNoZXogT3J0ZWdh?=
Guest
 
Posts: n/a

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.
  #3  
Old July 4th, 2008, 07:05 PM
Thomas Mlynarczyk
Guest
 
Posts: n/a

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)
  #4  
Old July 4th, 2008, 07:55 PM
The Natural Philosopher
Guest
 
Posts: n/a

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);
  #5  
Old July 6th, 2008, 12:05 PM
C. (http://symcbean.blogspot.com/)
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Elegant way of making template arguments dependent on user input? Thomas Pajor answers 3 October 10th, 2007 06:35 PM
Looking for a more elegant way to do memory offsets mikegw answers 5 November 14th, 2005 07:23 AM
There's got to be a more elegant way Lilith answers 9 September 2nd, 2005 07:45 AM
More elegant way to cwd? Kamilche answers 6 July 18th, 2005 08:35 PM