On my localhost, if I do echo $a->b
where $a
is not an object, it shows nothing. But on another server, it gives me an error of "Trying to get property of non-object". How to ignore this error? I just want a quick and easy way to make the code running, and I know what I am doing.
views:
52answers:
3
A:
try use something like
error_reporting(E_ALL & ~E_NOTICE);
in first line of your code.
heximal
2010-10-31 07:53:29
Better delete this "answer"
Col. Shrapnel
2010-10-31 08:06:33
@Col - I know what I am doing. The site just run into a tricky bug which cannot be fixed with simply adding something like `if is_object($a)`. So I need a quick fix temporarily to make the site live. I'll fix it later.
Ethan
2010-10-31 08:13:29
This one is accepted? So instead of fixing the error, you will simply ignore it? Way to go. -1 since there isn't any "stupid" option to select.
David Kuridža
2010-10-31 08:13:58
@Ethan So you've got production site that displays all errors... Indeed, you know what you are doing.
dev-null-dweller
2010-10-31 08:20:06
+1
A:
If you just want to not display this notice you have the option of turning off the notices by including this snippet at the beginning of your script:
error_reporting(0);
// or error_reporting(E_ALL & ~E_NOTICE); to show errors but not notices
ini_set("display_errors", 0);
This could also be setup globally in the server settings.
Alternatively you can ignore the notice on a specific line by using the @
symbol, like so:
echo @$a->b;
That would suppress the notice for that single statement.
The third, most 'proper', most time consuming, and most workload expensive option would be to add checks around each such code segment, to ensure it has been set prior to trying to read it, Jacob's suggestion.
dsclementsen
2010-10-31 08:06:09