Undefined Variable [closed]
<?php
$name = $_GET['name'];
if ($_GET['size'] == "small")
{
$delivery = 5;
$_GET['size'] = 5;
$_GET['topping'] = 1;
if ($_GET['deliverytype'] == "pickup")
{
$total = $_GET['size'] + $_GET['topping'];
}
elseif ($_GET['deliverytype'] == "delivery")
{
$total = $_GET['size'] + $_GET['topping'] + $delivery;
}
}
echo "Dear " . $name. " your " . $_GET["size"] . " pizza has been ordered.";
echo "You Tota开发者_如何学编程l is " . $total;
?>
I am getting an error:
Notice: Undefined variable: total in C:\xampp\htdocs\process.php on line 57
Can anyone suggest, why is it so?
If $_GET['size']
is not "small", $total
will be undefined. Ditto if the size is "small", but the "deliverytype" is neither "delivery" nor "pickup".
To solve the problem, add in line 1:
$total = 0;
To write good PHP, you have to define variables first, or you will have chances to encounter null values.
I feel like doing a little bit of hand holding ;), so here's how you probably want the majority of your price calculating function to look. I can tell you're probably very new to actual programming, so... I figure it helps to see something that kind of paints the entire picture for you.
<?php
//These are some dummy variables, these 4 lines
//are pretend data that are theoretically sent form your form
$_GET['name'] = 'John';
$_GET['size'] = 'small';
$_GET['topping'] = 1;
$_GET['deliverytype'] = 'pickup';
//Typically you want to initialize all of your variables
//you're going to use at the top
$name = $_GET['name'];
$size = $_GET['size'];
$total = 0;
//All of the prices are sequentially added to the total
$total += calculate_size_cost();
$total += calculate_topping_cost();
$total += calculate_delivery_cost();
//And THEN, since the data exists, 100% certain it exists
//, this will output your desired information, with no missing variable stuff.
echo "Dear $name your {$_GET["size"]} pizza has been ordered.";
echo "You Total is $ $total";
//If the size isn't small or large, this will return 5000
//You have to EXPLICITLY state everything that can happen
//If something unexpected happens, you have to place it after
//an "ELSE"
function calculate_size_cost(){
if($_GET['size'] == "small")
return 5;
else if($_GET['size'] == "large")
return 10;
else
return 5000;
}
function calculate_topping_cost(){
//This is already a number, so, I'm assuming it's the topping price
return $_GET['topping'];
}
function calculate_delivery_cost(){
//Will add delivery cost, otherwise this will be zero if
//it's not pickup
$delivery_cost = 0;
if($_GET['deliverytype'] == "pickup"){
$delivery_cost += 5;
}
return $delivery_cost;
}
The problem lies, of course, in the fact that $total is not declared. Like any programming language, a variable must be declared before being used. PHP is unique in that setting a variable will both declare and set that variable, meaning you have one less step. So, you have three options.
In line one, do $total = "";
This will set the variable to an empty string. This is not effective, since the message will print regardless of whether or not there is a total price.
You can also make sure that EVERY code path defines $total
in some way. This will make sure that it will never be undefined. This is similar to the above answer, but helps prevent against the need to error check since it will always be set to a valid value (though error checking is still recommended).
The last, and most preferable method would be to change the last line from
code
echo "Dear " . $name. " your " . $_GET["size"] . " pizza has been ordered.";
echo "You Total is " . $total;
to something like
if(isset($total))
{
echo "Dear " . $name. " your " . $_GET["size"] . " pizza has been ordered.";
echo "You Total is " . $total;
}
else
{
echo "There was an error in your order. Please try again.";
}
This will check to see that a total value was generated before "completing" the order, which prevents the need for a large bulky change to your code or large amounts of error checking. Granted, using isset()
for error checking is not ideal, but it should work.
精彩评论