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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -