This question already has answers here:
What does the PHP error message "Notice: Use of undefined constant" mean?
(2 answers)
Closed 8 years ago.
I'm only a beginner with PHP and server side programming so excuse me for this question about variables. I was reading a tutorial about uploading a photo.
(tutorial on plus2net)
and like described in the tutorial i directly copied and pasted the code after understanding it. However the webpage filled with following errors after i inserted PHP in to the HTML.
Notice: Use of undefined constant size - assumed 'size' in C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 44
Notice: Undefined index: file_up in C:\Program Files
(x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 44
Notice: Use of undefined constant file_up - assumed 'file_up' in
C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on
line 45
Notice: Use of undefined constant name - assumed 'name' in C:\Program
Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 45
Notice: Undefined index: file_up in C:\Program Files
(x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 45
Notice: Use of undefined constant file_up - assumed 'file_up' in
C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on
line 47
Notice: Use of undefined constant size - assumed 'size' in C:\Program
Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 47
Notice: Undefined index: file_up in C:\Program Files
(x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 47
Notice: Use of undefined constant file_up - assumed 'file_up' in
C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on
line 51
This is the PHP Code:
<?Php
$file_upload="true";
$file_up_size=$_FILES['file_up'][size];
echo $_FILES[file_up][name];
if ($_FILES[file_up][size]>500000){
$msg=$msg."Uploaded file size more than 500KB<BR>";
$file_upload="false";}
if (!($_FILES[file_up][type] =="image/jpeg" OR $_FILES[file_up][type] =="image/png")){
$msg=$msg."Your uploaded file must be of JPG or PNG.<BR>";
$file_upload="false";}
$file_name=$_FILES[file_up][name];
$add="upload/$file_name"; //the path with the file name
if($file_upload=="true"){
if(move_uploaded_file ($_FILES[file_up][tmp_name], $add)){
//do your coding here to give a thanks message
}
else{
echo "Failed to upload file.";}
}
else{
echo $msg;
}
?>
I've corrected the variable error but I'm getting this error now instead
Notice: Undefined index: file_up in C:\Program Files
(x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 44
Please guide me through this. :)
Where you have
$file_up_size=$_FILES['file_up'][size];
size is being interpreted as a constant due to the lack of a dollar sign.
What you want is
$file_up_size=$_FILES['file_up']['size'];
The problem is that you are relying on bare strings being interpreted as such. Normally, in PHP code, this is considered a constant:
echo FOO;
If you don't define that constant first, like this:
define('FOO', 'foo');
You will encounter this error and PHP will interpret it as a bare string and echo FOO. This is a bad choice. I think what you mean to do is use actual strings (rather than undefined constants) as your array indices:
if ($_FILES['file_up']['size']>500000){
// etc..
Which is what is recommended.
Note: Bare strings are covered in the manual entry for Arrays, specifically underneath the section Arrays do's and don'ts (the manual says don't do this).
This is wrong, but it works. The reason is that this code has an
undefined constant (bar) rather than a string ('bar' - notice the
quotes). PHP may in future define constants which, unfortunately for
such code, have the same name. It works because PHP automatically
converts a bare string (an unquoted string which does not correspond
to any known symbol) into a string which contains the bare string. For
instance, if there is no defined constant named bar, then PHP will
substitute in the string 'bar' and use that.
Related
I use the callback function without quotation marks, although it returns an error, it can be executed normally.
function fc($v) {
echo $v + 1;
}
$a = [2, 4];
array_map(fc, $a); // <-- this works! notice how fc is not a string.
// output: 35
Although it seems useless, I want to know why it works.
PHP converts undefined constants to strings automagically.
If you let PHP execute this:
echo my_undefined_constant . " is a " . gettype(my_undefined_constant);
PHP will complain, a lot, but eventually my_undefined_constant will be the string "my_undefined_constant":
Warning: Use of undefined constant my_undefined_constant - assumed 'my_undefined_constant' (this will throw an Error in a future version of PHP) in php shell code on line 1
Call Stack:
136.9851 395312 1. {main}() php shell code:0
Warning: Use of undefined constant undefined_constant - assumed 'undefined_constant' (this will throw an Error in a future version of PHP) in php shell code on line 1
Call Stack:
136.9851 395312 1. {main}() php shell code:0
my_undefined_constant is a string <---
But, as a result of undefined constants being converted to string literals, this will work:
php > echo call_user_func(strlen, 'test123');
Warning: Use of undefined constant strlen - assumed 'strlen' (this will throw an Error in a future version of PHP) in php shell code on line 1
Call Stack:
258.5445 395240 1. {main}() php shell code:0
7 <---
This behavior is obviously highly questionable, and that's why implicitly converting undefined constants to string literals has been deprecated since PHP 7.2.
Its php feature that for warning, notice errors it displays them but continue executing script.
In similar manner though it has thrown error(warning) array_map function did its job.
I am getting hundreds of from ANSI.php. Here is an example:
/usr/share/pear/File/ANSI.php on line 553 Notice: Undefined offset: 75 in
/usr/share/pear/File/ANSI.php on line 555 Notice: Undefined offset: 76 in
/usr/share/pear/File/ANSI.php on line 553 Notice: Undefined offset: 76 in
/usr/share/pear/File/ANSI.php on line 555 Notice: Undefined offset: 77 in
/usr/share/pear/File/ANSI.php on line 555 Notice: Trying to get property of non-object in
/usr/share/pear/File/ANSI.php on line 496 Notice: Trying to get property of non-object in
This is generating from:
$ansi->appendString($ssh->read());
Everything is working. I suspect the old machines I am working with are giving ANSI.php a hard time.
Is there a way i can disable just the error messages from ANSI.PHP and keep the others? Unless someone has a way of fixing the error.
Simplest Solution (Sub-Optimal)
I guess the simplest way to suppress the errors would be to use the error suppression operator #. eg.
#$ansi->appendString($str);
Optimal Solution (Possibly)
There have been two commits to phpseclib since the latest release (1.0.7 and 2.0.6, as of this post) that fixed issues with File/ANSI.php:
https://github.com/phpseclib/phpseclib/commit/5c792f6bc1fa8a5d26b43fb8200191c073637e15
https://github.com/phpseclib/phpseclib/commit/84d1628cb7734134b1ba80545b38985025942b79
More info:
https://github.com/phpseclib/phpseclib/issues/1161
https://github.com/phpseclib/phpseclib/issues/1150
Kinda makes me wonder if one of those might fix the issue for you.
Fallback to Optimal Solution
If the previously mentioned "optimal solution" doesn't fix the issue for you then it would be nice to fix the problem at the source. What'd help me do that would be a copy of the data you got before you passed it to $ansi->appendString(). To make it so the characters don't get garbled because they're extended ASCII maybe hex encode it. eg. echo bin2hex($ssh->read()); or something.
This question already has answers here:
What does the PHP error message "Notice: Use of undefined constant" mean?
(2 answers)
Closed 9 years ago.
$CONFIG[dbserver] = 'localhost';
$CONFIG[dbuser] = 'test';
$CONFIG[dbpass] = 'rehrsdfvseg'
$CONFIG[dbname] = 'test';
I get this error, what can I do?
[20-Feb-2014 19:32:20] PHP Notice: Use of undefined constant dbserver - assumed 'dbserver' in /home/knul/public_html/porr/setup/config.php on line 2
[20-Feb-2014 19:32:20] PHP Notice: Use of undefined constant dbuser - assumed 'dbuser' in /home/knul/public_html/porr/setup/config.php on line 3
[20-Feb-2014 19:32:20] PHP Notice: Use of undefined constant dbpass - assumed 'dbpass' in /home/knul/public_html/porr/setup/config.php on line 4
[20-Feb-2014 19:32:20] PHP Notice: Use of undefined constant dbname - assumed 'dbname' in /home/knul/public_html/porr/setup/config.php on line 5
There is two possibilities.
Or your configuration are in array, and so you need to quote around the keys like this $CONFIG['dbserver']
Or dbserver etc are PHP constants, defined earlier with define function.
I am new to drupal theme . I have created a direcory mytheme and added mytheme.info to it and copied other files from drupal's default theme directory . Now after editing the page.tpl.php, drupal is showing the errors below .
Notice: Undefined variable: hide_site_name in include() (line 99 of C:\wamp\www\dtest\sites\all\themes\mytheme\bartik\templates\page.tpl.php).
Notice: Undefined variable: hide_site_name in include() (line 109 of C:\wamp\www\dtest\sites\all\themes\mytheme\bartik\templates\page.tpl.php).
Notice: Undefined index: featured in include() (line 168 of C:\wamp\www\dtest\sites\all\themes\mytheme\bartik\templates\page.tpl.php).
Notice: Undefined index: highlighted in include() (line 187 of C:\wamp\www\dtest\sites\all\themes\mytheme\bartik\templates\page.tpl.php).
Notice: Undefined index: sidebar_second in include() (line 212 of C:\wamp\www\dtest\sites\all\themes\mytheme\bartik\templates\page.tpl.php).
Notice: Undefined index: triptych_first in include() (line 220 of C:\wamp\www\dtest\sites\all\themes\mytheme\bartik\templates\page.tpl.php).
Notice: Undefined index: triptych_middle in include() (line 220 of C:\wamp\www\dtest\sites\all\themes\mytheme\bartik\templates\page.tpl.php).
Notice: Undefined index: triptych_last in include() (line 220 of C:\wamp\www\dtest\sites\all\themes\mytheme\bartik\templates\page.tpl.php).
Notice: Undefined index: footer_firstcolumn in include() (line 230 of C:\wamp\www\dtest\sites\all\themes\mytheme\bartik\templates\page.tpl.php).
Notice: Undefined index: footer_secondcolumn in include() (line 230 of C:\wamp\www\dtest\sites\all\themes\mytheme\bartik\templates\page.tpl.php).
Notice: Undefined index: footer_thirdcolumn in include() (line 230 of C:\wamp\www\dtest\sites\all\themes\mytheme\bartik\templates\page.tpl.php).
Notice: Undefined index: footer_fourthcolumn in include() (line 230 of C:\wamp\www\dtest\sites\all\themes\mytheme\bartik\templates\page.tpl.php).
After searching on google, I have found that clearing cache will solve the problem . But even after clearing my cache,it remains the same !
Generally these errors occurred when you call a region in your page.tpl.php file that doesn't exist in the theme's .info file.
In your page.tpl.php:
$page['footer_firstcolumn'];
In your theme's .info:
regions[footer_firstcolumn] = Footer first column
After rechecking all regions, don't forget to flush the cache.
If you want to create a fresh theme best practice is to use something like Zen. It's blank and fully customizable.
As long as you follow the prescribed instructions, you will avoid nasty errors like the ones you have above
i had almost the same problem of messages saying "Notice: undefined index: myIndex in include() (line n in some/path/myPage.tpl.php)" whe i was coding my custom sub-template
I kind of solved the problem using php's function isset() for every line told in the Notice message.
For example in your line 99 i would use:
if(isset(hide_site_name)){
//use hide_site_name in the normal way
}
or in your line 168
if(isset( some_var[featured] )){
//use "featured" index in the normal way
}
hope this helps someone as this helped me after a long time searching for a solution.
i never found the cause of this behavior btw.
sorry for bad grammar, if there's any.
I had the same problem after creating a sub theme and I followed the answer by Meiker and it worked great about including isset. But as I do not have a lot of experience with programing I ran into a snag with line 220 and multiple triptych in the same line.
Notice: Undefined index: triptych_first in include() (line 220 of
C:\wamp\www\dtest\sites\all\themes
So I added isset like this:
if(isset($page['triptych_first']) || (isset($page['triptych_middle']) || (isset($page['triptych_last'])))) :
and did the same for similar error lines and now I do not have any more errors appearing.
I hope this helps others who are programming challenged.
I am working with my website, and have just have completed it successfully. This website is for payment process. Everything works fine even though am getting errors. What can I do to fix them?
Here are the errors:
Notice: Constant FIRSTNAME already defined in E:\wamp\www\david\include\variables.php on line 42
Notice: Constant LASTNAME already defined in E:\wamp\www\david\include\variables.php on line 43
Notice: Constant EMAIL already defined in E:\wamp\www\david\include\variables.php on line 44
Notice: Constant ADDRESS1 already defined in E:\wamp\www\david\include\variables.php on line 45
Notice: Constant ADDRESS2 already defined in E:\wamp\www\david\include\variables.php on line 46
Notice: Constant CITY already defined in E:\wamp\www\david\include\variables.php on line 47
Notice: Constant POSTCODE already defined in E:\wamp\www\david\include\variables.php on line 48
it comes untill the 63 lines..
How can I fix this?
Your are just including the file twice where these constants are defined.
Check and include them once.
if you are including the file use
require_once('constants.php')
instead of include()
The code is trying to re-define constants somewhere, an action that is not allowed (hence the name "constants").
My guess is that the same file is getting included more than once somewhere. Look through your code to find out what file defines those constants, and then find out what places it's included.
You should also use include_once instead of include. include_once checks to make sure it hasn't been included already. See the manual here:
http://php.net/include-once
Check to see if you are including the same thing twice in your .php page.
you can turn off error reporting at run time using error_reporting(0);