Page 1 of 1

Getting index errors even though script works...why?

Posted: Sat Mar 13, 2010 4:56 pm
by Joeam
Greetings,

I'm a PHP n00b and am writing the following script for class. That being said, I've gotten 99% of the function out of the script that I want, but keep getting the following errors when accessing the page:

Notice: Undefined index: number1 in beer.php on line 6

Notice: Undefined index: number2 in beer.php on line 7

Notice: Undefined index: number3 in beer.php on line 8

which I can clear up by including:
$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);

but I'd like to know a)why they appear even if the script works and b)how can I get rid of them legitimately.

My script is below; any help you can offer (the simpler the better!) would be greatly appreciated.

<?php
$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);

$a = $_POST["number1"];
$b = $_POST ["number2"];
$c = $_POST["number3"];
if (!isset($_POST['submit'])){


?>
<html>
<head>
<title>Beer $tore</title>
</head>
<body>
<div>

<h2>Welcome. Order a few drinks!</h2></div>
<div> <u>All prices are in Ameros</u> </div> <br />

<form action="beer.php" method="post">


<b>Arrogant Bastard</b> by Stone Brewing Co. <b>8.75/ea.</b><br>

How many? <input type = "text" name="number1" type="text" size="2" /> <p>
<b>Storm King</b> by Victory Brewing Co. <b>9.25/ea.</b><br>

How many? <input type ="text" name="number2" type="text" size="2" /> <p>
<b>Skull Splitter</b> by Orkney Brewery <b>11.50/ea.</b> <br>

How many? <input type ="text" name="number3" type="text" size="2" /> <p>
<input type="submit" name="submit" value="Calculate your total" />
</form>
<div> *Tax rate of 6.35% applies to all orders. <br /><br /></div>
<div><b>**If the total number of beers you order matches the random number of the moment, your order is free! Good luck!!</b></div>
</body></html>

<?php

} else {

$beer_num = $a + $b + $c;
$cost = ($a * 8.75) + ($b * 9.25) + ($c * 11.50);
$tax = .0635;
$tax = $tax + 1;
$total = $cost * $tax;
$total = number_format ($total, 2);


print '<p><h2><u>Checkout Confirmation</u></h2></p>';
print "<p>You have ordered <b> $beer_num </b> beers for a total cost of $total Ameros. <b>Pay up</b>.</p>";
$n1 = rand (1, 25);
print "<p>The random number of the moment is <b>$n1</b>.</p>";
if ($n1 == $beer_num) {
print '<p><b>YOU WON! Enjoy your free beer.</b></p>';
} else {
print '<p><b>No</b> free beer for you. Better luck next time!</p>';
}
}


?>

</body></html>

Thanks for your help and your time!

Re: Getting index errors even though script works...why?

Posted: Sat Mar 13, 2010 5:48 pm
by McInfo
The "Notice: Undefined index" error occurs when you try to access a member of an array that does not exist in that array. If a user visits your page without submitting the form (which is typical for the first visit) the $_POST array is not populated. Therefore, the "number1", "number2", and "number3" members do not exists. In other words, those indexes are undefined.

You can avoid the error by testing whether the array members are "set" before attempting to access them. One way to do this is to use isset().

Code: Select all

if (isset($_POST['number1'])) {
    $a = $_POST['number1'];
} else {
    $a = null;
}
A shorthand equivalent for this if-else structure is the ternary operator.

Code: Select all

$a = isset($_POST['number1']) ? $_POST['number1'] : null;
isset() does not trigger the "undefined index" error because it is a language construct, not a function, and it is designed to not trigger the error.

You can test multiple variables at once with isset() by using a comma-separated list. In this case, isset() will return true only if all of the tested variables are set.

Code: Select all

isset($_POST['number1'], $_POST['number2'])
Another option is to test whether a specific key exists in the $_POST array.

Code: Select all

if (array_key_exists('number1', $_POST))
Another option is to test whether the member is not "empty". The isset() test is included in the tests that empty() performs, so it would be redundant to use both.

Code: Select all

if (! empty($_POST['number1']))
Be sure to read the documentation because these methods are not identical.

Edit: This post was recovered from search engine cache.