Reputation: 14141
********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
)
Upvotes: 12
Views: 55073
Reputation: 4765
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.
Upvotes: 0
Reputation: 5374
Empty array:
$array = array();
reset($array)
returns FALSE
.
Populated array:
$array = array('foo', 'bar');
reset($array)
returns first element ('foo'
).
Upvotes: 0
Reputation: 88647
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.
Upvotes: 8
Reputation: 157839
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.
Upvotes: 3
Reputation: 11
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.
Upvotes: 1
Reputation: 695
that is not an array for sure.
do var_dump($book["Booking"]["comments"])
to check the data type accordingly
Upvotes: 1
Reputation: 2978
!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.
Upvotes: 2
Reputation: 6351
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.
Upvotes: 1
Reputation: 1852
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'])) {?>
Upvotes: 1