The case is as follows, I have a function that if something happens it creates an array with n numbers ($x[n])
and if it happens another it stores in a common variable with the same name ($x)
.
I know I can instead of storing things in a common variable ($x)
, could store in an array with index 0 ($x[0])
, but while writing code I came across this doubt and thought it would be interesting to share this doubt here.
Follow the example below:
if (!isset($_POST['x']))
{
$x = 1;
}
else
{
for ($i = 0; $i < 10; $i++)
{
$x[$i] = $i;
}
}
echo $x[0]; // Se $_POST['x'] não existir, o número 1 será printado?
This block of code is just an example, I did not use this logic in my program, as I said it was just a question that came to me when doing something similar.
So, someone would know to answer this question and if the answer is no, why does not it work?