Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6375257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:35:32+00:00 2026-05-25T01:35:32+00:00

Could you help me to improve my coding style?:) In some tasks I need

  • 0

Could you help me to improve my coding style?:) In some tasks I need to check – is variable empty or contains something. To solve this task, I usually do the following.

Check – is this variable set or not? If it’s set – I check – it’s empty or not?

<?php
    $var = '23';
    if (isset($var)&&!empty($var)){
        echo 'not empty';
    }else{
        echo 'is not set or empty';
    }
?>

And I have a question – should I use isset() before empty() – is it necessary? TIA!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:35:33+00:00Added an answer on May 25, 2026 at 1:35 am

    It depends what you are looking for, if you are just looking to see if it is empty just use empty as it checks whether it is set as well, if you want to know whether something is set or not use isset.

    Empty checks if the variable is set and if it is it checks it for null, “”, 0, etc

    Isset just checks if is it set, it could be anything not null

    With empty, the following things are considered empty:

    • “” (an empty string)
    • 0 (0 as an integer)
    • 0.0 (0 as a float)
    • “0” (0 as a string)
    • NULL
    • FALSE
    • array() (an empty array)
    • var $var; (a variable declared, but without a value in a class)

    From http://php.net/manual/en/function.empty.php


    As mentioned in the comments the lack of warning is also important with empty()

    PHP Manual says

    empty() is the opposite of (boolean) var, except that no warning is
    generated when the variable is not set
    .

    Regarding isset

    PHP Manual says

    isset() will return FALSE if testing a variable that has been set to NULL


    Your code would be fine as:

    <?php
        $var = '23';
        if (!empty($var)){
            echo 'not empty';
        }else{
            echo 'is not set or empty';
        }
    ?>
    

    For example:

    $var = "";
    
    if(empty($var)) // true because "" is considered empty
     {...}
    if(isset($var)) //true because var is set 
     {...}
    
    if(empty($otherVar)) //true because $otherVar is null
     {...}
    if(isset($otherVar)) //false because $otherVar is not set 
     {...}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please help meto improve this part of code? The string is something
was wondering if anyone could help me improve my code. Its just some basic
I'm working on a simple game and I need some help to improve my
if someone could help me to improve this function to use it with this
I hope someone could help me on this. I want to add a month
I wonder if you could help me with something I've been thinking about. Say
Is there something available that could help me convert a XSD into SQL relational
HI All I was hoping someone could help me improve a query I have
I need to improve this statement by either correcting it or making it shorter
I was wondering if anyone could help me with this problem: I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.