Search code examples
phpjsonrightnow-crm

Checking if an attribute value is null or empty


I am having issue in manipulating JSON data using PHP in Oracle RightNow CRM. The sample json is included below. When I try to check in an if condition, the data contained in the "person.private_email" attribute, it only evaluates to true when there is some data. If there is no data for that attribute, it does nothing. I am not getting any error at all. What would be the best way to check if any attribute contains no data.

JSON

{
    "PERSON.PERSON_ID": 272839,
    "PERSON.Surname": "FirstName",
    "PERSON.Given_Names": "LastName",
    "PERSON.TITLE": "MR",
    "PERSON.BIRTH_DT": "10/JUL/14",
    "PERSON.GENDER": "M",
    "PERSON.CDU_EMAIL": "[email protected]",
    "PERSON.PRIVATE_EMAIL": ""
}

PHP

self::$person=json_decode($json);

if (isset(self::$person->{'PERSON.PRIVATE_EMAIL'}) && !empty(self::$person->{'PERSON.PRIVATE_EMAIL'}))

Solution

  • To check whether it is null or not, isset() is enough, for checking whether it is empty or not, you better trim() the string as it removes spaces and spaces are counted as character if you check emptiness with empty() function.

    $email = self::$person->{'PERSON.PRIVATE_EMAIL'};
    if (isset($email) && trim($email) != '')
        echo "Contains data";