Access matrix within function

Asked

Viewed 62 times

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?

  • I didn’t understand the need for this, how I should?

  • would have to pass $ships as a kind of reference to the raffle function?

  • 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?

  • Can pass direct if you want: draw(3,5,"P", $ships); and add in receipt Function draw($quantity,$n_positions,$vessel, $ships)

2 answers

0

That’s what I’m doing:

        //CHAMA AS FUNÇÕES
		sorteia(3,5,"P",$navios);//CONSTROI OS PORTA AVIÕES
		//sorteia(5,3,"D",$navios);//CONSTROI OS DESTROIER
		//sorteia(7,2,"S",$navios);//CONSTROI OS SUBMARINOS
		//sorteia(10,1,"B",$navios);//CONSTROI OS BOTES
		
		echo "<table border='1'";
			for ($l=0; $l < 30 ; $l++) { 
				echo "<tr>";
					for ($c=0; $c < 30 ; $c++) { 
						echo "<td>";
							echo 
						echo "</td>";
					}
				echo "</tr>";
			}
		echo "</table>";

0

Well I don’t know if I could explain it properly from the comments so let’s go.

What happens in your code is this, you’re using functions correct? Then the variable you created in the function reiniciar can only be used there! Unless you declare it as global, then at function sorteia you get 3 variables:

 function sorteia($quantidade,$n_posicoes,$embarcacao)

You can then work with these 3 variables here because you are setting their values when making the method call:

sorteia(3,5,"P");

However $navios which is the variable you use in the if, does not exist here because you did not pass and/or did not set a value for it. In case you could pass directly:

sorteia(3,5,"P", $navios);

And then get her in function:

function sorteia($quantidade,$n_posicoes,$embarcacao, $navios)

So you could work with her on function draw.

I hope you were able to explain, any questions ask

  • I understood, I did this and it worked, but I need to display it outside of this draw Function, because I will call the draw function several times to go feeding this array. And from what I noticed if I put inside the Function draw the screen view of the array it displays, but when I put out it does not appear anything of what I entered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.