Error Notice Undefined variable Notice Undefined index and Notice Undefined offset using PHP

0 votes

I'm running a PHP script and continue to receive errors like:

Notice: Undefined variable: my_variable_name in C:\wamp\www\niraj\index.php on line 18

Notice: Undefined index: my_index C:\wamp\niraj\mypath\index.php on line 19

Line 18 and 19 looks like this:

echo "My variable value is: " . $my_variable_name;
echo "My index value is: " . $my_array["my_index"];

What is the meaning of these error messages?

How to fix this?

Apr 1, 2020 in PHP by kartik
• 37,520 points
37,586 views

1 answer to this question.

0 votes

Hello @kartik,

The best way for getting input string is:

$value = filter_input(INPUT_POST, 'value');

This one-liner is almost equivalent to:

if (!isset($_POST['value'])) {
    $value = null;
} elseif (is_array($_POST['value'])) {
    $value = false;
} else {
    $value = $_POST['value'];
}

If you absolutely want string value, just like:

$value = (string)filter_input(INPUT_POST, 'value');

Hope this is helpful!!

Thank You!!

answered Apr 1, 2020 by Niroj
• 82,840 points
Thanks for the Solution

Related Questions In PHP

0 votes
1 answer

Notice: Undefined index: bid in C:\xampp\htdocs\userac\courier_management_system-master\courier_management_system-master\addstaff.php on line 130

Hello @saima , Before you extract values from $_POST ,$_SESSION, ...READ MORE

answered Sep 14, 2020 in PHP by Niroj
• 82,840 points
2,872 views
0 votes
1 answer

PHP undefined key and variable

Hi, @Dann, If you are using some variable ...READ MORE

answered Dec 30, 2020 in PHP by anonymous
• 65,770 points
45,700 views
0 votes
0 answers

PHP Notice: Undefined offset: 1 with array when reading data

I am getting this error : PHP Notice: ...READ MORE

Jun 9, 2022 in PHP by Kichu
• 19,040 points
1,350 views
0 votes
0 answers

PHP - Notice: Undefined index

I wanted to do a registration form ...READ MORE

Jun 9, 2022 in PHP by Kichu
• 19,040 points
1,720 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,080 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,016 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,948 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,884 views
0 votes
1 answer

Error:PHP Notice: Undefined offset: 1

Hello @kartik, Change $data[$parts[0]] = $parts[1]; to if ( ! isset($parts[1])) ...READ MORE

answered Oct 20, 2020 in PHP by Niroj
• 82,840 points
7,686 views
0 votes
1 answer

Error:Undefined index: barangay in C:\xampp\htdocs\TextBlast\homepage\insert.php on line 22

Hello @ Regina, First checks whether a variable is ...READ MORE

answered Aug 14, 2020 in PHP by Niroj
• 82,840 points
5,820 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP