kaens Posted March 23, 2007 Share Posted March 23, 2007 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? Quote Link to comment Share on other sites More sharing options...
jguy Posted March 23, 2007 Share Posted March 23, 2007 why not use switch statements? Quote Link to comment Share on other sites More sharing options...
kaens Posted March 23, 2007 Author Share Posted March 23, 2007 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? Quote Link to comment Share on other sites More sharing options...
r-it Posted March 23, 2007 Share Posted March 23, 2007 it doesnt matter if($a == 1 || $b == 1 || $c == 1) { ..................... } Quote Link to comment Share on other sites More sharing options...
kaens Posted March 23, 2007 Author Share Posted March 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
r-it Posted March 23, 2007 Share Posted March 23, 2007 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 Quote Link to comment Share on other sites More sharing options...
kaens Posted March 23, 2007 Author Share Posted March 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
r-it Posted March 23, 2007 Share Posted March 23, 2007 dude, $x == "/watever"; Quote Link to comment Share on other sites More sharing options...
per1os Posted March 23, 2007 Share Posted March 23, 2007 foreach ($_POST as $key => $val){ switch ($key) { case 'foo': print 'Yay!'; break; } } Quote Link to comment Share on other sites More sharing options...
kaens Posted March 23, 2007 Author Share Posted March 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
kaens Posted March 23, 2007 Author Share Posted March 23, 2007 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.) Quote Link to comment Share on other sites More sharing options...
monk.e.boy Posted March 23, 2007 Share Posted March 23, 2007 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 Quote Link to comment Share on other sites More sharing options...
per1os Posted March 23, 2007 Share Posted March 23, 2007 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 =) Quote Link to comment Share on other sites More sharing options...
kaens Posted March 23, 2007 Author Share Posted March 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
monk.e.boy Posted March 23, 2007 Share Posted March 23, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.