0
Good Afternoon,
I’m mounting a naval battle and I’m having the following difficulty: I’m unable to access a particular matrix created within a function. Within the same function I call another function that accesses this matrix, but PHP informs that this matrix is not defined. Down with the structure I’m making.
//SORTEIA OS NAVIOS
function reiniciar(){
$navios = array();
//ZERA A MATRIZ
for ($l=-1; $l < 31; $l++) {
for ($c=-1; $c < 31 ; $c++) {
$navios[$l][$c] = -1;
}
}
//CHAMA AS FUNÇÕES
sorteia(3,5,"P");//CONSTROI OS PORTA AVIÕES
}
function sorteia($quantidade,$n_posicoes,$embarcacao){
if($navios[$rand_linha][$rand_coluna] == -1){
//AQUI DIZ QUE $navios ESTA INDEFINIDO
}
}
But in your code the $ships is even undefined.. Where are you passing $ships to Function draw? And where does it receive?
– Fleuquer Lima
I didn’t understand the need for this, how I should?
– Eliezer B.
would have to pass $ships as a kind of reference to the raffle function?
– Eliezer B.
The need is that if you don’t pass $ships for the Function draw it doesn’t work. You see, you’re setting $ships on the restart Function, but you don’t pass $ships for the draw Function, nor does the Function call to restart there. So it appears undefined because technically you’re doing an IF on a variable that doesn’t exist in that part of the code. Got it?
– Fleuquer Lima
Can pass direct if you want: draw(3,5,"P", $ships); and add in receipt Function draw($quantity,$n_positions,$vessel, $ships)
– Fleuquer Lima