Jump to content


Photo

switch case


  • Please log in to reply
4 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 18 April 2006 - 03:36 AM

I apologize for another question. I backtracked and found somethign I didn't understand, I have tried fighting with these for awhile, the switch case statements, I am not sure how or when to supplement these, I wrote my own script, basic script to test, and mastered if statements(atleast the basics).
if
<?php
$grade = 70;
$mark = 97;
$david = 53;
$jim = 63;
$walton = 96;
$jessy = 63;
$dave = 93;
$parker = 63;
if ($mark
?>
well whatever you get the picture I can't retype all that right now, but I created it to where if you compare someones grade, with teh grade value, for instance, if there grade is above 70 or equal to, it says you passed, sort of, or if it didn't they failed. So I set all that up, and when I tried to run it to a switch case statement, THere's no way possible switch statements, are not meant to be used like that, so how are they used.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 Hooker

Hooker
  • Members
  • PipPipPip
  • Advanced Member
  • 193 posts
  • LocationWales, UK

Posted 18 April 2006 - 05:21 AM

Switch case statements are generaly used on one variable, i think the problem you have here is more down to script planning than anything, try using an array to hold the information (or a database) in a single variable that can be processed by the same function, failing that heres a couple of ways to do it:

<?php
$i = "50";   // set the value of the variable
echo ($i >= 70) ?  "passed" :   // check if the value of variable $i is above 70 and output "passed"
    (($i <= 70) ?  "failed" : "")); // check if the value of variable $i is below 70 and output "failed"
?>

although if you're trying to learn about switch case statements, heres a quick example:

<?php
$i = "50";

switch ($i) {
  case ($i <= 70):
     echo "failed";
  break;

  case ($i >= 70):
     echo "passed";
  break;

  default:
     echo "no input!";
  break;
}
?>

both examples work pretty much the same way and both written off the top of my head so excused me if ya pick out the odd miss-placed "(" :P

anyway as you can see most switch case statements will use one variable and compare its contents to pre-set values (the cases) then if it finds it it outputs everything up to the next "break" or "case" at which point it will either check the value again or stop the output, if there is no value or the value of the variable doesnt match any of the case's you can set a default output (as seen above) so you dont have a completely empty output, hope this helps!

#3 ypirc

ypirc
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 18 April 2006 - 05:24 AM

Okay, you wouldn't use a switch statement for this. Also, you wouldn't seperate -each- student into a different variable. They'd normally probably be stored in an array, or be looped one-by-one from a database connection. I'm not going to explain switches here, but would prefer if you read and reviewed the examples at [a href=\"http://www.php.net/switch\" target=\"_blank\"]http://www.php.net/switch[/a]

Here is how to perform this task using an if/else format.

<?   
$grade = 70;

$student = array('parker'=>'63', 'dave'=>'93', 'jessy'=>'63', 'walton'=>'96', 'david'=>'53');

foreach($student as $name => $mark) {
        if ($mark < $grade) {
                echo "$name failed with a $mark\n";
        }
        else {
                echo "$name passed with a $mark\n";
        }
}
?>

Output:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
%php -f c.php
parker failed with a 63
dave passed with a 93
jessy failed with a 63
walton passed with a 96
david failed with a 53
[/quote]

#4 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 18 April 2006 - 05:28 AM

I will read these tomorrow, but I also don't know arrays, yet or anything liket hat I am building up to those currently.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#5 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 18 April 2006 - 05:44 PM

perfect thanks for the help

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users