Keith Power Keith Power - 3 months ago 14
PHP Question

Check if array is not empty

********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] => Seon
[surname] => Gleeson
[address1] => 8 Crow St
[address2] =>
[city] => Dublin
[state] => Co. Dublin
[postcode] => 2
[country] => Ireland
[phone] => 0863269087
[mobile] =>
[fax] =>
[email] => ger@res360.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] => Seon Gleeson
)

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.

Comments