I was wondering if this code snippet is considered legal:
$arr = array(123,456,789,123,456,789);
foreach($arr as $a) {
$arr = $a;
break;
}
//prints 123
echo $arr;
It executes, but are there any pitfalls i should know using this method?
Update: Here is the actual problem
You have an array as follows from the database query (select * from table where code = $code)
Array
(
[0] => Array
(
[id] => 1
[code] => 1234567
[member_id] => 7
)
[1] => Array
(
[id] => 5
[code] => 1234567
[member_id] =>
)
[2] => Array
(
[id] => 67
[code] => 1234567
[member_id] => 43
)
)
all you care about is finding the first (if any) row that has an empty member_id (this means the code has not been claimed).
So how do you go about doing this?
According to Felix Kling using the variable to hold the array of codes and then overwriting it with the row that you want is not the best solution, so what do you propose.
Also, bonus credit:
How many different 7 digit codes can you generate using 32 characters (duplicate characters allowed)?
is it 32^32*7 or ((((((32^32)^32)^32)^32)^32)^32)^32)?
As others explained, there is no reason to reuse the variable. Is there a reason for it? Why not create a new one?
But here is the big question – why don’t you simply do that in SQL? You’re already creating an SQL query. What’s wrong with
select * from table where code = $code and member_id = null?Btw, to answer your other question: Number of permutations with seven characters, each one with 32 different possibilities = 32^7.