Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

property_exists

Checks whether a class or an object has a property.

Syntax

property_exists(object|string $object_or_class, string $property): bool

Parameters

ParameterTypeDescription
$object_or_classobject|stringAn object or a class name.
$propertystringThe property name.

Return value

bool`true` if the property is declared.

Examples

A property holding null exists:

<?php
 
class User
{
    public ?string $email = null;
}
 
$user = new User();
 
var_dump(property_exists($user, 'email'));
var_dump(isset($user->email));

Output:

bool(true)
bool(false)

Notes

isset() returns false for a property holding null, while property_exists() returns true.

See also

  • method_exists — Checks whether a class or an object has a method.
  • isset — Checks that a variable exists and is not null.

In the tutorial

  • Classes and objects — What classes and objects are in PHP, how to describe properties and methods and create objects.

Thanks for reading!

Donate to the site — we work hard for you.