propertiesnullphp-5.3

Notice: Undefined property - how do I avoid that message in PHP?


Hello I am making this call:

$parts = $structure->parts;

Now $structure only has parts under special circumstances, so the call returns me null. Thats fine with me, I have a if($parts) {...} later in my code. Unfortunately after the code finished running, I get this message:

Notice: Undefined property: stdClass::$parts in ...

How can I suppress this message?


Solution

  • The function isset should do exactly what you need.

    PHP: isset - Manual

    Example:

    $parts = (isset($structure->parts) ? $structure->parts : false);