/    Sign up×
Community /Pin to ProfileBookmark

Which Of These 3 Is An Empty Array ?

Folks,

In your professional experience and opinion which of these are not empty arrays ?
Just experimenting. That is all.

[code]
$array_01a = array();
$array_02a = array(”);
$array_03a = array(“”);
$array_04a = array(,);

$array_01b = array( );
$array_02b = array(‘ ‘);
$array_03b = array(” “);
$array_04b = array( , );

$array_01c = array( ” );
$array_02c = array( “” );

$array_01d = array( ‘ ‘ );
$array_02d = array( ” ” );

$array_01e = array(”,”);
$array_02e = array(“”,””);
$array_03e = array(”,””); //Sngl & DBL Quotes

$array_01f = array(‘ ‘,’ ‘);
$array_02f = array(” “,” “);
$array_03f = array(‘ ‘,” “); //Sngl & DBL Quotes

$array_01g = array( ”,” );
$array_02g = array( “”,”” );
$array_03g = array( ”,”” ); //Sngl & DBL Quotes

$array_01h = array(” , ”);
$array_02h = array(“” , “”);
$array_03h = array(” , “”); //Sngl & DBL Quotes

$array_01i = array( ” , ” );
$array_02i = array( “” , “” );
$array_03i = array( ” , “” ); //Sngl & DBL Quotes

$array_01j = array( ‘ ‘,’ ‘ );
$array_02j = array( ” “,” ” );
$array_03j = array( ‘ ‘,” ” ); //Sngl & DBL Quotes

$array_01k = array(‘ ‘ , ‘ ‘);
$array_02k = array(” ” , ” “);
$array_03k = array(‘ ‘ , ” “); //Sngl & DBL Quotes

$array_01l = array( ‘ ‘ , ‘ ‘ );
$array_02l = array( ” ” , ” ” );
$array_03l = array( ‘ ‘ , ” ” ); //Sngl & DBL Quotes

$array_1 = array(1);
$array_2 = array(‘1’);
$array_3 = array(“1″);
$array_4 = array(‘1’,”1”); //Sngl & DBL Quotes.
[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorFeb 20.2021 — Folks,

Q1.

How to findout if an array exists or not ?

Q2.

And, how to findout if an array is empty or not ?

Talking about non-key value arrays, like the ones shown above.

Q3.

I use the following 'Set A' codes to findout if array is empty of values or not. Are these codes ok or not ? If not then why not ?

Set A: 1
<i>
</i>If($array)
{
echo 'array full';
}



Set A: 2
<i>
</i>If(!$array)
{
echo 'array empty';
}



Set A: 3.
<i>
</i>If(empty($array))
{
echo 'array full';
}



Set A: 4.
<i>
</i>If(!empty($array))
{
echo 'array empty';
}



Set A: 5.
<i>
</i>If(empty($array) == FALSE)
{
echo 'array empty';
}



Set A: 6.
<i>
</i>If(empty($array) == 'FALSE')
{
echo 'array empty';
}



Set A: 7.
<i>
</i>If(empty($array) == "FALSE")
{
echo 'array empty';
}


___________________________________________
I reckon all followings are wrong:

Notice the closing brackets and operator on each example.

Q4. Are all these wrong or not to findout if an array is empty of values or not ?


Set B: 1
<i>
</i>If(empty($array == FALSE))
{
echo 'array empty';
}



Set B: 2
<i>
</i>If(empty($array == 'FALSE'))
{
echo 'array empty';
}



Set B: 3
<i>
</i>If(empty($array == "FALSE"))
{
echo 'array empty';
}



Set B: 4
<i>
</i>If(empty($array) = FALSE)
{
echo 'array empty';
}



Set B: 5
<i>
</i>If(empty($array) = 'FALSE')
{
echo 'array empty';
}



Set B: 6
<i>
</i>If(empty($array) = "FALSE")
{
echo 'array empty';
}



Set B: 7
<i>
</i>If(empty($array = FALSE))
{
echo 'array empty';
}



Set B: 8
<i>
</i>If(empty($array = 'FALSE'))
{
echo 'array empty';
}



Set B: 9
<i>
</i>If(empty($array = "FALSE")
{
echo 'array empty';
}


Show me which ones you prefer over the others to findout if an array is empty or not.

Give your rankings to all the code samples I mentioned so I can see which method you prefer.

Thanks
Copy linkTweet thisAlerts:
@developer_webauthorFeb 20.2021 — Folks,

Imagine no array exists like so:
<i>
</i>$arr = array();


Imagine I either forgot to build the array or I got the array name wrong when checking if array is set with values or not and imagine I checked with these 3 codes if array got values or not.

1.
<i>
</i>if($arr="FALSE") // DBL quotes.
{
echo '4. empty'; //This line echoes
}
else
{
echo '4. full'; //This line doesn't echo
}


Q5a. Is above code ok to check if array is full or not ?

I did not create the array and I get echoed array is empty. Am I getting this echoed because the array is empty or am I getting this echoed because the variable is empty or the variable doesn't exist ? Which is it ?


2.
<i>
</i>if($arr='FALSE') // SNGL quotes.
{
echo '4. empty'; //This line echoes
}
else
{
echo '4. full'; //This line doesn't echo
}


Q5b. Is above code ok to check if array is full or not ?

I did not create the array and I get echoed array is empty. Am I getting this echoed because the array is empty or am I getting this echoed because the variable is empty or the variable doesn't exist ? Which is it ?

3.
<i>
</i>if($arr="FALSE") // DBL quotes.
{
echo '4. empty'; //This line doesnt echoe
}
else
{
echo '4. full'; //This line gets echoed
}


Q5c. Is above code ok to check if array is full or not ?

I did not create the array and I get echoed **array is FULL. Why this false statement ?**

When you answer all 3 questions do number your answers likewise or I might get confused to which question you are replying.
Copy linkTweet thisAlerts:
@NogDogFeb 20.2021 — > @developer_web#1628315 If(empty($array == "FALSE"))

This is just plain wrong.
  • * The argument provided to empty() should be a variable, not a comparison; so it would be if(empty($array) == false), which can be simplified to if(!empty($array)) with the ! operator. (You can read that as "if not empty".)

  • * When comparing to false or true, do _not_ quote them, as then you are just comparing to a string, not a reserved word with a specific meaning in PHP.
  • Copy linkTweet thisAlerts:
    @developer_webauthorFeb 25.2021 — @NogDog#1628319

    Everything you said makes sense.

    However, I have seen others quoting TRUE/FALSE thus turning them into strings but my tests most of the time show the TRUE/FALSE string gets counted by php as Boolean and not string if I use the "==" before the "TRUE"/'FALSE' quoted string. Strange!

    Nevermind. Sticking to your advice.
    Copy linkTweet thisAlerts:
    @developer_webauthorFeb 25.2021 — Folks,

    Lately been using is_array() to check if array exists or not.

    Using array_key_exists() to check of first array value exists or not.

    <i>
    </i>$arr1 = array(1,10);
    if(ISSET($arr1))
    {
    if($arr1 != '')
    {
    //Array has to exist to use array_key_exists(). Array Variable Name has to exist atleast even if it holds no values. Eg $arr.
    if(array_key_exists('0',$arr1))
    {
    echo '3. $arr1[0] exists!';
    }
    else
    {
    echo '4. $arr1[0] doesn not exist!';
    }
    }
    else
    {
    echo '2. Array Values missing';
    }
    }
    else
    {
    echo '1. Array missing';
    }

    $arr2 = array('0'=&gt;'1','1'=&gt;'5');
    if(ISSET($arr2))
    {
    if($arr2 != '')
    {
    //Array has to exist to use array_key_exists(). Array Variable Name has to exist atleast even if it holds no values. Eg $arr.
    if(array_key_exists('0',$arr2))
    {
    echo '3. $arr2[0] exists!';
    }
    else
    {
    echo '4. $arr2[0] doesn not exist!';
    }
    }
    else
    {
    echo '2. Array Values missing';
    }
    }
    else
    {
    echo '1. Array missing';
    }
    ×

    Success!

    Help @developer_web spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.24,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...