12

********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'])>0)

Code:

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

Array:

Array
(
[Booking] => Array
    (
        [id] => 109
        [user_id] => 1
        [corporate_account_id] => 0
        [id_ref] => RES000109
        [price] => 178.00
        [arrival] => 2011-10-18 00:00:00
        [departure] => 2011-10-19 00:00:00
        [rate_title] => 
        [adult_guests] => 4
        [child_guests] => 0
        [company] => gravitate
        [titlename] => 
        [firstname] => John
        [surname] => Doe
        [address1] => 123 Fake St
        [address2] => 
        [city] => New York
        [state] => NY
        [postcode] => 12345
        [country] => US
        [phone] => 1111111111
        [mobile] => 
        [fax] => 
        [email] => example@example.com
        [comments] => 
        [created] => 2011-10-18 13:40:47
        [updated] => 2011-10-18 13:40:47
        [status] => 1
        [cancelled] => 0
        [request_src] => website
        [request_token] => 0
        [token] => ayzrGnx
        [survey_sent] => 0000-00-00 00:00:00
        [survey_returned] => 0000-00-00 00:00:00
        [send_sms] => 0
        [payment_time] => 0000-00-00 00:00:00
        [fullname] =>  John Doe
    )
Shrujan Shetty
  • 2,298
  • 3
  • 27
  • 44
Keith Power
  • 13,891
  • 22
  • 66
  • 135
  • 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. – Jon Oct 18 '11 at 13:08
  • What `var_dump($book['Booking']['comments'])` gives? – Toto Oct 18 '11 at 13:12
  • 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) – Limon Monte Aug 26 '15 at 13:26

10 Answers10

8

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.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
3

if (count($book['Booking']['comments']) > 0) { ... }

Nathan Loding
  • 3,185
  • 2
  • 37
  • 43
3

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 question at all.

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
2

!empty($var), count($var) > 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.

Elte Hupkes
  • 2,773
  • 2
  • 23
  • 18
1

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'])) {?>

leo.vingi
  • 1,842
  • 12
  • 17
1

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.

Donnie
  • 6,229
  • 8
  • 35
  • 53
1

that is not an array for sure.

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

hex4
  • 695
  • 2
  • 9
  • 23
1

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)):?>

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

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

jotorres1
  • 11
  • 2
0
  1. Empty array:

    $array = array();

    reset($array) returns FALSE.

  2. Populated array:

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

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

leymannx
  • 5,138
  • 5
  • 45
  • 48
0

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.

Faisal
  • 4,591
  • 3
  • 40
  • 49