Eval and array index as a variable

I already talked about trying to use the index of the array as the variable name earlier and received one answer that interested me here .

One of the answers suggested using eval (), although not recommended, due to possible security issues.

$string = 'welcome["hello"]';
eval('$' . $string . " = 'thisworks';");

// This also works : eval("$$string = 'thisworks';");

// I could then print the array keys value like this
print( $welcome["hello"] ); // Prints 'thisworks'

However, this solution is the simplest and probably the most suitable for what I want to achieve. I am actually working on a Wordpress admin panel full of various options that the user can set. Each parameter is specified through a variable containing a multidimensional array, like this (only much more):

$options = array(
 array(
  'id' => 'option_id', // The name of the input field
  'type' => 'text', // Input type
  'value' => 'some value here' // Input value
 )
);

, , . , :

foreach ( $options as $o ) {
 if ( isset( $_POST[ $o[ 'id' ] ] ) ) {
  $settings[ $o[ 'id' ] ] = $_POST[ $o[ 'id' ] ];
 }
}

. , 'option_id [value1]'.

. , [], , ( ).

eval . , , . , , .

, , , , eval . eval ? , ? , $ PHP?

+3
1

eval . ?

$string = 'welcome';
$key = "hello";

${$string}[$key] = "this works better";
print( $welcome["hello"] ); // Prints 'this works better'
+3

All Articles