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.
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
– riki481
was because of the $this in function call. retry edited code.
– Mateus
It now only displays the following message:
Array ( [sword] => 1 [damage] => 5 [weapon] => 0 )
Variables continue with original value– riki481
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.
– Mateus
Yes, I did exactly the same
– riki481
now you have to look like you’re treating them within the function
– Mateus
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
– riki481