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] => [email protected]
        [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
    )
3
  • 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. Commented Oct 18, 2011 at 13:08
  • What var_dump($book['Booking']['comments']) gives? Commented Oct 18, 2011 at 13:12
  • possible duplicate of How to tell if a PHP array is empty? Commented Aug 26, 2015 at 13:26

10 Answers 10

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.

Sign up to request clarification or add additional context in comments.

4 Comments

which is not true for empty() - who said you that?
if (!$book['Booking']['comments']) { will do the opposite.
Whoa! Two months took you to correct one statement ;) What about other one?
@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...
3

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

Comments

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.

Comments

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.

Comments

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

Comments

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.

Comments

1

that is not an array for sure.

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

2 Comments

then you should just do - if($book["Booking"]["comments"] == "")
I would like to display the code if it is not empty would I use ===? Thanks
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.

Comments

0
  1. Empty array:

    $array = array();

    reset($array) returns FALSE.

  2. Populated array:

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

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.