Pergunta

I have this question, and i can't find any radical answer...

So, is there any possibility to set two variable in one variable

if ($post) {
    $'item_name'. $x .' = $_POST['item_name'. $x .''];
}

if x = 1 then, $item_name1 = $_POST['item_name1']; that's the bahivor I want to implement, a way to wright the first part of the post.

the main issue item_namex (x) could be 1, 2, 3, 4 ext

Foi útil?

Solução

You are looking for variable variables in PHP:

$x = 7;
$var = 'item_name' . $x;
$$var = $_POST['item_name'. $x];  

and then you get $item_name7 with value you want. Also trailing concatenation of .'' in your right side of assignment is useless

Outras dicas

foreach($_POST as $key=>$value){
    $$key=$value;
}

For every $key variable will be created and $value would be assigned to it

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top