How to check if the value of a key in an object is empty or not

by | Jun 22, 2021 | PHP

Home » PHP » How to check if the value of a key in an object is empty or not

Introduction

Whatever we see on our website, it is either saved within HTML or being fetched from a table. The HTML has a script file attached with it which calls backend, where SQL codes are written. These SQL codes interact with database. In this article we will learn how to check if the value of a key in an object is empty or not.

Syntax

if(isset($obj-><key>)){
// if value is there, true is returned and the pointer comes here
}

How to check if the key in an object is empty or not

Suppose we have a use case where we need to check if the incoming value of a variable is empty and pass “failed” for the same. In this particular case, we will write given code:

if(isset($obj->status)){
   $status = $obj->status;
 }
 else{
   $status = 'failed';
  }

 

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author