why the following error is showing? [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
the following error is showing in the web page but the code is working..i just want to know why the following error is showing?
Notice: Use of undefined constant food - assumed 'food' in E:\server\htdocs\table\action.php on line 33
enter image description here
the following links are the code source.
https://drive.google.com/open?id=1u59Z0WipMgPEE10KP1c12maoUS-KqhlA
https://drive.google.com/open?id=1ngHyhwOdCbryj7UvZ_8tE2pIqp2uoSmE

Even without the code sample where it is happening I believe you are not using quotations ' or " in your array key names.
All you need is to wrap the key name into the quotations such as:
$your_array['food'];
// or
$your_array["food"];

For example on 10 line $dep = $_POST['dep']; I think you get text value. And on 33 line you have if-else operator if($dep==food){. You need to change your right value food to 'food'. Then you haven't get Notice messages. Because all text values must be in single or double quotes.
Your current syntax (with notice) is: comparing variable $dep with constant food.

Related

how do I make a php variable successfully available in another php file? [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 2 years ago.
I have 2 php files inside joomla and I want to make a variable from php-file 1 available in php-file 2.
php-file_1.php
$a_variable = $input->getString('text');
php-file_2.php
include(php-file_1.php);
echo $a_variable;
I get Notice: Undefined variable: a_variable in /var/www/vhosts/a_domain.de/httpdocs/php-file_2.php on line 2
what's wrong here?
include and require are not functions, they are language constructs. Therefore they need to be used without brackets.
Either way, you missed quotes around php-file_1.php.
include 'php-file_1.php'; should work fine.

Undefined offset when trying to get the next position in php [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
error message when trying to print the next position in loop?
$coba = "testing";
for($h=1;$h<(count($coba));$h++){
echo $coba[$h+1];
}
If this is your code, you would get that error:
$coba = "testing";
for($h=1;$h<strlen($coba);$h++){
echo $coba[$h+1];
}
The reason is you're trying to print something that don't exist.
You are incrementing $h to a number greater than the array. That's why you are getting the offset error.
Original code is incorrect here --> for($h=1;$h<(**count**($coba));$h++){
See the PHP reference guide for difference in count() and strlen().
count($coba) is 1
strlen($coba) is 7
If you use strlen then the code would correctly loop through the string:
$coba = "testing";
for($h=1;$h<strlen($coba);$h++){
echo $coba[$h+1];
}
Now, regarding the error you mentioned, when I run the above I get:
PHP Notice: Uninitialized string offset: ...
Two problems here with your original code. Since I do not know your original intent, I can only guess at what you were trying to do with the loop.
Problem #1: indexing into the string should start with 0, not 1
Problem #2: $coba[$h+1] will be an invalid index at the END of the array at h+1.
You can either adjust the indexing h+1 to just be h OR
change the loop to loop 1 less via for($h=0;$h<(strlen($coba)-1);$h++){
Thus, final code could look like:
$coba = "testing";
for($h=0;$h<(strlen($coba)-1);$h++){
echo $coba[$h+1];
}
Which outputs when run:
esting

Is seeing Notice in the HTML of any concern for PHP? [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I went to view page source and seen this in the HTML for a select drop-down list that is populated by a query:
Notice: Constant DB_USER already defined in C:\xampp\htdocs\bookhippie\mysqli_connect.php on line 8
Notice: Constant DB_PASSWORD already defined in C:\xampp\htdocs\bookhippie\mysqli_connect.php on line 9
Notice: Constant DB_HOST already defined in C:\xampp\htdocs\bookhippie\mysqli_connect.php on line 10
Notice: Constant DB_NAME already defined in C:\xampp\htdocs\bookhippie\mysqli_connect.php on line 11
Should I be concerned about this? And if so, what should I be looking for in the PHP?
Thanks!
Yes, you should always worry over notices being generated by your code. PHP is trying to tell you that it thinks you've done something wrong, but can't be sure what it is.
In your case, I'm assuming you're using require instead of require_once.
You should always strive to write code that produces zero errors including notices. This will help you prevent unexpected behavior and make sure PHP is performant.
In your case you either are including a file multiple times or are trying to define a constant multiple times. In the first case you need to switch from include()/require() to include_once()/require_once(). In the latter you need to check if the constant is defined before trying to define it again.
defined('DB_USER') || define('DB_USER', 'your value');

Undefined error $_POST index [duplicate]

This question already has answers here:
if(!isset($_POST["user"]) ignored and returns Undefined Index
(4 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
Can someone please explain what does undefined error means on this link?
Whenever i visit my forum page it does gives me this error undefined index fpost.
fpost is the text area holder for the message. Obviously there is still no content in fpost when you initially visit the page, i don't know why it's propmting an error i already have if( isset($_POST['fpost']) && $_POST['fpost']!=='' ) condition to eliminate the error but it's not working...
I haven't even click the post button to begin with
Notice: Undefined index: fpost in ... on line 231
I don't understand the words people are using, can you please explain it in laymans term where beginners will understand. Thanks in advance.
Make sure that you have set name="fpost" for text area holder for the message.
If you didn't it will through undefined variable error.

What is the meaning of the "Use of undefined constant" notice in PHP? [duplicate]

This question already has answers here:
What does the PHP error message "Notice: Use of undefined constant" mean?
(2 answers)
Closed 9 years ago.
Notice: Use of undefined constant username - assumed 'username' in
/home/content/04/7195304/html/header.php on line 54
I get this when writing things like $_COOKIE[username] or $_POST[username].
Edit
So I've been playing around with the code, putting quotes in my POST, COOKIE, and GET's.. I still get the same thing!
It means you likely forgot a $ in front of your variable name.
Edit
You need to encapsulate your call in qoutes. I.e.
$_COOKIE["username"]
or
$_POST["username"]
It probably means you forgot to put a $ in front of your username variable, so it's treating it like a constant instead of a variable.
You should post the code from that line for better help.
You might as well try $_COOKIE['username'] or $_POST['username'], to access the associative arrays with a string.
Sorry, overlooked comment with same advice.

Categories