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
Related
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I don't know why the code below giving an error on my laptop while not at my friend's.
<?php
function myfunction() : int {
return 10;
}
echo myfunction();
?>
Error
Parse error: syntax error, unexpected ':', expecting '{' in (my location) on line 2.
If I remove the ": int" on line 2 everything is fine, but can someone explain why this code can't run on mine?
Please read the documentation. It is a PHP7+ only feature.
What might be a good idea as a work around, until you migrate to PHP7, is to do the following:
function myfunction() {
return (int)10;
}
var_dump(myfunction());
That will convert the return to an integer.
It's worth noting, this won't throw any warnings if the return value cannot be resolved.
I.E. If you passed parameters and those parameters were letters in a string, for instance, you'd get Warning: A non-numeric value encountered. However, for now, I think the above solution will suffice.
I strongly recommend upgrading to the latest version of PHP, though.
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 5 years ago.
I'm trying to take out the game id from the database but I'm having trouble with it. And I do it like this:
if(!isset($_GET['round'])){
$currentgame=$mysql->query('SELECT `value` FROM `info` WHERE `name`="current_game"')->fetch_assoc();
$currentgame=(int)$currentgame['value'];
The error that comes up is
Fatal error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\index.php on line 85
I have no clue on what I might be doing wrong.
Without seeing all of your code, I'm assuming there's an error thrown by MySQL - possibly to do with the name="current_game"' section.
You should catch all errors when you're performing an action on a MySQL database. The error you're seeing at the moment is from MySQL returning boolean value FALSE.
Add something like this to your code:
if (!$currentgame) {
throw new Exception("Database Error [{$mysql->query->errno}] {$mysql->query->error}");
}
This should help you work out what error is being returned.
This question already has answers here:
Weird PHP error: 'Can't use function return value in write context'
(12 answers)
Closed 6 years ago.
I get the error in the subject. Also I spent ages on google and found dozens of resources having the same error, but still I can't figure out what the issue is.
This is my code:
<?php
if(empty(trim($_POST["user"])) || empty(trim($_POST["text"]))) {
echo "no luck";
}
?>
PHP Fatal error: Can't use function return value in write context in
/var/www/test.php on on line 2
If you refer to a manual, you will see
Determine whether a variable is considered to be empty.
The result of trim passed to empty is not a variable.
So your options are:
$user = trim($_POST['user']);
if (!empty($user)) { }
Or php5.5, in which
empty() now supports expressions
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 7 years ago.
if (isset( $xml->xpath('/lfm/artist/image[#size="extralarge"]')[0])) {
PHP Version 5.3 in use (was 5.7 until server dude switched it to 5.3).
On the line at the top we're getting this error:
PHP Fatal error: Can't use method return value in write context
I've read a few articles but nothing obvious springs to mind - might be a case of not seeing the wood through the trees.
Any advice on how to get rid of the error other than switching back to 5.7?
For compatibility with PHP < 5.4, update your code as follows:
$elements = $xml->xpath('/lfm/artist/image[#size="extralarge"]');
if (isset($elements[0])) {
Take a look at https://wiki.php.net/rfc/functionarraydereferencing if you're interested in learning more.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Any reason why Mage::registry(‘current_category’) would return NULL?
Reference - What does this error mean in PHP?
Fatal error: Call to a member function getParentCategory() on a non-object in...
the code:
$_category_detail=Mage::registry('current_category');
$id=$_category_detail->getParentCategory()->getId();
now, when the page can't use getParentCategory() i using the following but can't work.
if( isset(getParentCategory()){
$id=$_category_detail->getParentCategory()->getId();
}
why? thank you
It appears that $_category_detail is not an object. Therefore Mage::registry('current_category') is not returning an object.
It's most likely returning some sort of NULL or false value upon fail. And PHP is making you notice that (NULL)->getParentCategory() is meaningless.
In your particular case it returns NULL because current_category is not set in your registry.
You need to use method_exists() rather than trying to call a non-existent function:
if (method_exists($_category_detail, "getParentCategory"))
isset() only checks for member variables. Use method_exists().
PHP Manual: http://php.net/manual/de/function.method-exists.php
if (method_exists($_category_detail, 'getParentCategory')) {
$id = $_category_detail->getParentCategory()->getId()
}