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
Lots of ways to do it, but here’s what I’d use:
array_filterremoves the “empty” ones, see converting to boolean in the PHP docs.implodejoins 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:
Just remember that
issetchecks if something is “set”, not if it is “empty”. For example, a variable with an empty string or a zero is “set”, but fails theif ($var)condition.