Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Concatenate various strings in php with null effect

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..
like image 281
Deepansh Jain Avatar asked May 23 '15 14:05

Deepansh Jain


1 Answers

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])) ;
like image 106
Rizier123 Avatar answered Oct 21 '22 01:10

Rizier123