Create a line with 5 php variables

Just think we need an echo line, something like this

sring01, sring02.<br /> sring03, sring04.</br /> sring05. 

the whole line belongs to variables. It is not important to have true values ​​for all five variables. if they have a false or empty output line should be different from the previous one. just suppose we have 2 empty variables for string02 and string03, then the output should be

 sring01, sring04.</br /> sring05. 

Can someone tell me this is the best way to achieve this?

I just tried something like this, but this does not work for me if not all the variables are true.

 if($addressOne||$addressTwo||$city||$province||$country) { $location = "$addressOne, $addressTwo.<br />"; $location .= "$city, $province.<br />"; $location .= "$country"; } else { $location = "some text"; } 
+4
source share
6 answers

1. Replace the entire variable in one variable of the array in the form of elements such as this 2. Then filter the filter for false values ​​using this array function 3. Then cut the filtered array using

 $array = array($var1, $var2, $var3, $var4, $var5); //place all variables in an array $filtered = array_filter($array); //Filter all false values such as '', null, FALSE $chunk = array_chunk($filtered, 2); //Chunk whole array to smaller groups with //atmost 2 elements $data = ''; foreach($chunk as $value) { $data .= implode(',', $value) . '<br/>'; //Then join two elements with ',' symbol } echo $data; 
+6
source

first step: Put all the variables in an array and filter out the empty, false ones using the empty () function.

 $values=array("value1", "", "value3", '',"value4", "", "value6", ''); $str =''; $arrNew=array(); foreach($values as $v){ if(! empty($v)) $arrNew []=$v; } 

second step: iterating through the new array and using the modulo command set the line break after each odd number of the loop counter, except for the zero number.

 for( $i=0; $i<count($arrNew); $i++){ if( ($i % 2 !== 0) && ($i !== 0) ) { $str .='.</br>'; }else{ $str .=','; } } echo $str; 
+2
source

Using:

 <?php $addressOne = "string1"; $addressTwo = ""; $city = ""; $province = "string4"; $country = "string5"; if(!empty($addressOne)) $string[] = $addressOne; if(!empty($addressTwo)) $string[] = $addressTwo; if(!empty($city)) $string[] = $city; if(!empty($province)) $string[] = $province; if(!empty($country)) $string[] = $country; $str = ""; for($i=0; $i<count($string); $i++) { $str .= $string[$i]; $str .= $i%2==0 ? "," : ".<br>"; } $str = trim($str,","); $str .= "."; echo $str; ?> 
+1
source

This code should work, but it is not very nice. There are probably "nicer" solutions

 $location = ""; $count = 0; // The first string can be set if not empty if($string01) { $location = $string01; $count++; } // second string if($string02) { // If $location is not empty we know the first was set and so we are have to add a "<br>" if($location != "") { $location .= "," . $string02 . "<br />"; $count = 0; } else { $location = $string02; $count++; } } // For String 3 to 5 we have to check what our count is. when it is 0 then we don't have to add a <br> else we have because the string is the second in line if($string03) { if($location != "") { if($count == 0) { $location .= $string03; $count++; } else { $location .= "," . $string03 . "<br />"; $count = 0; } } else { $location = $string03; $count++; } } if($string04) { if($location != "") { if($count == 0) { $location .= $string04; $count++; } else { $location .= "," . $string04 . "<br />"; $count = 0; } } else { $location = $string04; $count++; } } // For the last string we don't have to reset count because it the last if($string05) { if($location != "") { if($count == 0) { $location .= $string05; } else { $location .= "," . $string05 . "<br />"; } } else { $location = $string05; } } // Final text in $location 
0
source

try it

 $addressOne='ome'; $province='prov'; $country='$country'; if(!empty($addressOne)||!empty($addressTwo)||!empty($city)||!empty($province)||!empty($country)) { $locs = array(); if (!empty($addressOne))$locs[]=$addressOne; if (!empty($addressTwo))$locs[]=$addressTwo; if (!empty($city))$locs[]=$city; if (!empty($province))$locs[]=$province; if (!empty($country))$locs[]=$country; $l = array(); for ($i=0;$i<count($locs);$i+=2){ $c=$locs[$i]; if (!empty($locs[$i+1]))$c.=", {$locs[$i+1]}"; $l[]=$c; } $location = implode('<br/>',$l); }else{ $location = "some text"; } echo $location; 
0
source

Try to create separate variables for the corresponding positions and assign them the necessary values ​​instead of trying to display the values ​​from the original variables themselves.

I'm still new to PHP, so please point out errors or errors :)

Assuming that the position variables are as follows:

 pos1, pos2.<br/> pos3, pos4.<br/> pos5. 

then the following may work:

 $pos = array ($addressOne, $addressTwo, $city, $province, $country); for ($i=0; i<5; $i++) { if(!$pos[$i]) { $j=$i; while($pos[$j+1]) { $pos[$j] = $pos[$j+1]; //shift the strings one position to the front $j++; } } } echo $pos[0].', '.$pos[1].'.<br/>'. $pos[2].', '.$pos[3].'.<br/>'. $pos[4].'.'; 

Please note that this will not result in deletion , and . when the corresponding rows are missing. This is easy to do, and I thought it would be superfluous to include this in the answer.

Yours faithfully,
rktcool :)

0
source

Source: https://habr.com/ru/post/1501050/


All Articles