1

I'm not sure if this will work, or how to make it work.

Any help would be so great thanks.

I need to make this contitional statement string into one variabe?

How can I do this?

if ($address1) {
    echo $address1 . ', ';
}

if ($address2) {
    echo $address2 . ', ';
}

if ($address3) {
    echo $address3 . ', ';
}

if ($address4) {
    echo $address4 . ', ';
}

if ($townCity) {
    echo $townCity . ', ';
}

if ($county) {
    echo $county . ', ';
}

if ($postCode) {
    echo $postCode;
}

So basically I need this in one variable, and the reason I have it in if statements, is because it is possible for the some off these variables to not exist.

And after each variable, it has a comma and space.

If I do it like this...

$singleVar =    $address1 . ', ' . 
                $address2 . ', ' .
                $address3 . ', ' . 
                $address4 . ', ' . 
                $townCity . ', ' . 
                $county . ', ' . 
                $postCode;

Then for the variables that do not exist, will have and extra comma and space - not cool.


Can any one please advise.

Thanks

Joshc
  • 3,825
  • 16
  • 79
  • 121

6 Answers6

7

Lots of ways to do it, but here's what I'd use:

$items = array(
    $address1,
    $address2,
    $address3,
    $address4,
    $townCity,
    $county,
    $postCode
);
echo implode(', ', array_filter($items));

array_filter removes the "empty" ones, see converting to boolean in the PHP docs.

implode joins the array items together by the string you pass as the first argument, and makes sure there is no extra , dangling off the end of the output.

If you're worried the variables may not defined, use something like this instead:

$items = array();
$fields = array(
    'address1',
    'address2',
    'address3',
    'address4',
    'townCity',
    'county',
    'postCode'
);
foreach ($fields as $field)
{
    isset($$field) && $items[] = $$field;
}
echo implode(', ', $items);

Just remember that isset checks if something is "set", not if it is "empty". For example, a variable with an empty string or a zero is "set", but fails the if ($var) condition.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • Nice solutions, if the variables indeed exist. If not, you'll need an `isset` somewhere. – GolezTrol Oct 16 '12 at 21:17
  • Yes, this will fail when any of those variables does not exist.. which is the reason the original OP code had so many IF's.. – Nelson Oct 16 '12 at 21:18
  • @GolezTrol: `array_filter` will remove them, but yes I agree. – Wesley Murch Oct 16 '12 at 21:18
  • @Nelson: The OP is not using `isset`, but `if ($var)` which is much different. – Wesley Murch Oct 16 '12 at 21:19
  • Thank @Welsey and for elaborating. Will try this. Can I ask, will there be a `', '` after the final variable? – Joshc Oct 16 '12 at 21:22
  • No, implode only puts them inbetween values. – GolezTrol Oct 16 '12 at 21:22
  • Great, when you say defined? My variable will be defined, but will just be empty. So if a variable is empty, does that mean its not defined? – Joshc Oct 16 '12 at 21:24
  • Check the docs on [`isset`](http://www.php.net/manual/en/function.isset.php), and [`empty`](http://www.php.net/manual/en/function.empty.php) for a better explanation. Some values can be "set" but also "empty", also check out the `array_filter` docs for more info. – Wesley Murch Oct 16 '12 at 21:26
  • @Joshc: Check this link for more info about the "falsey value" thing: http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting – Wesley Murch Oct 16 '12 at 21:33
1
$address1 = 'foo';
$address3 = 'bar';
$postCode = 'baz';

$vars = array();
foreach (array('address1', 'address2', 'address3', 'address4', 'townCity', 'county', 'postCode') as $var)
{
    isset($$var) and $vars[] = $$var;
}
$single = implode(', ', $vars);

Test http://codepad.org/WwzRYvuO

kodeart
  • 1,903
  • 1
  • 19
  • 27
0

If the variables actually don't exist, you'll need isset to check for that. If they do exist but are empty/null/false, you may use the answer by Wesley Murch.

$singleVar = '';
$variables = array('address1','address2','address3',
                   'address4','townCity','county','postCode');

foreach($variables as $variable)
{
    if (isset($$variable))
      $singleVar .= ($$variable . ', ');
}
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • I can only guess the problem is the trailing `, ` (not my dv). – Wesley Murch Oct 16 '12 at 21:16
  • You are right. It would be easy to trim it, or use this loop to build a new array, which can then be imploded. This method shows the use of variable variables combined with isset, but if the code posted by OP works, the variables actually do exist and this answer is obsolete. – GolezTrol Oct 16 '12 at 21:21
  • My only fear with trim is if one of the actual values contains a character that you would trim. – Wesley Murch Oct 16 '12 at 21:22
  • I meant, cutting it off using substr or something, not actually using trim. (sorry). :) – GolezTrol Oct 16 '12 at 21:23
  • Thank for you method too, will try. Interesting reading your comment. – Joshc Oct 16 '12 at 21:27
0

You can do it using variable variables, like so:

$arr = array('address1','address2','address3','address4','townCity','county','postCode');
$singleVar = '';
foreach ($arr as $val) {
   if (isset($$val)) {
      $singleVar .= $$val . ', ';
   }
}
Nelson
  • 49,283
  • 8
  • 68
  • 81
-1
$string='';
if($address1)
  $string.= $address1.',';
if(address2)
  $string.= $address2.',';
//...etc
echo $string;
Landon
  • 4,088
  • 3
  • 28
  • 42
-1

i would propose the following:

join(array_filter(array($a,$b,$c,$d,$e,$f)), ", ");

with $a, etc being your variables. cf. https://stackoverflow.com/a/2603047/1689451

Community
  • 1
  • 1
Rudolf Mühlbauer
  • 2,511
  • 16
  • 18