1

Hello there do you know a way for writing this in PHP without repeating the variable name?

if($abcdefg["blaBlaBlaBlaBlaBlaBlaBlaBlaBla"]!=="") {
    echo $abcdefg["blaBlaBlaBlaBlaBlaBlaBlaBlaBla"];
} else if($abcdefg["evenMoreBlaBlaBlaBlaBlaBlaBlaBlaBlaBla"]!=="") {
    echo $abcdefg["evenMoreBlaBlaBlaBlaBlaBlaBlaBlaBlaBla"];
} else if($abcdefg["stillAlotBlaBlaBlaBlaBlaBlaBlaBlaBla"]!=="") {
    echo $abcdefg["stillAlotBlaBlaBlaBlaBlaBlaBlaBlaBla"];
}

Of course you can write

$a = $abcdefg["blaBlaBlaBlaBlaBlaBlaBlaBlaBla"];
$b = $abcdefg["evenMoreBlaBlaBlaBlaBlaBlaBlaBlaBlaBla"];
$c = $abcdefg["stillAlotBlaBlaBlaBlaBlaBlaBlaBlaBla"];

if($a) { echo $a; } else if($b) { echo $b; } else if ($c) { echo $c; }

This is a bit shorter but I still wonder if there is some syntactical nice thing to write it without variable repetition.

Ternary operator does not solve the problem because of the "elseif" I think.

7
  • either spell it out in full, or copy/reference to another variable and use that. there are no "shortcuts". that or just don't use insanely long variable/key names... Commented Jun 30, 2015 at 18:04
  • 1
    It chitter chatters, just like birds and chickens. Commented Jun 30, 2015 at 18:07
  • possible duplicate of Check if variable is set and then echo it without repeating? Commented Jun 30, 2015 at 18:07
  • yeah, was just going to suggest a ternary ^ Commented Jun 30, 2015 at 18:08
  • The problem with ternary is the "elseif" i guess? However i like the answers of bitworking and rizier123 both solutions are nice. Commented Jun 30, 2015 at 18:10

3 Answers 3

4

This should work for you:

Just loop through all indexes, which you want to check and print them if they pass the if statement, e.g.

$indexes = ["blaBlaBlaBlaBlaBlaBlaBlaBlaBla", "evenMoreBlaBlaBlaBlaBlaBlaBlaBlaBlaBla", "stillAlotBlaBlaBlaBlaBlaBlaBlaBlaBla"];

foreach($indexes as $key) {
    if($abcdefg[$key] !== "") {
        echo $abcdefg[$key];
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can do a variable declaration in the if condition:

if(($var = $abcdefg["blaBlaBlaBlaBlaBlaBlaBlaBlaBla"]) !== "") {
    echo $var;
}

Comments

0

What about declaring a function for the ... hmmm ... functionnality?

function echoNotEmpty($s)
{
    if ($s !== '') echo $s;
}
echoNotEmpty($abcdefg["blaBlaBlaBlaBlaBlaBlaBlaBlaBla"]);
echoNotEmpty($abcdefg["evenMoreBlaBlaBlaBlaBlaBlaBlaBlaBlaBla"]);
echoNotEmpty($abcdefg["stillAlotBlaBlaBlaBlaBlaBlaBlaBlaBla"]);

Or even shorter:

echo $abcdefg["blaBlaBlaBlaBlaBlaBlaBlaBlaBla"];
echo $abcdefg["evenMoreBlaBlaBlaBlaBlaBlaBlaBlaBlaBla"];
echo $abcdefg["stillAlotBlaBlaBlaBlaBlaBlaBlaBlaBla"];

I mean, if you don't want to echo empty string and you still echo them, who cares?

1 Comment

The problem is the "elseif" (there are maybe two strings which are not empty, and only the first should be echoed).

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.