PHP Display comma after each variable if variable is not empty -
how display comma after each variable if variable not empty.
<?php echo $city; ?>, <?php echo $province(); ?>, <?php echo $postalcode(); ?>, <?php echo $country(); ?> 
another way put them inside array in conjunction array_filter clean out empty strings , implode them:
$vars = array_filter(array($city, $province, $postalcode, $country)); echo implode(',', $vars); sidenote: if want treat empty spaces also, map out trim on elements, filter:
$test = array_filter(array_map('trim', array('1', ' ', 'test')));                                               //   ^ single space 
Comments
Post a Comment