Jump to content

Undefined Index/Undefined Variable


razzor

Recommended Posts

Hi everyone,

 

I just started learning PHP using PHP and MySQL Web Development book by Luke Welling. Here's my problem in Chap1, they talked about variables (short, medium and long notation).

 

It's my understanding that PHP 5 has turned off the register_globals inside the php.ini, but the book used the short notation. So I turned it on to complete the examples, but for some reason when I fill out my order form (using short variable notation eg. $numtires) I get an error message saying that my variables and index are undefined. Can someone tell me what I need to do to make it work?

 

Here's an example:

 

PHP Notice: Undefined index: tireqty in processorder.php on line 13 PHP Notice: Undefined index: oilqty in processorder.php on line 14 PHP Notice: Undefined index: sparkqty in processorder.php on line 15 PHP Notice: Undefined variable: OILPRICE in processorder.php on line 28

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/39653-undefined-indexundefined-variable/
Share on other sites

Actually these are not critical errors these are 'notices' that you should take care of.

Undefined index means that you are using an index in array which is not defined first.

 

Example:

echo $my_array['first'];

 

before defining $my_array['first'] before using it.

 

And same is the case with variables you are not explicitly defining them before using them.

It sounds like you're trying to access the array variables directly without first seeing if they have been set. The most common error for this is the following:

<?php
// This is not good
if ($_POST['tireqty']) {
  // do something
} 

// You should see if it is set instead
if (isset($_POST['tireqty'])) {
  // do something
}
?>

 

Without more detail about the code you're trying, we have no way to really help you, but if you will give us a little more detail, we will do what we can.

 

 

Thank you for your responses,

 

Here's the chunk of code I'm talking about.

 

<?php 
	//create short variable names
		$tireqty=$_POST['tireqty'];
		$oilqty=$_POST['oilqty'];
		$sparkqty=$_POST['sparkqty'];
	//variable declarations
		$totalqty=0;
		$totalamount=0.00;
	//constant definitions
		define ('TIREPRICE',100);
		define ('OILPRICE',10);
		define ('SPARKPRICE',4);

		$totalqty=0;
		$totalqty=$tireqty + $oilqty+$sparkqty;
		$totalamount =$tireqty*TIREPRICE
								  +$oilqty*$OILPRICE
									+$sparkqty*SPARKPRICE;

		$taxrate = 0.1; //local tax rate is 10%
		$totalamount=$totalamount * (1+$taxrate);



		echo 'isset($tireqty): '.isset($tireqty).'<br />';
		echo 'isset($nothere): '.isset($nothere).'<br />';
		echo 'empty($nothere): '.empty($nothere).'<br />';

		echo '<p>Order processed at ';
		echo date('H:i, jS F');
		echo '</p>';

		echo '<p>Your order is as follows: </p>';
		echo $tireqty.' tires<br />';
		echo $oilqty.' bottles of oil<br />';
		echo $sparkqty.' spark plugs<br />';


		echo 'Items ordered: '.$totalqty.'<br />';
		echo 'Subtotal: $'.number_format($totalamount,2).'<br />';
		echo 'Total including tax: $'.number_format($totalamount,2).'<br />';

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.