Toleo
Toleo

Reputation: 774

Defining Multiple Variables with Equal Value and Concatenating

In this PHP Code I want to define a static value to multiple variables while concatenating the string to its parent variable like the following code here

$Alpha = "Alpha-";
$Beta = "Beta-";
$Gamma = "Gamma-";
function func($vars, $number, $letter) {
    switch ($number) {
        case 1:
            foreach ($vars as $var) {
                $var .= "1";
            }
                break;
        case 2:
            foreach ($vars as $var) {
                $var .= "2";
            }
                break;
        case 3:
            foreach ($vars as $var) {
                $var .= "3";
            }
    }
    switch ($letter) {
        case "a":
            foreach ($vars as $var) {
                $var .= "A";
            }
                break;
        case "b":
            foreach ($vars as $var) {
                $var .= "B";
            }
                break;
        case "c":
            foreach ($vars as $var) {
                $var .= "C";
            }
    }
    return $var;
}
func([$Alpha, $Beta, $Gamma], 1, "a");
$Alpha .= "-haEND";
$Beta .= "-taEND";
$Gamma .= "-maEND";

echo $Alpha;
echo '<br>';
echo $Beta;
echo '<br>';
echo $Gamma;

I expect the result to be

Alpha-1A-haEND

Beta-1A-taEND

Gamma-1A-maEND

But i don't get the 1A

Upvotes: 0

Views: 23

Answers (2)

iainn
iainn

Reputation: 17433

You've got an awful lot of repeated code here - there's no need to use six separate loops to append a number and letter to some strings.

PHP's array_walk method lets you apply a user-defined callback to each member of an array, which can cut this down dramatically, and also removes the need to add additional case statements if you need to expand the set of supported numbers/letters:

$Alpha = "Alpha-";
$Beta = "Beta-";
$Gamma = "Gamma-";

$values = [&$Alpha, &$Beta, &$Gamma];
$number = 1;
$letter = 'a';

array_walk($values, function (&$item) use ($number, $letter) {
    $item .= $number . strtoupper($letter);
});

$Alpha .= "-haEND";
$Beta .= "-taEND";
$Gamma .= "-maEND";

As @Syscall mentioned, the important difference is that the elements are passed to the function by reference - your original code was modifying copies, rather than the originals.

See https://eval.in/949087

Upvotes: 1

Syscall
Syscall

Reputation: 19764

You are trying to update a value of a copy of your variable (in each foreach). To update the value, you have to pass them by reference in the loop.

Then, you are not using your return value.

You could try something like this. Note the & in foreach, the using of return, and list() to extract your data from the array.

$Alpha = "Alpha-";
$Beta = "Beta-";
$Gamma = "Gamma-";
function func($vars, $number, $letter) {
    switch ($number) {
        case 1:
            foreach ($vars as &$var) {$var .= "1"; }
                break;
        case 2:
            foreach ($vars as &$var) {$var .= "2"; }
                break;
        case 3:
            foreach ($vars as &$var) {$var .= "3"; } 
    }

    switch ($letter) {
        case "a":
            foreach ($vars as &$var) {$var .= "A"; }
                break;
        case "b":
            foreach ($vars as &$var) {$var .= "B"; }
                break;
        case "c":
            foreach ($vars as &$var) {$var .= "C"; }
    }
    return $vars;
}

$arr = func([$Alpha, $Beta, $Gamma], 1, "a");
list($Alpha, $Beta, $Gamma) = $arr ;

$Alpha .= "-haEND";
$Beta .= "-taEND";
$Gamma .= "-maEND";

echo $Alpha;
echo '<br>';
echo $Beta;
echo '<br>';
echo $Gamma;

Outputs:

Alpha-1A-haEND
Beta-1A-taEND
Gamma-1A-maEND

Upvotes: 1

Related Questions