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

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts

Comments

Leave a Reply

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.