开发者

Concatenation php variables:

开发者 https://www.devze.com 2023-03-05 12:24 出处:网络
I have a problem in creating variable in a loop andaccessing third variable value, i did try many way but now i don\'t know how to do that....

I have a problem in creating variable in a loop and accessing third variable value, i did try many way but now i don't know how to do that.... the code is..

$rand_1 =       random_username($_POST['txtuser_name']);
$rand_2 =       random_username($_POST['txtuser_name']);
$rand_3 =       random_username($_POST['txtuser_name']);

$username   =   "";

for($i=1; $i<=3; ++$i){
   $name   =   开发者_C百科 "rand_".$i;
   $username .= $name."<br />";
}

echo $username;

Any suggestion.....


Try $$name, which is a variable variable.

Still, when you see var_1 etc, generally it means you should be using an array.

Then you could make your code...

$rand = array();

foreach(range(0, 2) as $index) {
    $rand[] = random_username($_POST['txtuser_name']);
}

$username = join('<br />', $rand) . '<br />'; 


use $username .= $$name."<br />"; instead of $username .= $name."<br />";

But better approach may be

$user=array();

for($i=1; $i<=3; ++$i){
   $user[] =  random_username($_POST['txtuser_name']);
}

echo implode("<br/>", $user)."<br />";
0

精彩评论

暂无评论...
验证码 换一张
取 消