Page 1 of 2
switch statement in php
Posted: Tue May 03, 2016 9:07 pm
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;
Re: switch statement in php
Posted: Tue May 03, 2016 9:12 pm
by Celauran
You'll need to post more of your code and ideally the text of the error.
Re: switch statement in php
Posted: Tue May 03, 2016 9:18 pm
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
Re: switch statement in php
Posted: Tue May 03, 2016 10:50 pm
by Christopher
So "Undefined variable" means you have not assigned a value to a variable before you use it. For example, with you code:
you set $message to the string 'Train' and then use it as a parameter for echo(). You error is "Undefined variable: transport_types ..."
Re: switch statement in php
Posted: Thu May 05, 2016 11:56 am
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;
}
?>
Re: switch statement in php
Posted: Thu May 05, 2016 12:46 pm
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?
Re: switch statement in php
Posted: Thu May 05, 2016 1:01 pm
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]
Re: switch statement in php
Posted: Thu May 05, 2016 1:06 pm
by Celauran
OK, so where's the rest of your code? $transport certainly appears to be undefined.
Re: switch statement in php
Posted: Thu May 05, 2016 2:32 pm
by nokuthula
Remember the question says I must do five cases for transport choices
How do I define this transport variable?
Re: switch statement in php
Posted: Thu May 05, 2016 2:49 pm
by Christopher
So what is returned from the function types()? Do:
Code: Select all
$transport=types("T");
print_r($transport);
Re: switch statement in php
Posted: Sun May 08, 2016 2:42 am
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;
}
Re: switch statement in php
Posted: Sun May 08, 2016 6:36 am
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.
Re: switch statement in php
Posted: Sun May 08, 2016 1:39 pm
by nokuthula
OK let me try something
Re: switch statement in php
Posted: Mon May 09, 2016 3:54 am
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
?>