Lets say I have a variable called $file and the for loop: for($i=1; $i <= 5; $i++) {}
For each iteration of the for loop, the $i
value will be appended to the $file
variable name so after the for loop ends, I should have five variables: $file1
, $file2
, $file3
, $file4
, and $file5
.
All variables in PHP start with a $ sign, followed by the name of the variable. A variable name must start with a letter or the underscore character _ . A variable name cannot start with a number. A variable name in PHP can only contain alpha-numeric characters and underscores ( A-z , 0-9 , and _ ).
The $var_name is a normal variable used to store a value. It can store any value like integer, float, char, string etc. On the other hand, the $$var_name is known as reference variable where $var_name is a normal variable. The $$var_name used to refer to the variable with the name as value of the variable $var_name.
A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive ( $age and $AGE are two different variables)
Use ${'varname'} syntax:
for($i=1; $i <= 5; $i++) { ${'file' . $i} = $i; }
However, it's often better to use arrays instead of this.
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