473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jul 4 '08 #1
4 1051
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.
Jul 4 '08 #2
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)
Jul 4 '08 #3
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);
Jul 4 '08 #4
On Jul 4, 7:02 pm, Thomas Mlynarczyk <tho...@mlynarczyk-webdesign.de>
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

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
($a=$b[$a]) || ($a='first');

C.
Jul 6 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: steve | last post by:
Hi, In my script (phpnuke), whenever there is access to database, there is this line of code: message_die(GENERAL_ERROR, ’some error msg’, ’’, __LINE__, __FILE__, $sql); Is there a more...
9
by: William S. Huizinga | last post by:
I've got an array.array of unsigned char and would like to make a slice of that array (e.g. a) become one long like I would in "C" : l = ((unsigned long *) (&a)); I have been getting what I...
6
by: Kamilche | last post by:
Is there a more elegant way to change the working directory of Python to the directory of the currently executing script, and add a folder called 'Shared' to the Python search path? This is what...
3
by: Adriaan van Heerden | last post by:
hi, once again apologies if I'm covering old ground. I've looked through lots of previous posts and tried some code but nothing works as yet. However, the problem must be a very common one so...
13
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the...
3
by: moose | last post by:
In C# I can write: if(someObj is someType) { .. } In C++/CLI I used: if(someObj->GetType()->Eqals(someType::typeid)) { .. } Is there some other way to do this stuff in C++/CLI? Perhaps...
4
by: Morten Wennevik | last post by:
Hi, Upon clicking a button I perform some heavy server side calculations lasting up to five minutes. During this time, the web page appear to be loading the response page. Is there some...
28
by: Rob Cowie | last post by:
Hi all, I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba', 'abb', 'abc' etc. all the way to 'zzz'. How would you construct a generator to acheive this? A simple,...
35
by: Bjoern Hoehrmann | last post by:
Hi, For a free software project, I had to write a routine that, given a Unicode scalar value U+0000 - U+10FFFF, returns an integer that holds the UTF-8 encoded form of it, for example, U+00F6...
3
by: Thomas Pajor | last post by:
Hey everybody, I got into serious trouble with template programming. I have a class which uses three template arguments, say template<typename Atype, typename Btype, typename Ctype> class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.