PHP

PHP property_exists

Property_exists() is a built-in function in PHP that is used to check the existence of a property in a class or object.

The property_exists() function gives a result in a Boolean value that means true or false, even if the value is null the property_exists function will return true.

Syntax

property_exists(object, property);

Parameters

There are 2 parameters in the property_exists() function first one is the “object” or “class” name in which you want to check the existence of your property and the other one is “property” which is the property name that you want to check, is it exists or not.

Example 1

<?php

$ob = (object) array('adward' => 4, 'ben' => 5);

if(property_exists($ob, 'cat')){

    echo "Property Cat Exists";

}

if(property_exists($ob, 'ben')){

    echo "Property Ben Exists";

}

?>

In the above example, we created an object associative array then we used the property_exists the method with if the condition that if in the object array, property cat exists or not if it exists so print “property cat exists”, then we used another if condition with property_exists() method which states that if the property “ben” exists so print “property ben exists”.

Output

Property ben Exists

As you can see from the output our property “ben” exists in the object array.

Example 2

<?php
 
class library

{
    private $bookname;
 
    public $price;
 
    protected $bookcolor;
 
    public static $bookcategory;
}
 
var_dump(property_exists(library::class, 'bookauthor')); // false
 
var_dump(property_exists(library::class, 'bookname')); // true
 
var_dump(property_exists(library::class, 'price')); // true
 
var_dump(property_exists(library::class, 'bookcolor')); // true
 
var_dump(property_exists(library::class, 'bookcategory')); // true
 
?>

In the above example, we created a class then we defined some properties with access modifiers then for checking the existence of each property we used the property_exists() method.

Output

 bool(false)

 bool(true)

 bool(true)

 bool(true) 

bool(true)

As you can see from the output the method gave the result in Boolean value, the property bookauthor does not exist in our class so that’s why it returned a false value.

Example 3

<?php

class Food

{
  
public $foodie = null;

}

$food = new Food();

echo "<pre>";

var_dump(property_exists($food, 'foodie')); // true

var_dump(isset($food->foodie)); // false

echo "</pre>";

?>

in the above example, we created a class and then assigned a null value to the property after that we created an object for class food, then for checking the existence of the property we used the property_exists() method and isset method now let’s check the results.

Output

bool(true)

bool(false)

as you can see both methods returned different outputs, as we mentioned in the introduction the property_exists() method returns a true value even if the given value is null, and the isset method returned the false value.

Conclusion

In this tutorial we talk about the Property_exists() method in PHP, we see the introduction and syntax and then we discuss some examples.

Suggested Article:

Leave a Comment