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.
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.
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.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do I catch a PHP Fatal Error
I have this line of PHP code:
thisFunctionDoesNotExist();
And it stops script execution with:
Fatal error: Call to undefined function
I tried using set_error_handler and it does help for warning type of errors. But not for fatal errors. As I understand it from various threads on internet it should be possible to handle by set_error_handler, but I cannot make it work.
Can you please post working example?
Note: The code above is only an example. I don't need to detect that function exists. I am setting up general application error catcher.
Fatal errors cannot be caught.
Although not an answer to your question; if you have reasons to believe that function might not be around in all cases, check with function_exists();
http://php.net/manual/en/function.function-exists.php
$functionExists = function_exists("thisFunctionDoesNotExist");
iF($functionExists)
thisFunctionDoesNotExist();
else
die("failure");
Takes a string which is your function and returns true or false