granttoth
granttoth

Reputation: 70

If array contains any data

I have these bits here that will display a google map link for me. How can I wrap this in some sort of if statemnt that will check to see if there is ANY data in $event['where']. I don't want the link to display when there is no data.

<a title="See on Map" target="_blank" href="http://maps.google.com/maps?q=<?php echo $event['where']; ?>">See on map</a>

Upvotes: 2

Views: 4342

Answers (4)

Benjie
Benjie

Reputation: 7946

The following will not show the link if $event['where'] is not set, is null, is blank (""), is false or is 0 - I think it is what you want:

<?php
if (!empty($event['where'])) {
  ?>
  <a title="See on Map" target="_blank" href="http://maps.google.com/maps?q=<?php echo $event['where']; ?>">See on map</a>
  <?php
}
?>

Upvotes: 1

Nick Rolando
Nick Rolando

Reputation: 26177

Well isset() will determine if a variable/object is set and is not null

if(isset($event['where'])){
//...
}

http://php.net/manual/en/function.isset.php

Upvotes: 1

Derek
Derek

Reputation: 4931

<?php if(!empty($event)){ ?> <a title="See on Map" target="_blank" href="http://maps.google.com/maps?q=<?php echo $event['where']; ?>">See on map</a> <?php } ?>

Upvotes: 3

qJake
qJake

Reputation: 17139

Try this:

<?php echo (!empty($event['where'])) ? $event['where'] : ""; ?>

Upvotes: 1

Related Questions