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

2

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Nice solutions, if the variables indeed exist. If not, you'll need an isset somewhere.
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..
@GolezTrol: array_filter will remove them, but yes I agree.
@Nelson: The OP is not using isset, but if ($var) which is much different.
Thank @Welsey and for elaborating. Will try this. Can I ask, will there be a ', ' after the final variable?
|
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

Comments

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 . ', ');
}

5 Comments

I can only guess the problem is the trailing , (not my dv).
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.
My only fear with trim is if one of the actual values contains a character that you would trim.
I meant, cutting it off using substr or something, not actually using trim. (sorry). :)
Thank for you method too, will try. Interesting reading your comment.
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 . ', ';
   }
}

Comments

-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

Comments

-1
$string='';
if($address1)
  $string.= $address1.',';
if(address2)
  $string.= $address2.',';
//...etc
echo $string;

2 Comments

hmmm, why the downvote with no comment? this solution works fine
Perhaps due to lack of explanation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.