Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 4th, 2008, 06:05 PM
Derik
Guest
 
Posts: n/a
Default if ($b[$a]) $a=$b[$a]; More elegant way to write this?

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

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles