Can you use a variable that is inside the for?

1

For example. I'm working with Heredoc and whenever I need to do a for , I have to close Heredoc to do the for then reopen the tag mentioned.

Is there any way to query a variable within the for which will return all results to me? For example:

$telefones = 3;
for($i = 0; $i < $telefones; $i++) {
     $numero_tel = $buscar_telefones[$i]['numero'];
     $todosNumeros = "telefone: $numero_tel";
}
echo <<< EOT
     $todosNumeros
EOT;

PRINT: phone: 9999-9999 phone: 9999-7777 phone: 9999-8888

Thank you!

    
asked by anonymous 06.06.2014 / 00:16

2 answers

5

Only declare the variable before, outside the for() .

There are a few more changes that need to be made, as follows:

$todosNumeros = "";
$telefones = 3;

for($i = 0; $i < $telefones; $i++) {
     $numero_tel = $buscar_telefones[$i]['numero'];
     $todosNumeros .= "telefone: " . $numero_tel . " ";
}
echo <<< EOT
     $todosNumeros
EOT;
    
06.06.2014 / 00:20
3

You can seamlessly use outside of any variable created within the context of for() , and you do not have to create it before;

$telefones = 3;
for($i = 0; $i < $telefones; $i++) {
     $numero_tel = $buscar_telefones[$i]['numero'];
     $todosNumeros .= "telefone: $numero_tel ";
}

echo $todosNumeros;
    
06.06.2014 / 01:53