3
The case is this, I have a function that if something happens it creates a array with n numbers ($x[n])
and if another happens it stores in a common variable with the same name ($x)
.
I know I can instead of storing things in one common variable ($x)
, could store in a array 0-rated ($x[0])
, but while writing the code I came across this doubt and I thought it would be interesting to share this doubt here.
Below is an example:
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 was just a question that came to me when doing something similar.
Then someone would know to answer that question and if the answer is negative, why it doesn’t work?
Enter the code to show how you’re doing. It might be easier to visualize your intention and come up with a solution. Put the generation of array or scalar variable and how it is consuming it. In a general way the answer is not only arrays - of all types, including strings - have index. This does not mean that it cannot be done otherwise.
– Maniero
The code is not necessary because I didn’t use it. It was just a question I had while developing, but I will edit with an example.
– Rafael