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
23,496 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,800 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 13, 2020 in PHP by Niroj
• 82,800 points
1,071 views
0 votes
1 answer

PHP undefined key and variable

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

answered Dec 29, 2020 in PHP by anonymous
• 65,950 points
17,096 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,800 points
716 views
0 votes
1 answer

Invalid argument supplied for foreach() on line 25. Error in my php and mysql

Hello @viven , This should be a simple fix ...READ MORE

answered Aug 4, 2020 in PHP by Niroj
• 82,800 points
878 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,800 points
9,370 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,800 points
1,040 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,800 points
839 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,800 points
10,432 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,800 points
2,319 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,800 points
2,553 views