Function does not change the variable

Asked

Viewed 166 times

0

I’m learning to use functions in php. I created this html, which will get 3 information, Name, race and player class:

<html>
<head><font face="Arial">
<input type="hidden" name="decicsion" value="0"></head>
<body>
<form method="POST">
<div id="start" class="start">
Name:<input type="text" name="n1"><br>
Race:<select name="n2">
<option value="H">Humano</option>
<option value="O">Orc</option>
<option value="E">Elfo</option>
</select><br>
Class:<select name="n3">
<option value="W">Guerreiro</option>
<option value="M">Mago</option>
<option value="A">Arqueiro</option>
</select><br>
<b>
<input type="submit" name="Enviar" value="Enviar"></form></div>
<body>';

In php, I started creating an algorithm that saves each value, did the test and it works, showing all values already defined.
Each choice in class and race has a change in other variables that I didn’t mention because it would be a very large code snippet.
Basically, when the html values are chosen, this will increase some character attribute, and give two items to it

if($equip==0)
{
//Define os atributos da classe
if($race=="H")      {$mana=$mana+20; $race="Humano";     }
elseif($race=="O")  {$health=$health+20;    $race="Orc";}
elseif($race=="E")  {$dextery=$dextery+20; $race='Elfo';}

//Adiciona um item de acordo com a classe
if($class=='W'  )   {$sword=1;  $plate=1; $staff=0; $cape=0; $bow=0;    $vest=0; $classe='Guerreiro';}
if($class=='M') {$staff=1;  $cape=1; $plate=0; $sword=0; $bow=0;    $vest=0; $classe='mago';}
if($class=='A') {$bow=1;    $vest=1; $sword=0; $plate=0;    $cape=0; $classe='arqueiro';}

}

I then created several if and elseif to increase character values if item equals 1. Like this example:

if($sword==1)
{
    $damage=$damage+3; $weapon='Sword'; $sword=3;
}
elseif($sword==2)
{
    $damage=$damage-3;$sword=3;
} 

I did this for every item I put in the class excerpt, so the code got too long and complicated. I wanted to turn these code snippets into functions, so I could use them again if I needed to. so I created the function (equipSword) and passed the code inside:

//Trecho do código alterado no if
if($class=='W'  )   
{
    equipSword(1);
    $plate=1; 
    $staff=0;   
    $cape=0; 
    $bow=0; 
    $vest=0; 
    $classe='Guerreiro';
}


//defini essa função abaixo do if($equip==0)
function equipSword($sword)
{
if($sword==1){$damage=$damage+3; $weapon='Sword'; $sword=3;}
elseif($sword==2){$damage=$damage-3;$sword=3;} 
if($sword == 3 ){echo'<table style="position:absolute;left:1px;" border="1"><tr><td>Weapon</td><td>Sword</td><tr></table>';}
return $sword;
return $damage;
return $weapon;
}

The problem I’m going through, is that even doing the Returns, my variables do not change, continue with the same value. I wonder what I’m doing wrong to return these values.

2 answers

2


You have to pass as parameter the new values you want. Because the way you are doing the values are already defined in the function.

example:

$sword = 5;
$damage = 33;
$weapon = "Tword";

$dados = equipSword($sword, $damage, $weapon);
print_r($dados);

PHP function

function equipSword($sword, $damage, $weapon){

   if($sword==1){
       $damage = $damage + 3;
       $weapon = $weapon; 
       $sword = $sword;
   }
   elseif($sword == 2){
       $damage = $damage-3;
       $sword = 3;
   } 

   if($sword == 3 ){
       echo'<table style="position:absolute;left:1px;" border="1"><tr> 
       <td>Weapon</td><td>Sword</td><tr></table>';
   }

   $dados = [];
   $dados["sword"] = $sword;
   $dados["damage"] = $damage;
   $dados["weapon"] = $weapon;

   return $dados;
}
  • I tested your algorithm to see if it would work, the program sent the following error FATAL ERROR Uncaught Error: Using $this when not in object context in /home4/phptest/public_html/code.php70(5) : eval()'d code:73 Stack trace: #0 /home4/phptest/public_html/code.php70(5): eval() #1 {main} thrown on line number 73number 73

  • was because of the $this in function call. retry edited code.

  • It now only displays the following message: Array ( [sword] => 1 [damage] => 5 [weapon] => 0 ) Variables continue with original value

  • You have to send the new values as parameter, notice how I did it in the first example where I set the new values to Sword, Damage and Weapon. And from the moment the function receives these values you can work on them the way you want. then you can set a pattern within the function where it goes performing always.

  • Yes, I did exactly the same

  • now you have to look like you’re treating them within the function

  • It worked after I made the variables out of the data equal to the array. That’s not really what I wanted, because I wanted a way that would allow me to call Function from another place with a few lines of code. But you’ve helped, thank you

Show 2 more comments

1

There are some conceptual errors in its implementation

1 - functions and methods return in the first return executed, soon, everything that comes under return $sword will not be executed

2 - Return does not return the variable but its value, so you need this value, you must store the return in a variable, something like: $sword=equipSword($sword);

3 - there is the scope of variables, in summary form: the variables declared outside the function are not the same that vc called in, to be the same vc need to declare them as global within the function, type:

function equipSword()
{
    global $sword;
    global $damage;
    global $weapon;

    if($sword==1){$damage=$damage+3; $weapon='Sword'; $sword=3;}
    elseif($sword==2){$damage=$damage-3;$sword=3;} 
    if($sword == 3 ){echo'<table style="position:absolute;left:1px;" border="1"><tr><td>Weapon</td><td>Sword</td><tr></table>';}

}
  • I made the code changes as you explained, but the values don’t change anyway

Browser other questions tagged

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