Title: Check if array is not empty
Tags: php;arrays
Question: ********Update**********

```var_dump: string(0)``` "" 

I am trying to check if part of an array is not empty then display code but the code get's displayed anyway.

I have tried ```!is_null !empty```. I am not really sure which should be correct or should I go with: 
```if (sizeof($book['Booking']['comments'])&gt;0)```

Code:

```<?php if (!empty($book['Booking']['comments'])) {?&gt;
    <table width="100%" border="0"&gt;
        <tbody&gt;
            <tr&gt;
                <td style="font-family:'Lucida Grande', sans-serif;font-size:12px;font-weight:normal;color:#666666;"&gt;
                    <?=$book['Booking']['comments']?&gt;
                </td&gt;
            </tr&gt;
        </tbody&gt;
    </table&gt;
<? } ?&gt;
```

Array:

```Array
(
[Booking] =&gt; Array
    (
        [id] =&gt; 109
        [user_id] =&gt; 1
        [corporate_account_id] =&gt; 0
        [id_ref] =&gt; RES000109
        [price] =&gt; 178.00
        [arrival] =&gt; 2011-10-18 00:00:00
        [departure] =&gt; 2011-10-19 00:00:00
        [rate_title] =&gt; 
        [adult_guests] =&gt; 4
        [child_guests] =&gt; 0
        [company] =&gt; gravitate
        [titlename] =&gt; 
        [firstname] =&gt; John
        [surname] =&gt; Doe
        [address1] =&gt; 123 Fake St
        [address2] =&gt; 
        [city] =&gt; New York
        [state] =&gt; NY
        [postcode] =&gt; 12345
        [country] =&gt; US
        [phone] =&gt; 1111111111
        [mobile] =&gt; 
        [fax] =&gt; 
        [email] =&gt; betty78@example.net
        [comments] =&gt; 
        [created] =&gt; 2011-10-18 13:40:47
        [updated] =&gt; 2011-10-18 13:40:47
        [status] =&gt; 1
        [cancelled] =&gt; 0
        [request_src] =&gt; website
        [request_token] =&gt; 0
        [token] =&gt; ayzrGnx
        [survey_sent] =&gt; 0000-00-00 00:00:00
        [survey_returned] =&gt; 0000-00-00 00:00:00
        [send_sms] =&gt; 0
        [payment_time] =&gt; 0000-00-00 00:00:00
        [fullname] =&gt;  John Doe
    )
```

Comment: What's the type of `comments` supposed to be? `empty` should work fine in all cases except when you have a string composed entirely of whitespace.

Comment: What `var_dump($book['Booking']['comments'])` gives?

Comment: possible duplicate of [How to tell if a PHP array is empty?](http://stackoverflow.com/questions/2216052/how-to-tell-if-a-php-array-is-empty)

Here is the accepted answer: I suspect it may contain ```(bool) FALSE```, which is not true for ```is_null()```.

Try simply:

```if ($book['Booking']['comments']) {
```

This should also work for anything that evaluates to ```FALSE```, like an empty string.

Comment for this answer: `which is not true for empty()` - who said you that?
Comment for this answer: `if (!$book['Booking']['comments']) {` will do the opposite.
Comment for this answer: Whoa! Two months took you to correct one statement ;) What about other one?
Comment for this answer: @Col.Shrapnel somebody +1ed me for it randomly, to be honest I haven't re-visited this page since I wrote this, and I have admit this is *not* the best answer here... but it might as well be correct in it's inadequacy...

Here is another answer: ```!empty($var)```, ```count($var) &gt; 0```, ```!$var```, all of these will work in most situations. empty() has the "advantage" of not throwing a notice when the given variable / array key doesn't exist, but if you don't have to worry about that a boolean check (```!$var```) should suffice (see here which types convert to FALSE). It also happens to be the shortest in code.


Here is another answer: 
Empty array:

```$array = array();```

```reset($array)``` returns ```FALSE```.
Populated array:

```$array = array('foo', 'bar');```

```reset($array)``` returns first element (```'foo'```).



Here is another answer: I think you can try to use this following logic, and try to make it a little bit simpler. 

```<?php 
    // Only change this
    // and leave everything else as is
    $book_comment = $book['Booking']['comments'];
    $book_comment = trim($book_comment);
    // The reason I use empty trim, is to take 
    // away any space.  
    // In the output, use the original $book['Booking']['comments']

    if(!empty($book_comment)):?&gt;

      <table width="100%" border="0"&gt;
          <tbody&gt;
              <tr&gt;
                  <td style="font-family:'Lucida Grande', sans-serif;font-size:12px;font-weight:normal;color:#666666;"&gt;
                      <?=$book['Booking']['comments']?&gt;
                  </td&gt;
              </tr&gt;
          </tbody&gt;
       </table&gt;
<?php endif;?&gt;
```

I have not tested this, as I can't right now, but hopefully that should somewhat help you.


Here is another answer: If you want to ascertain whether the variable you are testing is actually explicitly an not empty array, you could use something like this:

```if ($book['Booking']['comments'] !== array()) {
  //your logic goes here
  ....................
}
```

This logic is more faster from others solution. Also it will maintain coding standard.


Here is another answer: that is not an array for sure.

do ```var_dump($book["Booking"]["comments"])``` to check the data type accordingly

Comment for this answer: I would like to display the code if it is not empty would I use ===? Thanks
Comment for this answer: then you should just do - if($book["Booking"]["comments"] == "")

Here is another answer: ```if (count($book['Booking']['comments']) &gt; 0) { ... }```


Here is another answer: You may want to ```trim()``` that property to remove any whitespace before testing if it is ```empty()```.

Edit: I'm assuming it is a string. It doesn't look like an empty array.


Here is another answer: I don't think that ```$book['Booking']['comments']``` is even an array in this case. So you could just use ```strlen```
http://php.net/manual/en/function.strlen.php

```<?php if (strlen($book['Booking']['comments'])) {?&gt;```


Here is another answer: your question is too localized. There is some typo in your code or something like that.

There is absolutely no difference what to use in your case, ```if (!empty($var))``` or ```if ($var)```. So, ```if ($book['Booking']['comments']) {``` worked, there was no problem with your ```if (!empty($book['Booking']['comments']))``` too. So, there was no poin in the betty78@example.net.

All these answers trying to answer this not-a-real-question are nonsense.

the only issue can be a space symbol mentioned by jotorres1, but you have already said there are none.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 10, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:10/18/11', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<php><arrays>', 'Title': 'Check if array is not empty', 'ViewCount': 54526, 'id': 7807630, 'user_AboutMe': None, 'user_AccountId': 329165.0, 'user_DisplayName': 'Keith Power', 'user_Reputation': 13437.0, 'user_Views': 561.0, 'user_id': 652541.0}}