1 balupton Dec 06, 2005 16:34
3 blueyed Dec 06, 2005 22:09
balupton, I've not looked at the code. But from the comment I'd say that there is a "case 'update'" above which does not break (always).
Could that be? :D
4 balupton Dec 07, 2005 02:52
;)
// NOTE: no break here, we go on to edit!
case 'edit':
// ---------- Edit blog form ----------
Yer but the if statement still is not needed, as its already perfomed by the switch.
To get to the if statement $action NEEDS to be 'edit'.
5 yabba Dec 07, 2005 16:46
case 'update':
// ---------- Update blog in DB ----------
// Check permissions:
$current_User->check_perm( 'blog_properties', 'edit', true, $blog );
?>
<div class="panelinfo">
<h3><?php printf( T_('Updating Blog [%s]...'), $edited_Blog->dget( 'name' ) )?></h3>
<?php
switch( $AdminUI->get_path(1) )
{
case 'general':
set_edited_Blog_from_params( 'general' );
break;
case 'perm':
blog_update_user_perms( $blog );
break;
case 'permgroup':
blog_update_group_perms( $blog );
break;
case 'advanced':
set_edited_Blog_from_params( 'advanced' );
break;
}
// Commit changes in cache: (so that changes are not lost in the form)
$BlogCache->add( $edited_Blog );
if( !$Messages->display_cond( T_('Cannot update, please correct this error:' ), T_('Cannot update, please correct these errors:' )) )
{ // Commit update to the DB:
$edited_Blog->dbupdate();
}
// display notes
$Messages->display( '', '', true, 'note' );
?>
</div>
<?php
// NOTE: no break here, we go on to edit!
case 'edit':
// ---------- Edit blog form ----------
if( $action == 'edit' )
{ // permissions have not been checked on update:
$current_User->check_perm( 'blog_properties', 'edit', true, $blog );
}
Or the action could be 'update', which still hits the same if statement ;)
¥
6 balupton Dec 07, 2005 17:21
Arrr, this is how switches work:
switch($var){
case "hello":
//do stuff
case "bye":
//$var must still be bye to reach here
break;
case "test":
$var = "cool";
break;
case "cool":
//if $var == "test" it would not get here because of the break
break;
case "abc":
$var = "123";
case "123":
//if $var == "abc" then it would reach here because we didnt break
break;
}
What your saying is that if you dont have break it skips the case?
So in the above example ur saying if $var == "hello" it will perform every case underneath it beacause the break is not there.
When its not like that :S
Well thats what i think your saying...
7 yabba Dec 07, 2005 17:32
Sort of, $var == 'hello' will continue to work until the break statement in 'bye'.
¥
8 balupton Dec 07, 2005 17:41
Yer but to get inside of the
case "bye"
$var actually needs to == "bye"
All the break; does is say to the switch were finished here.
Switches are just more convient ways to do if elses
So a switch is the same as:
if($var == "hello"){
//do code
}else if ($var == "bye"){
//$var MUST == "bye" to go here
}else if($var == "test"){
//etc
}
Except switches look better, and have breaks; so if i could chuck a break in the first IF statement above, where $var == "hello", it would not continue to check the values of $var.
So see how in my previous post, i have the case "test", see how i have the break; there, that means alrite kill the switch we dont care about the rest.
If i didnt have the break; there it would continue to run and check the values of $var, so because we changed $var to = "cool" the case "cool" will pass.
The break; does not say alrite skip the next case check and go inside of it, it says alrite continue the switch where we left off.
9 yabba Dec 07, 2005 18:29
You may find [url=http://uk2.php.net/manual/en/control-structures.switch.php]this page[/url] on the php manual site worth a read ;)
It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case.
¥
10 balupton Dec 07, 2005 18:34
Exactly, it wil continue doing the statements/cases, not the actual code inside the statements/cases.
I will make a php file right now and see which one of us is right ;)
11 balupton Dec 07, 2005 18:38
OMG well thats a first.
That is totally illogical, what a wacked out language PHP is.
I've been programming in C/C#/C++/AS for years, and they handle it the way i said above :S ...... Well at least i think they do
I'm going to find me a hole and die.
Heres the code i used to prove me wrong :'(
<?php
$var = "hello";
switch($var){
case "hello":
echo $var."/hello was hit<br />";
//do stuff
case "bye":
echo $var."/bye was hit<br />";
//$var must still be bye to reach here
break;
case "test":
echo $var."/test was hit<br />";
$var = "cool";
break;
case "cool":
echo $var."/cool was hit<br />";
//if $var == "test" it would not get here because of the break
break;
case "abc":
echo $var."/abc was hit<br />";
$var = "123";
case "123":
echo $var."/123 was hit<br />";
//if $var == "abc" then it would reach here because we didnt break
break;
}
?>
12 yabba Dec 07, 2005 18:39
<?php
$var = 1;
switch($var){
case 1:
echo '1';
case 2:
echo '2';
break;
case 3:
echo '3';
}
?>
[url=http://www.waffleson.co.uk/test.php]test file[/url]
¥
*edit*
lol, no prob, I only knew because I had the same problem ;)
13 balupton Dec 07, 2005 18:44
Haha yer, looks like im going back to the if elses from now on ;)
And that probz explains y a few things i've tried fell over ;)
14 blueyed Dec 07, 2005 23:43
balupton, what would be the reason to have "break" if it would have no influence?! :)
15 fplanque Dec 08, 2005 18:36
switch/case works in PHP **exactly** as it works in all other programming languages including c, c++, Java, etc.
It's is an elegant way or performing GOTOs inside of a weel defined scope.
16 balupton Dec 08, 2005 18:59
Alrite i just fiddled with switchs in C# 2005.
And again i just got put in my place ;)
Except in C# (not sure wether its the same for the other C languages)
You can do:
goto case "MyCase";
Except the case value must be a constant :(
I always thought that switches were a more convient way of doing if elses.
I wonder if instead of breaking the switch theres something you can use to break the else so it works the way i expected it too....
Oh well, u learn something new everyday.
Look on the bright side. There's no need to worry about an else condition :roll: