Notice: Undefined offset: 4 error HELP!

I am having an extremely hard time finding this error within my PHP code. Could anyone help me find it?

Error Message:
(notice** : Undefined offset: 4 in C:\xampp\htdocs\Assignment 4\lab.php on line 25)

Code: lab.php

1. <?php
2. require_once('dog.php');
3. function clean_input($value)
4. 	{
5. 	$bad_chars = array("{", "}", "(", ")", ";", ":", "<", ">", "/", "$");
6. 	$value = str_ireplace($bad_chars,"",$value);
7. 	// This part below is really overkill because the string replace above removed special characters
8. 	$value = htmlentities($value); // Removes any html from the string and turns it into &lt; format
9. 	$value = strip_tags($value); // Strips html and PHP tags
10. 	if (get_magic_quotes_gpc())
11. 	{
12. 	$value = stripslashes($value); // Gets rid of unwanted quotes
13. 	}
14. 	return $value;
15. 	}
16. if ((isset($_POST['dog_name'])) && (isset($_POST['dog_breed'])) && (isset($_POST['dog_color'])) &&
17. 	(isset($_POST['dog_weight'])) && (isset($_POST['dog_gender'])))
18. 	{
19. 	$dog_name = clean_input($_POST['dog_name']);
20. 	$dog_breed = clean_input($_POST['dog_breed']);
21. 	$dog_color = clean_input($_POST['dog_color']);
22. 	$dog_weight = clean_input($_POST['dog_weight']);
23. 	$dog_gender = clean_input($_POST['dog_gender']);
24. 	$lab = new Dog($dog_name,$dog_breed,$dog_color,$dog_weight,$dog_gender);
25. 	list($name_error, $breed_error, $color_error, $weight_error, $gender_error) = explode(',', $lab);
26. 	print $name_error == 'TRUE' ? 'Name update successful<br/>' : 'Name update not successful<br/>';
27. 	print $breed_error == 'TRUE' ? 'Breed update successful<br/>' : 'Breed update not successful<br/>';
28. 	print $color_error == 'TRUE' ? 'Color update successful<br/>' : 'Color update not successful<br/>';
29. 	print $weight_error == 'TRUE' ? 'Weight update successful<br/>' : 'Weight update not successful<br/>';
30. 	print $gender_error == 'TRUE' ? 'Gender update successful<br/>' : 'Gender update not successful<br/>';
31. 	// ------------------------------Set Properties--------------------------
32. 	$dog_error_message = $lab->set_dog_name($_POST["dog_name"]);
33. 	print $dog_error_message == TRUE ? 'Name update successful<br/>' : 'Name update not successful<br/>';
34. 	$dog_error_message = $lab->set_dog_weight($_POST["dog_weight"]);
35. 	print $dog_error_message == TRUE ? 'Weight update successful<br />' : 'Weight update not successful<br />';
36. 	$dog_error_message = $lab->set_dog_breed($_POST["dog_breed"]);
37. 	print $dog_error_message == TRUE ? 'Breed update successful<br />' : 'Breed update not successful<br />';
38. 	$dog_error_message = $lab->set_dog_color($_POST["dog_color"]);
39. 	print $dog_error_message == TRUE ? 'Color update successful<br />' : 'Color update not successful<br />';
40. 	$dog_error_message = $lab->set_dog_gender($_POST["dog_gender"]);
41. 	print $dog_error_message == TRUE ? 'Gender update successful<br />' : 'Gender update not successful<br />';
42. 	// ------------------------------Get Properties--------------------------
43. 	print $lab->get_dog_name() . "<br />";
44. 	print $lab->get_dog_weight() . "<br />";
45. 	print $lab->get_dog_breed() . "<br />";
46. 	print $lab->get_dog_color() . "<br />";
47. 	print $lab->get_dog_gender() . "<br />";
48. 	$dog_properties = $lab->get_properties();
49. 	list($dog_name, $dog_weight, $dog_breed, $dog_color, $dog_gender) = explode(',', $dog_properties);
50. 	print "Dog name is $dog_name. Dog weight is $dog_weight. Dog breed is $dog_breed. Dog color is $dog_color. Dog gender is $dog_gender.";
51. 	}
52. ?>							<!--php ends-->

just have a look at explode(',', $lab); with var_dump, it should be obvious which indexes are available.

I’m really curious why instantiating a new class would return errors? And why, if you are passing values in to the class to create it, you would also call a setter for it as well?

What does the dog class look like?

This is what I get when I use var dump.

"
array(4) { [0]=> string(4) “TRUE” [1]=> string(4) “TRUE” [2]=> string(4) “TRUE” [3]=> string(8) “TRUETRUE” }
Notice : Undefined offset: 4 in C:\xampp\htdocs\Assignment 4\lab.php on line 25
"
I think somehow position 3 and 4 in the array are becoming combined.

This is my dog PHP class.

1.<?php
2. class Dog
3. {
4. 	// ----------------------------------------- Properties -----------------------------------------
5. 	private $dog_weight = 0;
6. 	private $dog_breed = "no breed";
7. 	private $dog_color = "no color";
8. 	private $dog_name = "no name";
9. 	private $dog_gender = "no gender";
10. 	private $error_message = "??";
11. 	// ---------------------------------- Constructor ----------------------------------------------
12. 	function __construct($name, $breed, $color, $weight, $gender)
13. 	{
14. 	$name_error = $this->set_dog_name($name) == TRUE ? 'TRUE,' : 'FALSE,';
15. 	$breed_error = $this->set_dog_breed($breed) == TRUE ? 'TRUE,' : 'FALSE,';
16. 	$color_error = $this->set_dog_color($color) == TRUE ? 'TRUE,' : 'FALSE,';
17. 	$weight_error = $this->set_dog_weight($weight) == TRUE ? 'TRUE' : 'FALSE';
18. 	$gender_error = $this->set_dog_gender($gender) == TRUE ? 'TRUE' : 'FALSE';
19. 	$this->error_message = $name_error . $breed_error . $color_error . $weight_error . $gender_error;
20. 	}
21. 	//------------------------------------toString--------------------------------------------------
22. 	public function __toString()
23. 	{
24. 	 return $this->error_message;
25. 	}
26. 	// ---------------------------------- Set Methods ----------------------------------------------
27. 	function set_dog_name($value)
28. 	{
29. 	$error_message = TRUE;
30. 	(ctype_alpha($value) && strlen($value) <= 20) ? $this->dog_name = $value : $error_message = FALSE;
31. 	return $error_message;
32. 	}
33. 	function set_dog_weight($value)
34. 	{
35. 	$error_message = TRUE;
36. 	(ctype_digit($value) && ($value > 0 && $value <= 120)) ? $this->dog_weight = $value : $error_message = FALSE;
37. 	return $error_message;
38. 	}
39. 	function set_dog_breed($value)
40. 	{
41. 	$error_message = TRUE;
42. 	(ctype_alpha($value) && strlen($value) <= 35) ? $this->dog_breed = $value : $error_message = FALSE;
43. 	return $error_message;
44. 	}
45. 	function set_dog_color($value)
46. 	{
47. 	$error_message = TRUE;
48. 	(ctype_alpha($value) && strlen($value) <= 15) ? $this->dog_color = $value : $error_message = FALSE;
49. 	return $error_message;
50. 	}	
51. 	function set_dog_gender($value)
52. 	{
53. 	$error_message = TRUE;
54. 	($value == 'Male' || $value == 'male' || $value == 'Female' || $value == 'female') ? $this->dog_gender = $value : $error_message = FALSE;
55. 	return $error_message;
56. 	}
57. 	// ----------------------------------------- Get Methods ------------------------------------------------------------
58. 	function get_dog_name()
59. 	{
60. 	return $this->dog_name;
61. 	}
62. 	function get_dog_weight()
63. 	{
64. 	return $this->dog_weight;
65. 	}
66. 	function get_dog_breed()
67. 	{
68. 	return $this->dog_breed;
69. 	}
70. 	function get_dog_color()
71. 	{
72. 	return $this->dog_color;
73. 	}	
74. 	function get_dog_gender()
75. 	{
76. 	return $this->dog_gender;
77. 	}
78. 	function get_properties()
79. 	{
80. 	return "$this->dog_name,$this->dog_weight,$this->dog_breed,$this->dog_color,$this->dog_gender";
81. 	}
82. 	//--------------------------------------------SPEAK------------------------------------------------------
83. 	  function speak()
84. 	{
85. 	return "BARK!";
86. 	}
87. }
88. ?>

yes, that’s obvious when looking on how you set your error message codes.

What lines are you referencing when you talk about “…how I set my error message codes.”
Sorry I am very new to PHP and have looked over this document many times already but cannot figure it out.

Is it the set section of code?

Thanks.

Your list() gets its values from an explode, that gets its value from $lab, thats an instance of class Dog, that has a method __toString, that just sets together some local variables, that have a comma (or not?!)… pretty straight path, just keep that explode in mind.

1 Like

Still, do not fully understand it but I put a comma here and it seemed to fix the issue.
Thank you for your help. If you are able to explain why the final variable in $error message needed a comma that would be nice. If not that is fine. Thanks.

Line 19 in dog.php:
"
$this->error_message = $name_error . $breed_error . $color_error . $weight_error . "," . $gender_error;
"

Just compare line 16 and 17.

1 Like

Thank you. I found the missing commas and corrected it. It makes much more sense now. I appreciate your help and thank you for being patient with me.

Tip: If you ever plan to use explode, the source string should be created with implode, so you have a straight conversion path. But better use arrays.

1 Like
1. <?php
2. class Dog
3. {
4. 	// ----------------------------------------- Properties -----------------------------------------
5. 	private $dog_weight = 0;
6. 	private $dog_breed = "no breed";
7. 	private $dog_color = "no color";
8. 	private $dog_name = "no name";
9. 	private $dog_gender = "no gender";
10. 	private $error_message = "??";
11. 	// ---------------------------------- Constructor ----------------------------------------------
12. 	function __construct($name, $breed, $color, $weight, $gender)
13. 	{
14. 	$name_error = $this->set_dog_name($name) == TRUE ? 'TRUE,' : 'FALSE,';
15. 	$breed_error = $this->set_dog_breed($breed) == TRUE ? 'TRUE,' : 'FALSE,';
16. 	$color_error = $this->set_dog_color($color) == TRUE ? 'TRUE,' : 'FALSE,';
17. 	$weight_error = $this->set_dog_weight($weight) == TRUE ? 'TRUE' : 'FALSE';
18. 	$gender_error = $this->set_dog_gender($gender) == TRUE ? 'TRUE' : 'FALSE';
19. 	$this->error_message = $name_error . $breed_error . $color_error . $weight_error . $gender_error;
20. 	}
21. 	//------------------------------------toString--------------------------------------------------
22. 	public function __toString()
23. 	{
24. 	 return $this->error_message;
25. 	}
26. 	// ---------------------------------- Set Methods ----------------------------------------------
27. 	function set_dog_name($value)
28. 	{
29. 	$error_message = TRUE;
30. 	(ctype_alpha($value) && strlen($value) <= 20) ? $this->dog_name = $value : $error_message = FALSE;
31. 	return $error_message;
32. 	}
33. 	function set_dog_weight($value)
34. 	{
35. 	$error_message = TRUE;
36. 	(ctype_digit($value) && ($value > 0 && $value <= 120)) ? $this->dog_weight = $value : $error_message = FALSE;
37. 	return $error_message;
38. 	}
39. 	function set_dog_breed($value)
40. 	{
41. 	$error_message = TRUE;
42. 	(ctype_alpha($value) && strlen($value) <= 35) ? $this->dog_breed = $value : $error_message = FALSE;
43. 	return $error_message;
44. 	}
45. 	function set_dog_color($value)
46. 	{
47. 	$error_message = TRUE;
48. 	(ctype_alpha($value) && strlen($value) <= 15) ? $this->dog_color = $value : $error_message = FALSE;
49. 	return $error_message;
50. 	}	
51. 	function set_dog_gender($value)
52. 	{
53. 	$error_message = TRUE;
54. 	($value == 'Male' || $value == 'male' || $value == 'Female' || $value == 'female') ? $this->dog_gender = $value : $error_message = FALSE;
55. 	return $error_message;
56. 	}
57. 	// ----------------------------------------- Get Methods ------------------------------------------------------------
58. 	function get_dog_name()
59. 	{
60. 	return $this->dog_name;
61. 	}
62. 	function get_dog_weight()
63. 	{
64. 	return $this->dog_weight;
65. 	}
66. 	function get_dog_breed()
67. 	{
68. 	return $this->dog_breed;
69. 	}
70. 	function get_dog_color()
71. 	{
72. 	return $this->dog_color;
73. 	}	
74. 	function get_dog_gender()
75. 	{
76. 	return $this->dog_gender;
77. 	}
78. 	function get_properties()
79. 	{
80. 	return "$this->dog_name,$this->dog_weight,$this->dog_breed,$this->dog_color,$this->dog_gender";
81. 	}
82. 	//--------------------------------------------SPEAK------------------------------------------------------
83. 	  function speak()
84. 	{
85. 	return "BARK!";
86. 	}
87. }
88. ?>

the _toString method should return what you want about an object, not errors.

The object shouldn’t set errors either, it should throw them,

private function set_dog_weight($value) {
    if($value > 0 || $value < 120) {
        throw new Exception("Weight is outside of range. Weight should be between 1lbs and 120lbs.");
    }
    $this->dog_weight = $value;
}

I see you love teranary statements, but they are not necessary for everything.

if(strtoupper($value) == 'MALE' || strtoupper($value) == 'FEMALE') {
    throw new Exception("Gender is not Male or Female.");
}
$this->dog_gender = $value
function get_properties()

What is in here, should be what is in the _toString method.

A few things on naming conventions.

private $dog_weight = 0;
private $dog_breed = "no breed";
private $dog_color = "no color";
private $dog_name = "no name";
private $dog_gender = "no gender";

You are already in class Dog, so naming the properties with the prefix is redundant.
The constructor is used to set the values to their default, they don’t need to have defaults when declaring them.

Private variables tend to be prefixed with underscores,

private $_weight;

You also want to encapsulate the class methods. By default they are public, if you want them to be public, then there isn’t a point to using getters and setters. Getters and setters are used to limit access, like the weight setter method I posted - restricting access means that you will always get a valid value, if not, the class throws an error that the caller has to deal with.

2 Likes
Sponsor our Newsletter | Privacy Policy | Terms of Service