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 541185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:19:04+00:00 2026-05-13T10:19:04+00:00

I have the following PHP code: $required_fields = array (‘menu_name’,’visible’,’position’); foreach($required_fields as $fieldname) {

  • 0

I have the following PHP code:

$required_fields = array ('menu_name','visible','position');
foreach($required_fields as $fieldname)
{
    if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname]) )
    {
        $errors [] = $fieldname;
    }
}

menu_name, visible and position are variables that are received through the post method.

When the value of visible is zero, it creates an entry into the error array.

What is the best way to detect if a variable is empty when 0 is considered “not empty”?

  • 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-13T10:19:05+00:00Added an answer on May 13, 2026 at 10:19 am

    Since user data is sloppy, I use a custom function that treats empty spaces as non data. It sounds like this will do exactly what you want. This function will consider “0” to be valid (aka non-empty) data.

    function isNullOrEmpty( $arg )
    {
        if ( !is_array( $arg ) )
        {
            $arg = array( $arg );
        }
    
        foreach ( $arg as $key => $value )
        {
            $value = trim($value);
            if( $value == "" || $value == null )
            {
                return true;
            }
        }
        return false;
    }
    

    Please note it supports arrays too but requires that each value in the array contains data, which can be useful as you can just do something like this:

    $required = array( $_POST['name'],  $_POST['age'], $_POST['weight'] );
    if ( isNullOrEmpty($required) )
    {
      // required data is missing
    }
    

    PS: keep in mind this function will fire off PHP warnings if the value isn’t set and there’s no easy way around that, but you should NOT have warnings enabled in production anyways.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 502k
  • Answers 502k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't think you want a space here: .children("value[key='code']") Might… May 16, 2026 at 2:36 pm
  • Editorial Team
    Editorial Team added an answer Weird! Server.TransferRequest does exactly what I want without losing the… May 16, 2026 at 2:36 pm
  • Editorial Team
    Editorial Team added an answer Solution: I used ​ in it's place and it worked.… May 16, 2026 at 2:36 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the following code: <?php if ($x == 1){ ?> <b>Some html...</b> <?php
currently I have following code: home.php <form name='myformname' id='myformid'> <input type='text' name='mytext1' value='abc'> <input
I have the following code in header.php to echo the body's id: <body id=<?php
I have the following code that should, when run, update a table of victims
i have the following code for search from database show return result to the
I have an existing web application that I am converting to use CakePHP. The
Please See Correct Answer for solution to the requested question. Hi, Recently I have
I have a form which posts data to the same page. Based on the
Ok, have a bunch of questions that I have been thinking about the past
again. Right now I'm having issues with some pretty basic functionality in PHP. I

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.