RE: [PHP] control structure
-Original Message- From: Chris Stinemetz [mailto:chrisstinem...@gmail.com] Sent: Thursday, August 04, 2011 11:34 PM To: PHP General Subject: [PHP] control structure I have a php script with a simple condition. If it is not satisfied I want to exit the script otherwise I want to continue. I am having difficulties getting it to work. The script is just exiting.. Please excuse my indention. Gmail tends to distort it. Thank you, Chris This is what i have so far: if (!session_id()) { session_start(); } if($_SESSION['signed_in'] == false | $_SESSION['user_level'] != 1 || $_SESSION['user_level'] != 2 ) { //the user is not an admin echo 'Sorry, you do not have sufficient rights to access this page.br/ You must be a technician or an engineer to create a store visit.'; exit; } else { continue; If I get it to continue I want to execute the rest of the script, but It will only exit. Current user has user_level of 1 so that is not an issue. Rest of script: $market = isset($_GET['market']) ? $_GET['market'] : $_SESSION['market']; $type = isset($_GET['type']) ? $_GET['type'] : $_SESSION['type']; $store = isset($_GET['store']) ? $_GET['store'] : $_SESSION['store']; $type = str_replace('-', ' ', $type); if($_SESSION['type'] != $type) { $_SESSION['type'] = $type; $store = ''; } if($_SESSION['market'] != $market) { $type = ''; $store = ''; } $_SESSION['market'] = $market; $_SESSION['type'] = $type; $_SESSION['store'] = $store; $market_name = array(); $market_prefix = array(); $type_name = array(); $market_prefix = array(); $store_name = array(); $query = SELECT * FROM marketcode ; $result = mysql_query($query) or die(report($query,__LINE__ ,__FILE__)); //(Something went wrong); while($row = mysql_fetch_array($result)) { $market_name[] = $row['market_name']; $market_prefix[] = $row['market_prefix']; } $query = SELECT store_type FROM store_type WHERE market_prefix = '$market' ; $result = mysql_query($query) or die(report($query,__LINE__ ,__FILE__)); while($row = mysql_fetch_array($result)) { $type_name[] = $row['store_type']; } $type_name = array_unique($type_name); sort($type_name); if($type == '') { $type = $type_name[0]; $_SESSION['type'] = $type; } $query = SELECT store_name FROM store_list WHERE store_type = '$type' AND market_prefix = '$market' ; $result = mysql_query($query) or die(report($query,__LINE__ ,__FILE__)); while($row = mysql_fetch_array($result)) { $store_name[] = $row['store_name']; } // include ('includes/closedb.php'); // close dB sort($store_name); } ? div id=myspan form action=index.php method=post table tr th class=marketMarket/th th class=typeStore Type/th th class=storeStore Name/th /tr tr td select name=market onchange=javascript:get(this.parentNode); option value=Choose.../option ?php foreach($market_prefix as $key = $value) { $selected = ''; if($value == $market) { $selected = 'selected'; } //echo(option value=$value $selected $value : $market_name[$key]); echo 'option value=', htmlspecialchars($value), ' ', $selected, '', htmlspecialchars($value.' : '.$market_name[$key]), '/option'; } ? /select /td td select name=type onchange=javascript:get(this.parentNode); option value=Choose.../option ?php foreach($type_name as $value) { $selected = ''; if($value == $type) { $selected = 'selected'; } $v = str_replace(' ', '-', $value); //echo(option value=$v $selected $value);
Re: [PHP] control structure
// This part makes no sense they are not logged in and they have a level of 1 or 2 ? Yes. It might not be the best approach, but I am assigning the user a value: 1, 2, or 3 while they create an account. This will limit what they will be able to post. For example I only want users with user level of 1 or 2 to be able to complete the form in this script. Hence, the control structure I am trying to include. // And you was missing a Pipe after false. It will cause the If statement to fail. Thanks for pointing this out. I quess I have been staring at the computer way too long! Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Control structure - easier way than repeating conditions in IF?
You could do something like this: $valuesarr = array($a,$b,$c,$d); If (in_array($x,$valuesarr)) { # do something } Or I guess even: If (in_array($x,array($a,$b,$c,$d))) { # do something } I don't know if your method or this method have better performance but it's a little easier to read I guess. -TG -Original Message- From: BOOT [mailto:[EMAIL PROTECTED] Sent: Friday, April 30, 2004 11:49 AM To: [EMAIL PROTECTED] Subject: [PHP] Control structure - easier way than repeating conditions in IF? Hello! Can anyone tell me if there is an easier/shorthand for: if (($x == $a) || ($x == $b) || ($x == $c) || ($x == $d) ... ) {;} I understand the logic of why the following does not work: if ($x == ($a || $b || $c || $d)) {;} Thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control structure - easier way than repeating conditions in IF?
Why not just use a switch?... switch ($x) { case $a: case $b: case $c: case $d: //do whatever you need break; default: //catch any other values here break; } Neil BOOT wrote: *** This Email Has Been Virus Swept *** Hello! Can anyone tell me if there is an easier/shorthand for: if (($x == $a) || ($x == $b) || ($x == $c) || ($x == $d) ... ) {;} I understand the logic of why the following does not work: if ($x == ($a || $b || $c || $d)) {;} Thanks! -- -- www.curvedvision.com -- This communication is confidential to the intended recipient(s). If you are not that person you are not permitted to make use of the information and you are requested to notify the sender immediately of its receipt then destroy the copy in your possession. Any views or opinions expressed are those of the originator and may not represent those of Advanced System Architectures Ltd. *** This Email Has Been Virus Checked *** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure Syntax Question
$x ? xxx : xxx But it makes your code less readable IMHO and offers no tangible benefit whatsoever. It offers the tangible benefit of evaluating to something: return (isset($foo) ? $foo : $bar); $query = select * from foo .(isset($bar) ? where bar = ${bar} : ''); $foo = ((count($bar) 1) ? 'bars' : 'bar); - michal migurski- contact info and pgp key: sf/cahttp://mike.teczno.com/contact.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure Syntax Question
$x ? action1 : action2; structure... It looked something like: $x : action1 : action2; I'm trying to shorten having to do the following: if ($x) { action1; } else { action 2; } can someone please post the syntax if they know it? I am reading the fine documentation but can't find it... many thanks Ahbaid -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure Syntax Question
Hello Ahbaid, Tuesday, March 9, 2004, 11:42:21 PM, you wrote: AG Someone had posted a tip for using an abbreviated form of an if.. else AG structure... AG It looked something like: AG $x : action1 : action2; $x ? xxx : xxx But it makes your code less readable IMHO and offers no tangible benefit whatsoever. -- Best regards, Richard Davey http://www.phpcommunity.org/wiki/296.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure Syntax Question
Yeah, but if my code is less readable, my job security goes up ;) (Kidding) Thanks for all your help folks, that's what I needed. regards Ahbaid Richard Davey wrote: Hello Ahbaid, Tuesday, March 9, 2004, 11:42:21 PM, you wrote: AG Someone had posted a tip for using an abbreviated form of an if.. else AG structure... AG It looked something like: AG $x : action1 : action2; $x ? xxx : xxx But it makes your code less readable IMHO and offers no tangible benefit whatsoever. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure Syntax Question
Ahbaid Gaffoor wrote: Yeah, but if my code is less readable, my job security goes up ;) (Kidding) Thanks for all your help folks, that's what I needed. regards Ahbaid Richard Davey wrote: Hello Ahbaid, Tuesday, March 9, 2004, 11:42:21 PM, you wrote: AG Someone had posted a tip for using an abbreviated form of an if.. else AG structure... AG It looked something like: AG $x : action1 : action2; $x ? xxx : xxx But it makes your code less readable IMHO and offers no tangible benefit whatsoever. Before this thread dies, I'd like to clarify something. This syntax is mostly for inline ifs as what it does is return a value. For example: $apples = 2; echo 'There are '.$apples.' apple'.($apples == 1 ? '' : 's').'.'; In this case, it returns a string which is then echoed. This *could* be used in place of a normal if, but there is probably an added overhead as the value of the last statement is returned. Also, you *must* have an else when you use this syntax (even if it does nothing), it cannot be used for a simple if. -- paperCrane Justin Patrin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure Syntax Question
On 9 Mar 2004 Richard Davey wrote: $x ? xxx : xxx But it makes your code less readable IMHO and offers no tangible benefit whatsoever. Ah, to each his/her own I guess ... I actually find: $value = ($condition ? $val1 : $val2); easier to read than: if ($condition) $value = $val1; else $value = $val2; On the other hand for this: $value = ($var1 ($var2 == 4) (($var3 == $var7) || ($var9 == hello))) ? htmlspecialchars(strstr($string1, $string2) . test) : NULL; I would prefer to use: if ($var1 ($var2 == 4) (($var3 == $var7) || ($var9 == hello))) $value = htmlspecialchars(strstr($string1, $string2) . test); else $value = NULL; In other words ... IMO you can use the ?: operator to be concise (which increases readability) or to be cryptic (which reduces it). -- Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure problem
In article [EMAIL PROTECTED], [EMAIL PROTECTED] says... yeah, i really like using cases they work well and especially if you want to do something different for different values, i forgot about that, its a good way to do it, does php have case else? cuz that is a really handy thing in VB that people often forget... You mean 'default' switch ($var) { case 'TEST-1': case 'TEST-2': case 'TEST-2': do_something(); break; // to stop falling through to next option default: // any other case not above do_other_thing(); break; //Not really needed as is last statement but good practice } And default can appear anywhere in the statement, not just as last item. Cheers -- Quod subigo farinam A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Control Structure problem
This doesnt work as expected. if ( $var === TEST ONE || TEST TWO || TEST THREE) { do something; } It returns true on all strings. Ive tried using the or operator and the == but these fail as well. Any Ideas? It behaves exactly as expected. Try checking the manual section on control structures, I believe it explains that the syntax you are using is invalid. What you are saying is if ( $var === (TEST ONE || TEST TWO || TEST THREE)) { (TEST ONE || TEST TWO || TEST THREE) is always going to evaluate as true. I think what you want is if ( $var === TEST ONE || $var === TEST TWO || $var === TEST THREE) { -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure problem
try if ( $var == TEST ONE || $var == TEST TWO || $var == TEST THREE) { do something; } luke - Original Message - From: Dan J. Rychlik [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, September 17, 2003 9:53 AM Subject: [PHP] Control Structure problem This doesnt work as expected. if ( $var === TEST ONE || TEST TWO || TEST THREE) { do something; } It returns true on all strings. Ive tried using the or operator and the == but these fail as well. Any Ideas? -Dan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure problem
Thank you guys. I truly know the level of expertise on this mailing list, and I know that it proves invaluable. Thank you again. -Dan - Original Message - From: Luke Skywalker [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Tuesday, September 16, 2003 7:01 PM Subject: Re: [PHP] Control Structure problem try if ( $var == TEST ONE || $var == TEST TWO || $var == TEST THREE) { do something; } luke - Original Message - From: Dan J. Rychlik [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, September 17, 2003 9:53 AM Subject: [PHP] Control Structure problem This doesnt work as expected. if ( $var === TEST ONE || TEST TWO || TEST THREE) { do something; } It returns true on all strings. Ive tried using the or operator and the == but these fail as well. Any Ideas? -Dan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure problem
I think speaking for everyone its great to be able to help, we all started somwhere :) i remember getting stuck on the same problem in visual basic ages ago Luke - Original Message - From: Dan J. Rychlik [EMAIL PROTECTED] To: Luke Skywalker [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Wednesday, September 17, 2003 10:12 AM Subject: Re: [PHP] Control Structure problem Thank you guys. I truly know the level of expertise on this mailing list, and I know that it proves invaluable. Thank you again. -Dan - Original Message - From: Luke Skywalker [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Tuesday, September 16, 2003 7:01 PM Subject: Re: [PHP] Control Structure problem try if ( $var == TEST ONE || $var == TEST TWO || $var == TEST THREE) { do something; } luke - Original Message - From: Dan J. Rychlik [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, September 17, 2003 9:53 AM Subject: [PHP] Control Structure problem This doesnt work as expected. if ( $var === TEST ONE || TEST TWO || TEST THREE) { do something; } It returns true on all strings. Ive tried using the or operator and the == but these fail as well. Any Ideas? -Dan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Control Structure problem
Dan J. Rychlik mailto:[EMAIL PROTECTED] on Tuesday, September 16, 2003 5:12 PM said: Thank you guys. I truly know the level of expertise on this mailing list, and I know that it proves invaluable. lovingjab If by I truly know the level of expertise on this mailing list you meant I truly don't know the basics of PHP syntax, then yeah, you'd be right. ;) !! /lovingjab :) Chris. p.s. Or maybe it's me that's doesn't know the basics... ?? p.p.s. I totally think that the syntax you presented is logical and should be available. It makes much more sense to me to do it the way you did, but alas... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure problem
yeah, it does make more sense to do it that way for some things, but when you get ands, and ors, it gets complicated eg. if ($a == foo $b == bar || something){ //do something } does that mean $a can either = foo or something or $b = bar or something? and thats just a simple example :) but i know it would be really nice in simple logical expressions Luke - Original Message - From: Chris W. Parker [EMAIL PROTECTED] To: Dan J. Rychlik [EMAIL PROTECTED]; Luke Skywalker [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Wednesday, September 17, 2003 10:18 AM Subject: RE: [PHP] Control Structure problem Dan J. Rychlik mailto:[EMAIL PROTECTED] on Tuesday, September 16, 2003 5:12 PM said: Thank you guys. I truly know the level of expertise on this mailing list, and I know that it proves invaluable. lovingjab If by I truly know the level of expertise on this mailing list you meant I truly don't know the basics of PHP syntax, then yeah, you'd be right. ;) !! /lovingjab :) Chris. p.s. Or maybe it's me that's doesn't know the basics... ?? p.p.s. I totally think that the syntax you presented is logical and should be available. It makes much more sense to me to do it the way you did, but alas... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure problem
* Thus wrote Chris W. Parker ([EMAIL PROTECTED]): Dan J. Rychlik mailto:[EMAIL PROTECTED] on Tuesday, September 16, 2003 5:12 PM said: Thank you guys. I truly know the level of expertise on this mailing list, and I know that it proves invaluable. p.p.s. I totally think that the syntax you presented is logical and should be available. It makes much more sense to me to do it the way you did, but alas... Well technically you can do it like: switch ($var) { case 'TEST-1': case 'TEST-2': case 'TEST-2': do something } Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure problem
yeah, i really like using cases they work well and especially if you want to do something different for different values, i forgot about that, its a good way to do it, does php have case else? cuz that is a really handy thing in VB that people often forget... Luke - Original Message - From: Curt Zirzow [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, September 17, 2003 10:49 AM Subject: Re: [PHP] Control Structure problem * Thus wrote Chris W. Parker ([EMAIL PROTECTED]): Dan J. Rychlik mailto:[EMAIL PROTECTED] on Tuesday, September 16, 2003 5:12 PM said: Thank you guys. I truly know the level of expertise on this mailing list, and I know that it proves invaluable. p.p.s. I totally think that the syntax you presented is logical and should be available. It makes much more sense to me to do it the way you did, but alas... Well technically you can do it like: switch ($var) { case 'TEST-1': case 'TEST-2': case 'TEST-2': do something } Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Control Structure problem
are you thinking of default: ? -Original Message- From: Luke Skywalker [mailto:[EMAIL PROTECTED] Sent: Wednesday, 17 September 2003 10:51 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] Control Structure problem yeah, i really like using cases they work well and especially if you want to do something different for different values, i forgot about that, its a good way to do it, does php have case else? cuz that is a really handy thing in VB that people often forget... Luke - Original Message - From: Curt Zirzow [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, September 17, 2003 10:49 AM Subject: Re: [PHP] Control Structure problem * Thus wrote Chris W. Parker ([EMAIL PROTECTED]): Dan J. Rychlik mailto:[EMAIL PROTECTED] on Tuesday, September 16, 2003 5:12 PM said: Thank you guys. I truly know the level of expertise on this mailing list, and I know that it proves invaluable. p.p.s. I totally think that the syntax you presented is logical and should be available. It makes much more sense to me to do it the way you did, but alas... Well technically you can do it like: switch ($var) { case 'TEST-1': case 'TEST-2': case 'TEST-2': do something } Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure problem
Yeah, thats exactly what im thinking, and speaking of it, im going to start using it a lot more :) thanks Luke - Original Message - From: Martin Towell [EMAIL PROTECTED] To: 'Luke Skywalker' [EMAIL PROTECTED]; [EMAIL PROTECTED] Sent: Wednesday, September 17, 2003 10:51 AM Subject: RE: [PHP] Control Structure problem are you thinking of default: ? -Original Message- From: Luke Skywalker [mailto:[EMAIL PROTECTED] Sent: Wednesday, 17 September 2003 10:51 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] Control Structure problem yeah, i really like using cases they work well and especially if you want to do something different for different values, i forgot about that, its a good way to do it, does php have case else? cuz that is a really handy thing in VB that people often forget... Luke - Original Message - From: Curt Zirzow [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Wednesday, September 17, 2003 10:49 AM Subject: Re: [PHP] Control Structure problem * Thus wrote Chris W. Parker ([EMAIL PROTECTED]): Dan J. Rychlik mailto:[EMAIL PROTECTED] on Tuesday, September 16, 2003 5:12 PM said: Thank you guys. I truly know the level of expertise on this mailing list, and I know that it proves invaluable. p.p.s. I totally think that the syntax you presented is logical and should be available. It makes much more sense to me to do it the way you did, but alas... Well technically you can do it like: switch ($var) { case 'TEST-1': case 'TEST-2': case 'TEST-2': do something } Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure problem
On Wed, Sep 17, 2003 at 12:49:03AM +, Curt Zirzow wrote: : : switch ($var) { : case 'TEST-1': case 'TEST-2': case 'TEST-2': : do something : } The switch statement doesn't do an equivalency test, does it? So while this switch statement can be rewritten as: if (($var == 'TEST-1') || ($var == 'TEST-1') || ($var == 'TEST-1')) { do something } it doesn't do: if (($var === 'TEST-1') || ($var === 'TEST-1') || ($var === 'TEST-1')) { do something } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure problem
* Thus wrote Eugene Lee ([EMAIL PROTECTED]): On Wed, Sep 17, 2003 at 12:49:03AM +, Curt Zirzow wrote: The switch statement doesn't do an equivalency test, does it? So while this switch statement can be rewritten as: if (($var == 'TEST-1') || ($var == 'TEST-1') || ($var == 'TEST-1')) { do something } it doesn't do: if (($var === 'TEST-1') || ($var === 'TEST-1') || ($var === 'TEST-1')) { do something } Correct. Although in most cases == is a good enough test. Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure problem
On Wed, Sep 17, 2003 at 01:29:26PM +1000, Tom Rogers wrote: : Wednesday, September 17, 2003, 11:47:45 AM, Eugene Lee wrote: : : EL The switch statement doesn't do an equivalency test, does it? [...] : EL it doesn't do: : : EL if (($var === 'TEST-1') || : EL($var === 'TEST-1') || : EL($var === 'TEST-1')) : EL { : EL do something : EL } : : You can do it this way I think :) : : switch (true) { : case ($var === 'TEST-1')?true:false: : case ($var === 'TEST-2')?true:false: : case ($var === 'TEST-2')?true:false: :do something : } Oh man, that's just sick... I guess, for the sake of performance and readability, it's probably easier to do an is_string($var) (or whatever variable type) before doing a normal switch statement. Still... that's just sick... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure problem
--- Eugene Lee [EMAIL PROTECTED] wrote: : switch (true) { : case ($var === 'TEST-1')?true:false: : case ($var === 'TEST-2')?true:false: : case ($var === 'TEST-2')?true:false: :do something : } Oh man, that's just sick... Partially because it's unnecessarily complex. This is like saying: if ($var === 'TEST-1') { $expression = true; } else { $expression = false; } if ($expression) { ... While the ternary operator makes this redundancy less obvious, it only adds to the complexity and lack of readability. Consider the following code as a substitute for the above example: if ($var === 'TEST-1') { ... Hopefully that part is clear. Now, on to the original question. Try this example: ? $foo = 'bar'; switch (true) { case ($foo === 'notbar'): echo 'A'; break; case ($foo === 'bar'); echo 'B'; break; default: echo 'C'; break; } ? This should output B. You will also notice that it works when you switch on $foo instead of the boolean true, but this is misleading. PHP converts $foo to the boolean true when comparing to the expressions, because we set it to the string bar. To understand this point further, try this example: ? $foo = 0; switch ($foo) { case ($foo === 'notbar'): echo 'A'; break; case ($foo === 0); echo 'B'; break; default: echo 'C'; break; } ? This should also output B. That seems to be wrong, but in this case it is comparing each expression to $foo, which is the integer 0 in this case, so it evaluates to false. So, you will see A, because ($foo === 'notbar') also evaluates to false. Recap: 1. You can switch on anything, including boolean values. 2. Your cases can be expressions to be evaluated. Hope that helps. Chris = Become a better Web developer with the HTTP Developer's Handbook http://httphandbook.org/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] control structure question
On Tue, 23 Jul 2002, Javier Montserat wrote: So refering back, i re-wrote the original example using the switch syntax... switch (true) { case doStep1(): case doStep2(): case doStep3(): error(); break; default: valid(); } Each case expressions is evaluated, until one of them evaluates to a value equal (==) to the switch expression (in this case a boolean error flag). The error() code will only be called if one of the doStep() evaluates to false. And the valid() code will only be evaluated if the switch reached the default, which means that none of the above check returned false I think for this example the switch syntax is more elegant than using a series of if() statements. That's pretty clever! miguel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] control structure question
For steps and sequences, I always use switches. Most commonly associated with the action variable, it would look like this: switch($action) { default: // what to show if no variable exists, or is set to a value // that is not acceptable below break; case add_file: // what to do in the even that a file needs to be added // to the database. break; case remove_file: // what to do in the even that a file need sto be removed // from the database. break; } Basically, whatever the value of $action, that is what section of code will execute. Hope that helps! Martin Clifford Homepage: http://www.completesource.net Developer's Forums: http://www.completesource.net/forums/ Javier Montserat [EMAIL PROTECTED] 07/23/02 10:03AM is there a more programmatically elegant way of saying... $isError = ; function main() { doStep1(); if (!$isError) { doStep2(); } if (!$isError) { doStep3(); } // etc. etc. } function doStep1() { if ($something) { $isError = 1; } } function doStep2() { if ($something) { $isError = 1; } } _ Join the world's largest e-mail service with MSN Hotmail. http://www.hotmail.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] control structure question
So refering back, i re-wrote the original example using the switch syntax... switch (true) { case doStep1(): case doStep2(): case doStep3(): error(); break; default: valid(); } Each case expressions is evaluated, until one of them evaluates to a value equal (==) to the switch expression (in this case a boolean error flag). The error() code will only be called if one of the doStep() evaluates to false. And the valid() code will only be evaluated if the switch reached the default, which means that none of the above check returned false I think for this example the switch syntax is more elegant than using a series of if() statements. Thanks, Javier For steps and sequences, I always use switches. Most commonly associated with the action variable, it would look like this: switch($action) { default: // what to show if no variable exists, or is set to a value // that is not acceptable below break; case add_file: // what to do in the even that a file needs to be added // to the database. break; case remove_file: // what to do in the even that a file need sto be removed // from the database. break; } Basically, whatever the value of $action, that is what section of code will execute. Hope that helps! Martin Clifford Homepage: http://www.completesource.net Developer's Forums: http://www.completesource.net/forums/ Javier Montserat [EMAIL PROTECTED] 07/23/02 10:03AM is there a more programmatically elegant way of saying... $isError = ; function main() { doStep1(); if (!$isError) { doStep2(); } if (!$isError) { doStep3(); } // etc. etc. } function doStep1() { if ($something) { $isError = 1; } } function doStep2() { if ($something) { $isError = 1; } } _ Join the world's largest e-mail service with MSN Hotmail. http://www.hotmail.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php _ Chat with friends online, try MSN Messenger: http://messenger.msn.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Control Structure Problem
I forgot to say I deleted tabs so that the width might fit in a typical mail reader. Jeff Oien This code assigns rooms to kids signing up for Sunday school at a very large church. Each subsequent registration is put into the next room on the list. It looks up in the MySQL database the room the last person in that age category and hour was assigned and assigns the current person the next one. If someone is the first person they get assigned the first room. However if they are the first person ${newroom.$x} doesn't get assigned to anything. But thereafter it works fine. I've had a couple other problems and am wondering if there is a better way to do this or if I have any syntax problems. Thanks. Jeff Oien $x = 1; while ($x = $Number_Children) { if (${category.$x} == 'SS KidZone - Kindergarten') { if ((${Grade.$x}) == 'K' (${selection.$x}) == '2nd Hour') { $sql = SELECT * FROM $table_name WHERE Category = 'SS KidZone - Kindergarten' Element1 = '2nd Hour' ORDER by ID desc; $result = @mysql_query($sql,$connection) or die(Couldn't execute query); if ($row = mysql_fetch_array($result)) { $room = $row['Element2']; if ($room == 'C143 Pink Check') { ${newroom.$x} = 'C143 Pink Dot'; } if ($room == 'C143 Pink Dot') { ${newroom.$x} = 'C148 Teal Check'; } if ($room == 'C148 Teal Check') { ${newroom.$x} = 'C148 Teal Dot'; } if ($room == 'C148 Teal Dot') { ${newroom.$x} = 'C143 Pink Check'; } } else { ${newroom.$x} = 'C143 Pink Check'; } } do database insertion } $x++; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]