Hyaku_ Posted January 11, 2007 Share Posted January 11, 2007 Hi!is this posible?[code]switch($var){ case 1: if($something == $something_else){ jump_to_case_2; } break; case 2: ...... break;}[/code]Thanks! Quote Link to comment Share on other sites More sharing options...
trq Posted January 11, 2007 Share Posted January 11, 2007 Nope. What are you trying to do exactly? Quote Link to comment Share on other sites More sharing options...
btherl Posted January 11, 2007 Share Posted January 11, 2007 It's not possible.. but you can do this:[code=php:0]switch($var) {case 1: if ($something != $something_else) { break; }case 2: .....break;}[/code]That will fall through to case 2 if $something == $something_else. Quote Link to comment Share on other sites More sharing options...
trq Posted January 11, 2007 Share Posted January 11, 2007 case 2 still will not run unless $var == 2. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 11, 2007 Share Posted January 11, 2007 That's not true.Try this example:[code]<?phpif (isset($_GET['v'])) switch($_GET['v']) { case '0': echo 'zero<br>'; case '1': echo 'one<br>'; case '2': echo 'two<br>'; case '3': echo 'three<br>'; default: echo 'default<br>'; }?>[/code]Or in action at http://www.rbnsn.com/phpfreaks/case_test.php?v=0Ken Quote Link to comment Share on other sites More sharing options...
trq Posted January 11, 2007 Share Posted January 11, 2007 Well, this doesn't work?[code]#!/usr/bin/php<?php $a = 10; $v = 1; switch ($v) { case 1: if ($a == 10) { break; } break; case 2: echo "this is two"; break; } ?>[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 11, 2007 Share Posted January 11, 2007 Leave out the 2nd "break" in "case 1:".Ken Quote Link to comment Share on other sites More sharing options...
trq Posted January 11, 2007 Share Posted January 11, 2007 Still doesn't work. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 11, 2007 Share Posted January 11, 2007 It's not displaying anything since $a is 10 in your example, change it to something else:[code]<?php $a = 20; $v = 1; switch ($v) { case 1: if ($a == 10) { break; } case 2: echo "this is two"; break; } ?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
trq Posted January 11, 2007 Share Posted January 11, 2007 Yeah, Im aware of that, but read the question. Quote Link to comment Share on other sites More sharing options...
Hyaku_ Posted January 11, 2007 Author Share Posted January 11, 2007 Well, I was trying to acomplish this:[code]switch($mode){ case "read_pm": if(id_of_pm_doesn't_exist_in_the_db){ don't_display_error_just_jump_to_case_list_pm; } break; case "list_pm": list_all_private_messages(); break;}[/code]I know how it can be done in different way, but I just thought if it would be posible to go to case "list_pm" on error, it would be preatty clean, but I guess I could use [i]goto[/i], but I don't want to use it.. Thanks! Quote Link to comment Share on other sites More sharing options...
trq Posted January 11, 2007 Share Posted January 11, 2007 There is no such thing as goto in php [i]yet[/i]. Quote Link to comment Share on other sites More sharing options...
Hyaku_ Posted January 11, 2007 Author Share Posted January 11, 2007 I didn't know that! Thanks! :-X 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.