Lets say we have variables: $page and $id. For example, $id=2. I wanna create new variable from existing variables $page and $id: $page2. how to do it? is it possible??
You can use curly brackets notation to create your variable name : http://codepad.org/L6B5vYdG
<?php
$number = 2;
${'page' . $number} = 20 ;
var_dump($page2);
?>
You are asking for variable variables:
$page = 'x';
$id = 2;
${$page . $id} = 'lol'; // variable is named `$x2`
Or, simpler (I can't quite tell from your question):
$id = 2;
${'page' . $id} = 'lol'; // variable is named `$page2`
However, this is almost always a bad idea. Prefer arrays and/or objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With