我正在学习一些PHP。有没有办法用多个变量定义变量?
类似的东西:
$name = "John"
$lastname = "Doe"
$number = "017434234"
$user = $name,$lastname,$number
这个脚本不起作用。哪种方法正确?
答案 0 :(得分:1)
如果要将它们放在一起,则点连接变量:
$user = $name.$lastname.$number;
如果要定义多个字符串,可以使用数组:
$user = ["John", "Doe", "123"];
或者您可以使用数组中的键来赋予它们含义:
$user = [firstname => "John", lastname => "Doe"];
echo $user->firstname; //gives John
答案 1 :(得分:0)
你学习PHP真棒!)
你可以把它变成一个像这样的数组,当然......这比上面的例子更“复杂”。
脚本:
//Function array with inputting and selecting what to return:
function MyArray($Firstname, $Lastname, $Number, $OutPutWhat){
//Create an array:
$Array = array(
'FirstName' => "$Firstname",
'LastName' => "$Lastname",
'Number' => "$Number"
);
//Return the arrayValue[ofthis]
return $Array[$OutPutWhat];
}
//Echo out the value
echo MyArray("John", "Doe", "1234567", 'FirstName'); //Outputs the firstname