动态命名变量

I am accepting a variable into a function.

For example, the variable is called $field and I then want to name a variable by what is inside the $field variable.

Say $field = 'randomName', I want my variable to be $randomName.

Is this possible?

Thanks.

Yes it is possible:

$field = "randomName";
$$field = "test";
$$$field = "test 2";
echo $randomName . "
"; //outputs: "test"
echo $test. "
"; //outputs: "test 2"

Check this out

You want variable variables?

See http://php.net/manual/en/language.variables.variable.php

Example

$varname = 'var';
$$varname = 'content';
print $var; // outputs 'content'

You can assign variables as variable names.

$$field

This will do what it sounds like you're asking for.

$field = "randomName" ;
${$field} = "yeah" ;

${"randomName"} = "that works too" ;

It is possible (see "variable variables"), but this approach should be discouraged and it is often better to use a data structure (i.e. an array with keys) for this sort of dynamic operation.

PHP has a language feature called variable variables. In your case:

$field = 'randomName';

$$field = "Hello world!";

echo $randomName; // prints out "Hello world!"