switch statement in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

nokuthula
Forum Newbie
Posts: 17
Joined: Mon May 02, 2016 10:14 am

switch statement in php

Post by nokuthula »

Hello i need help again
here am required to write a switch statement of my choice that will test the value of an expression for equality and where outcome will decide what type of transport is required.it must at least 5 cases
see my code below

and it gives an error undefine variable

Code: Select all

switch ($transport_types){
            case 'A':
               
                $message='Train';
                echo $message;
                    break;
            case 'B':
                $message='Taxi';
                 echo $message;
                break;
            case 'C':
                $message='Plane';
                 echo $message;
                break;
            case 'D':
                $message='Buses';
                 echo $message;
                 break;
            case 'E':
                //default:
                $message ='invalid Transport';
                     echo $message;
                break;
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: switch statement in php

Post by Celauran »

You'll need to post more of your code and ideally the text of the error.
nokuthula
Forum Newbie
Posts: 17
Joined: Mon May 02, 2016 10:14 am

Re: switch statement in php

Post by nokuthula »

Notice: Undefined variable: transport_types in D:\xampp\htdocs\ict2613project\Task9.php on line 25

Notice: Undefined variable: transport_types in D:\xampp\htdocs\ict2613project\Task9.php on line 30

Notice: Undefined variable: transport_types in D:\xampp\htdocs\ict2613project\Task9.php on line 34

Notice: Undefined variable: transport_types in D:\xampp\htdocs\ict2613project\Task9.php on line 38

Notice: Undefined variable: transport_types in D:\xampp\htdocs\ict2613project\Task9.php on line 42
and that is all the code i have they 5 cases
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: switch statement in php

Post by Christopher »

So "Undefined variable" means you have not assigned a value to a variable before you use it. For example, with you code:

Code: Select all

$message='Train';
echo $message;
you set $message to the string 'Train' and then use it as a parameter for echo(). You error is "Undefined variable: transport_types ..."
(#10850)
nokuthula
Forum Newbie
Posts: 17
Joined: Mon May 02, 2016 10:14 am

Re: switch statement in php

Post by nokuthula »

still complaining about undefine variable function
Please someone help

Code: Select all

<?php
        
        
        $transport=types("T");
        switch($transport){
            Case"Plane":
                echo "Good Choice";
                break;
            case"Train":
                echo "Good Choice";
                case"Buses";
                    echo "Good Choice";
                    case"Taxi";
                        echo"Good Choice";
                        case"Motorbyke";
            default:
                echo"Please make a new selection";
            break;
        
            
        
        
            
        }
       
        ?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: switch statement in php

Post by Celauran »

nokuthula wrote:still complaining about undefine variable function
Please post the actual error message. They generally tell you precisely what's wrong.

You've also got semicolons after some case statements when it should be a colon. There's no types function in PHP; is that something you wrote?
nokuthula
Forum Newbie
Posts: 17
Joined: Mon May 02, 2016 10:14 am

Re: switch statement in php

Post by nokuthula »

Code: Select all

<?php
        
        
            switch ($transport){
            case 'A':
                $message ='Train';
                    break;
            case 'B':
                $message ='Taxi';
                break;
            case 'C':
                $message ='Plane';
                break;
            case 'D':
                $message='Buses';
                 break;
             case'E':
                 $message='Motorbike';
                 break;
                default:
                $message ='invalid Transport';
                break;

        
               }
[text]
Notice: Undefined variable: transport in D:\xampp\htdocs\ict2613project\Task9.php on line 20

Notice: Undefined variable: transport in D:\xampp\htdocs\ict2613project\Task9.php on line 23

Notice: Undefined variable: transport in D:\xampp\htdocs\ict2613project\Task9.php on line 26

Notice: Undefined variable: transport in D:\xampp\htdocs\ict2613project\Task9.php on line 29

Notice: Undefined variable: transport in D:\xampp\htdocs\ict2613project\Task9.php on line 32[/text]

Code: Select all

       
        ?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: switch statement in php

Post by Celauran »

OK, so where's the rest of your code? $transport certainly appears to be undefined.
nokuthula
Forum Newbie
Posts: 17
Joined: Mon May 02, 2016 10:14 am

Re: switch statement in php

Post by nokuthula »

Remember the question says I must do five cases for transport choices
How do I define this transport variable?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: switch statement in php

Post by Christopher »

So what is returned from the function types()? Do:

Code: Select all

        $transport=types("T");
        print_r($transport);
(#10850)
nokuthula
Forum Newbie
Posts: 17
Joined: Mon May 02, 2016 10:14 am

Re: switch statement in php

Post by nokuthula »

Hello Christopher

i have tried to declare a variable transport,when am running it ,it pulls blank page now

Code: Select all

$transport = "transport";
        
        
        switch ($transport){
            case 'A':
                $message ='Train';
                    break;
            case 'B':
                $message ='Taxi';
                break;
            case 'C':
                $message ='Plane';
                break;
            case 'D':
                $message='Buses';
                 break;
             case'E':
                 $message='Motorbike';
                 break;
                default:
                $message ='invalid Transport';
                break;

        
               }
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: switch statement in php

Post by Celauran »

Your switch statement is expecting $transport to be one of A, B, C, D, or E. You have instead assigned it a value of 'transport'. The default case will then be triggered, assigning 'Invalid transport' to the variable $message. That's all that bit of code does. You aren't doing anything with $message. There are no echo or print statements, so nothing being displayed is expected behaviour.
nokuthula
Forum Newbie
Posts: 17
Joined: Mon May 02, 2016 10:14 am

Re: switch statement in php

Post by nokuthula »

OK let me try something
nokuthula
Forum Newbie
Posts: 17
Joined: Mon May 02, 2016 10:14 am

Re: switch statement in php

Post by nokuthula »

Code: Select all

$transport = "transport";
        
        
        switch ($transport){
            case 'A':
                $transport ='Train';
                    break;
                
            case 'B':
                $transport ='Taxi';
                break;
            case 'C':
                $transport ='Plane';
                break;
            case 'D':
                $transport='Buses';
                 break;
             case'E':
                 $transport='Motorbike';
                 break;
                default:
                $transport ='invalid Transport';
                break;

        
               }
        
            
               echo $transport
            
        
       
        ?>
Post Reply