Jump to content

switch case


Ninjakreborn

Recommended Posts

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.
Link to comment
Share on other sites

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:

[code]<?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"
?>[/code]

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

[code]<?php
$i = "50";

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

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

  default:
     echo "no input!";
  break;
}
?>[/code]

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!
Link to comment
Share on other sites

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.

[code]
<?  
$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";
        }
}
?>
[/code]

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.