PHP Appending same value to two variables

前端 未结 1 1098
天涯浪人
天涯浪人 2021-01-22 10:40

I need to append a value to two variables.

$a = \"String A\";
$b = \"String B\";

...

$a .= \" more\";
$b .= \" more\";
<         


        
相关标签:
1条回答
  • 2021-01-22 11:06

    Not really.

    $a .= $b .= " more";
    

    Is equivalent to:

    $b .= " more";
    $a .= $b;
    

    The best way is to write:

    $a .= " more";
    $b .= " more";
    

    Or (if you have a lot of them) use array with some functions:

    #1 - array_map approach

    function addMore(&$vars) {
        $var .= " more";
    }
    $array = [$a, $b];
    $array = array_map('addMore',$array);
    

    #2 - classic approach:

    $array = array($a, $b);
    foreach ($array_before as &$var) {
        $var .= " more";
    }
    
    0 讨论(0)
提交回复
热议问题