Jump to content

[SOLVED] Which conditional set off an if statement with multiple ors?


kaens

Recommended Posts

Alright, here's the deal.

 

First off, sorry for such a crappy first post. Second off, if this has been asked - sorry for not searching through the forums, but it's a fairly odd question.

 

Lets say I have an if statement with some ors. Is something like this possible (warning, pseudocode)

 

 
if ($x or $y or $z) {
blah($the_one_that_was_true);
}

 

Basically, I've written an app to control the playlist of music playing at my house. It's got play pause and start buttons. I need to execute the same external command with different arguments (the value of their associated POSTs), and do a refresh afterwards.

 

Currently this is implemented with three different ifs, practically the same code.

 

I'm wondering if the variable that "sets off" an if statement is discernable. Mainly to make my code slightly more elegant (lame, I know.). I could solve this by adding ifs to check the vars under the if - which would be fine (but not preferable) if I was doing something more complex.

 

Not to mention that this functionality would be really handy in a lot of applications. . . (the only language I can think of a way to do this in off the top of my head is perl).

 

So, any help out there?

 

Link to comment
Share on other sites

found, through a friend of mine at RIT.

 

apparently you can do assignment in if statements so if you write (lets say)

 

$x = 0
if(($_POST['x'] and $x = x) or ($_POST['y'] and $x = y) or ($_POST['z'] and $x = z))

 

you can then use $x

 

this is a dirty, dirty hack, but it's better than a bunch of nested ifs.

 

Now, I just wish I could use a variable like $_SHORT_CIRCUTER within an if statement that would represent the variable that made php enter the if.

 

Guess I'd have to brush up on my C and learn how to write php modules to do that huh?

Link to comment
Share on other sites

why not use switch statements?

 

Normally I'd just use a switch statement with a flag variable. I can't figure out the syntax of switching through the $_POST array looking for $_POST['foo']. if you know how to do that off the top of your head, please tell.

Link to comment
Share on other sites

and keans, i don't know what you're trying to do here

if(($_POST['x'] and $x = x) or ($_POST['y'] and $x = y) or ($_POST['z'] and $x = z))

by coding $x = x, you are assigning x to $x, if you are comparing, use $x == x

 

That's EXACTLY what I'm doing. So I can tell which one of the variables set off the if statement.

 

if(($_POST['play'] and $x = '/play') or ($_POST['pause'] and $x = '/pause') or ($_POST['stop'] and $x = '/stop')) {
execInBackground($_SESSION['appPath'], $_SESSION['appExe'], $x);
}

 

Or rather, so I don't have to write three ifs with almost exactly the same code in them.

Link to comment
Share on other sites

foreach ($_POST as $key => $val){
     switch ($key) {
           case 'foo': 
                print 'Yay!';
           break;
     }
}

 

 

Thank you. I very well may just keep my dirty hack though... didn't know php had foreach (I've been using php for about 3 days. Yup, noob.)

Link to comment
Share on other sites

dude,

$x == "/watever";

 

Hey, at least it saves processing time if they didn't hit stop.

 

And watever isn't a valid switch to this external prog.

 

Ridiculous. Just program it so it is easy to understand. Not for speed.

 

I doubt you could tell the speed difference between any two ways to code something. Especially in PHP, cos of the network latency. Plus you have no idea how PHP will optimize the code, or your processor will schedule any particular task.

 

monk.e.boy

Link to comment
Share on other sites

Ridiculous. Just program it so it is easy to understand. Not for speed.

 

I doubt you could tell the speed difference between any two ways to code something. Especially in PHP, cos of the network latency. Plus you have no idea how PHP will optimize the code, or your processor will schedule any particular task.

 

monk.e.boy

 

Well put. Especially if this is going to be open to public. Unless of course your goal is to make someone else think really really hard and break it down to try and figure out what the heck you are doing, I would break it out. This is not the old days where you had to name your variables $a $b $c, we have plenty of memory now =)

Link to comment
Share on other sites

Ridiculous. Just program it so it is easy to understand. Not for speed.

 

I doubt you could tell the speed difference between any two ways to code something. Especially in PHP, cos of the network latency. Plus you have no idea how PHP will optimize the code, or your processor will schedule any particular task.

 

monk.e.boy

 

Well put. Especially if this is going to be open to public. Unless of course your goal is to make someone else think really really hard and break it down to try and figure out what the heck you are doing, I would break it out. This is not the old days where you had to name your variables $a $b $c, we have plenty of memory now =)

 

Good points. I just think it's kinda nifty (and surprising) that you can do assignment in if statements. I've already changed it over to using a switch. . .

 

I still wish there was an easy way to check what variable triggered the entering of an if statement though. Using a switch and a flag variable isn't too bad - but it would improve readablity and elegance if I could just check what got triggered.

 

I could do this in ruby, or perl . . . it's a challenge to myself (and an excersize in learning php) to write this app purely in php though (not counting html and css). No javascript, no SQL. SQL isn't needed for this anyhow.

Link to comment
Share on other sites

Ridiculous. Just program it so it is easy to understand. Not for speed.

 

I doubt you could tell the speed difference between any two ways to code something. Especially in PHP, cos of the network latency. Plus you have no idea how PHP will optimize the code, or your processor will schedule any particular task.

 

monk.e.boy

 

Well put. Especially if this is going to be open to public. Unless of course your goal is to make someone else think really really hard and break it down to try and figure out what the heck you are doing, I would break it out. This is not the old days where you had to name your variables $a $b $c, we have plenty of memory now =)

 

Good points. I just think it's kinda nifty (and surprising) that you can do assignment in if statements. I've already changed it over to using a switch. . .

 

I still wish there was an easy way to check what variable triggered the entering of an if statement though. Using a switch and a flag variable isn't too bad - but it would improve readablity and elegance if I could just check what got triggered.

 

I could do this in ruby, or perl . . . it's a challenge to myself (and an excersize in learning php) to write this app purely in php though (not counting html and css). No javascript, no SQL. SQL isn't needed for this anyhow.

 

that's quite interesting, how would you do this in ruby? And perl and python?

 

Please post, I like to learn new stuff. I'm starting to love how lists and lambda functions work in python. Honestly, it rocks my world ;-)

 

monk.e.boy

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.