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.
Related
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.
I have used Ajax to combine Javascript and PHP (vers. 5.4) and I was confronted with an error of the simple function intval().
13: intval($_POST["request"]);
The output said:
Fatal error: Call to undefined function intval() in [...]/ajax/request.php on line 13
I have really no idea, 'cause the PHP Manual here tell me that the function must be exists.
This is in Drupal but should be applicable for normal PHP.
field-event-address is an array that is under $entity_fetched.
<?php if ($entity_fetched->field-event-address != "") echo $entity_fetched->field-event-address['und']['0']['value']; ?>
For some reason, when I do this, I get this error if I leave the ['und']['0']['value'] off:
Notice: Undefined property: stdClass::$field in eval() (line 7 of /opt/staging/contitemp/public_html/sites/all/modules/rules/modules/php.eval.inc(125) : eval()'d code).
Notice: Use of undefined constant event - assumed 'event' in eval() (line 7 of /opt/staging/contitemp/public_html/sites/all/modules/rules/modules/php.eval.inc(125) : eval()'d code).
Notice: Use of undefined constant address - assumed 'address' in eval() (line 7 of /opt/staging/contitemp/public_html/sites/all/modules/rules/modules/php.eval.inc(125) : eval()'d code).
And this error if I leave it on:
Parse error: syntax error, unexpected '[', expecting ',' or ';' in /opt/staging/contitemp/public_html/sites/all/modules/rules/modules/php.eval.inc(125) : eval()'d code on line 7
I am trying to create an if statement that checks to see if the array under the object is empty or not, and if it isn't, echos the array contents.
I'm really scratching my head with this one - I should be able to just check if an array is empty if it's under an object, correct?
- sign can't be used in variables names in PHP. Because of that you're getting syntax error (or undefined constant when PHP is trying to understand what did you mean).
Instead you can change name to $entity_fetched->fieldEventAddress (it's still readable and now it's correct).
For Drupal, you really should be using entity metadata wrappers.
So your code would be something like:
$wrapper = entity_metadata_wrapper('entity_type', $fetched_entity);
$value = $wrapper->field_name->value();
if (!empty($value)) echo $value;
You can find more information about entity metadata wrappers here:
https://drupal.org/node/1021556
<?php
function myFunction($yesNname) { }
empty($noName);
print_r($noName);
isset($noName);
myFunction($noName);
output:
PHP Notice: Undefined variable: noName ... on line 6 // print_r
PHP Notice: Undefined variable: noName ... on line 9 // myFunction
The undefined variable is used in empty() and isset(). But PHP didn't issue notice about it. Why PHP shows discrimination to some function? How can I write such type of function?
Neither isset() nor empty() are functions. As the manual explains:
this is a language construct and not a function
To get this behaviour you'd need to tweak the PHP source code written in C.
It's possible that you can also get this behaviour with a PHP extension, but you'd also need to write it in C and install it in your server.
Update:
Manual page for isset()
Manual page for empty()
Guide to the Zend Engine
Did you mean like:
if(!empty($noName)) {
// print_r
// function($noName);
}
There is no way to do it on function side. If you simply don't want to show errors you can either check variable first before calling function or use error control operator "#".
PHP 1. {main}() /Users/aaron/NetBeansProjects/PhpProject2/CssToSQL.php:0
PHP 2. GetContentRules($contentRules = *uninitialized*) /Users/aaron/NetBeansProjects/PhpProject2/CssToSQL.php:134
Line 134: $contentRuleList = GetContentRules($configSections['vips']);
function GetContentRules($contentRules) { ... }
$configSections['vips'] contains like 1114 lines
My function works as expected, but I'm trying to figure out why this error is thrown?
TIA!
Error != warning. A warning is simply an indication of something you SHOULD fix, but don't HAVE to. If it was an error, your script would bomb out at the point the error occured. Given the error message you're sort of hinting at in the question title, your $configSections['vips'] is not defined. Either $configSections itself isn't, or there's no ['vips'] key in the array.