Help with PHP error "Undefined index"


#1

I'm starting to learn php and running into an error with some of my code. Im working on a simple calculator and getting an error message from xdebug "Undefined index".

When i input some numbers into the input fields the error goes away.

I'm running a PHP server locally through a program called "Uniform Server" running the latest PHP and Xdebug installed locally also.

Here is the error:

And the code I'm using:

<?php

class Calculator 
{
	public function Addition($num1, $num2) 
	{
		return $num1 + $num2;
	}
	
	public function Subtraction($num1, $num2) 
	{
		return $num1 - $num2;
	}
	
	public function Multiplication($num1, $num2) 
	{
		return $num1 * $num2;
	}
	
	public function Division($num1, $num2) 
	{
		return  $num1 / $num2;
	}
	
}

$calculator = new Calculator();

$choice = $_POST['choice'];
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];

if(isset($_POST['submit']))
{
	if(is_numeric($num1) && is_numeric($num2))
	{
		switch($choice) 
		{
		case "addition":
			echo "<h3>" . $num1 . " + " . $num2 . " = " . $calculator->Addition($num1,$num2) . "</h3>";
			break;
		
		case "subtraction":
			echo "<h3>" . $num1 . " - " . $num2 . " = " . $calculator->Subtraction($num1,$num2) . "</h3>";
			break;
		
		case "Multiplication":
			echo "<h3>" . $num1 . " * " . $num2 . " = " . $calculator->Multiplication($num1,$num2) . "</h3>";
			break;
		
		case "division":
			echo "<h3>" . $num1 . " / " . $num2 . " = " . $calculator->Division($num1,$num2) . "</h3>";
			break;
			
		default:
			echo "Please pick a choice";
		}
	}
	else 
	{
		echo "Please enter only numeric numbers";
	}	
}
?>
<html>
<head>
	<title>Simple Calculator</title>	
</head>
<body>
	<h1>Simple Calculator</h1>
	
	<form action="index.php" method="POST">
		<table>
			<tr>
				<td>First Number: <input type="text" name="num1" /></td>			
			</tr>		
			<tr>
				<td>Second Number: <input type="text" name="num2" /></td>			
			</tr>	
			<tr>
				<td>Select A choice</td>			
			</tr>	
			<tr>
				<td>Addition<input type="radio" name="choice" value="addition" /></td>			
			</tr>		
			<tr>
				<td>Subtraction<input type="radio" name="choice" value="subtraction"/></td>			
			</tr>		
			<tr>
				<td>Multiplication<input type="radio" name="choice" value="multiplication" /></td>			
			</tr>		
			<tr>
				<td>Division<input type="radio" name="division" /></td>			
			</tr>				
			<tr>
				<td><input type="submit" name="submit" value="Submit" /></td>			
			</tr>		
		</table>	
	</form>	
</body>
</html>

I appreciate any help anyone can give me.


#2

$choice = $_POST['choice'];
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];

If the page doesnt have any post data, then $POST['choice'] is undefined. So is $POST['num1'] and $_POST['num2']. Try sticking those three lines inside the

if(isset($_POST['submit']))
{
//Put them here
    if(is_numeric($num1) && is_numeric($num2))
    {

#3

Ok, thank you. So what does the error exactly mean. Do i have undefined variables?

Should i define those variables in advance and set them to null next time?


#4

gets out crayons smiley

Undefined index means that the array you're refererncing doesnt have an entry at the key you specified.

$choice = $_POST['choice'];

So you're looking into your array for a certain key. If that key doesnt exist, you get an Undefined Index error. (Which might as well be called an Undefined Key error)

You should only need to pre-define your variables if you're going to use them whether-or-not something else sets them later on in the page. (In your case, you only use num1, num2, or choice when the post data is set, so you dont need to)


#5

To add to Star's post, to solve this problem change the following code:

//...

$calculator = new Calculator();

$choice = $_POST['choice'];
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];

if(isset($_POST['submit']))
{
    if(is_numeric($num1) && is_numeric($num2))
    {
        switch($choice) 
        {

// ...

to:

//...

$calculator = new Calculator();

if(isset($_POST['submit']))
{
    $choice = $_POST['choice'];
    $num1 = $_POST['num1'];
    $num2 = $_POST['num2'];

    if(is_numeric($num1) && is_numeric($num2))
    {
        switch($choice) 
        {

// ...


#6

Get used to seeing and swiftly interpreting these messages smile

It either means that the array is not set somewhere (as adequately described above) or the exact key you are interrogating or accessing somewhere is not set, or, most likely, you misspelt the key.

// given:
$a[1] = 'this';
$a[2] = 'that';
$a['me'] = 'Cups';

// these throw the somewhat similarly worded errors:

echo $a[0]; // Notice: Undefined offset: 0
echo $a[3]; // Notice: Undefined offset: 3
echo $a['Me']; // Notice: Undefined index: Me
echo $b[0]; // Notice: Undefined variable: b

+1 for using xdebug and running with Showing Notices btw wink