I have four php variables like: $abc, $bcd, $dsf, $gkg
. Now I want to concatenate them with a comma separating each.
output:
abc,bcd,dsf,gkg
Now if any of the variable does not return any value.. then the output is coming like this:
abc,,dsf,gkg
So what to do to avoid this if value of any variable is null??
$street = tribe_get_address( $event->ID );
$city = tribe_get_city( $event->ID );
$state = tribe_get_stateprovince($event->ID);
$country = tribe_get_country( $event->ID );
$zipcode = tribe_get_zip( $event->ID );
$fulladdress= concatenation of these variables..
This solution should work for you:
Just put all variables into an array and filter all empty values out with array_filter()
, then just implode()
them by a comma, e.g.
$fulladdress = implode(",", array_filter([$street, $city, $state, $country, $zipcode])) ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With