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 wan开发者_运维知识库t a quick and easy way to make the code running, and I know what I am doing.
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.
try use something like
error_reporting(E_ALL & ~E_NOTICE);
in first line of your code.
check the php.ini, in the section of errors.
精彩评论