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
Post
by nokuthula » Tue May 03, 2016 9:07 pm
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;
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Tue May 03, 2016 9:12 pm
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
Post
by nokuthula » Tue May 03, 2016 9:18 pm
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
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Tue May 03, 2016 10:50 pm
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 ..."
(#10850)
nokuthula
Forum Newbie
Posts: 17 Joined: Mon May 02, 2016 10:14 am
Post
by nokuthula » Thu May 05, 2016 11:56 am
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;
}
?>
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Thu May 05, 2016 12:46 pm
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
Post
by nokuthula » Thu May 05, 2016 1:01 pm
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]
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Thu May 05, 2016 1:06 pm
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
Post
by nokuthula » Thu May 05, 2016 2:32 pm
Remember the question says I must do five cases for transport choices
How do I define this transport variable?
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Thu May 05, 2016 2:49 pm
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
Post
by nokuthula » Sun May 08, 2016 2:42 am
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;
}
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Sun May 08, 2016 6:36 am
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
Post
by nokuthula » Sun May 08, 2016 1:39 pm
OK let me try something
nokuthula
Forum Newbie
Posts: 17 Joined: Mon May 02, 2016 10:14 am
Post
by nokuthula » Mon May 09, 2016 3:54 am
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
?>