Hello,
Right now, I am working on a little text-based game in PHP, and I am running into some problems with some functions that I defined.
The exact error is this:
Notice: Undefined variable: level in C:\Program Files\wamp\www\germ\game_functions.php on line 52
Notice: Undefined variable: exp_cap in C:\Program Files\wamp\www\germ\game_functions.php on line 80
Here are where the functions are defined. find_user_status uses the other two functions find_user_level and find_user_experience cap to produce its data. The lines where the errors are are marked by a comment.
require_once('connectvars.php');
require_once('appvars.php');
//function to display mini player status
function find_user_status($user_id)
{
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = "SELECT user_name, user_health, user_experience FROM user_info WHERE user_id = '" . $_SESSION['user_id'] . "'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
$level = find_user_level($_SESSION['user_id']);
$exp_cap = find_user_experience_cap($level);
echo '<table>';
echo '<tr><td>Username: ' . $_SESSION['user_name'] . '</td></tr>';
echo '<tr><td>Health: ' . $row['user_health'] . '/100 </td></tr>';
echo '<tr><td>Level: ' . $level . '</td></tr>';
echo '<tr><td>Experience: ' . $row['user_experience'] . '/' . $exp_cap . '</td></tr>';
echo '</table>';
}
//function to calculate level based on experience
function find_user_level($user_id)
{
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = "SELECT user_name, user_health, user_experience FROM user_info WHERE user_id = '" . $_SESSION['user_id'] . "'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
$experience = $row['user_experience'];
switch($experience)
{
case "$experience < 100":
$level = 1;
break;
case "99 < $experience < 500":
$level = 2;
break;
case "499 < $experience < 2000":
$level = 3;
break;
}
//error 1
return $level;
}
//function to find experience cap based on experience
function find_user_experience_cap($user_id)
{
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = "SELECT user_name, user_health, user_experience FROM user_info WHERE user_id = '" . $_SESSION['user_id'] . "'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
$experience = $row['user_experience'];
switch($experience)
{
case "$experience < 100":
$exp_cap = 100;
break;
case "99 < $experience < 500":
$exp_cap = 500;
break;
case "499 < $experience < 2000":
$exp_cap = 2000;
break;
}
//error 2
return $exp_cap;
}
It has something to do with the returning of the variables to the large function, but I can't figure out what.
Also, I think that my switch syntax might be wrong (should I use elseif?), so that might be causing the problem too.
Thanks