0

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

3
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by network18
0

You can't use a switch statement like the way you are attempting. Try changing those switch statements to if-else statements and you should be fine.

0

yes, the switch is definitely something innovative, any programming language should think of offering!But at the moment no support for such switch statements yet, which can evaluate the expression.
one more thing, what if the session variable "user_id" wont be set.
$level will be nothing and will be passed as it is to the function and the query inside it will fail, hence no return from it.

0

Oh I see, I fixed it and now it works, so thanks for your help!!

0

yes, the switch is definitely something innovative, any programming language should think of offering!But at the moment no support for such switch statements yet, which can evaluate the expression.
one more thing, what if the session variable "user_id" wont be set.
$level will be nothing and will be passed as it is to the function and the query inside it will fail, hence no return from it.

Actually PHP does support the switch statement, the original poster was just using it incorrectly. A simple example of a PHP switch statement might be:

// get the mode of operation from the URL
if (isset($_GET["mode"]))
   $mode = intval($_GET["mode"]);
else
   $mode = 0;
// decide what to set something else to based on the mode
switch($mode)
{
   case 0: // this means that $mode == 0 is true
      $car = "Ferrari";
      break;
   case 1:
      $car = "Porche";
      break;
   case 2:
      $car = "Chevrolet";
      break;
   case 3:
      $car = "Dodge";
      break;
   default: // gets to here if $mode < 0 or $mode > 3
      $car = "Ford";
}

Of course this could be written with a long if-else statement, but personally I think the switch is neat in this situation. The original poster had case-statements attempting to evaluate a true/false themselves which is incorrect in syntax.

0

Yeah, I knew that the switch statements would work with static values, but I just wanted to see if they could evaluate logic statements, like conditionals.

0

Actually PHP does support the switch statement, the original poster was just using it incorrectly. A simple example of a PHP switch statement might be:

// get the mode of operation from the URL
if (isset($_GET["mode"]))
   $mode = intval($_GET["mode"]);
else
   $mode = 0;
// decide what to set something else to based on the mode
switch($mode)
{
   case 0: // this means that $mode == 0 is true
      $car = "Ferrari";
      break;
   case 1:
      $car = "Porche";
      break;
   case 2:
      $car = "Chevrolet";
      break;
   case 3:
      $car = "Dodge";
      break;
   default: // gets to here if $mode < 0 or $mode > 3
      $car = "Ford";
}

Of course this could be written with a long if-else statement, but personally I think the switch is neat in this situation. The original poster had case-statements attempting to evaluate a true/false themselves which is incorrect in syntax.

yes, PHP supports switch and other traditional logical structures too, and it has its one more foreach(); and this is clear to the thread starter, but as he mentions above he just wanted to see if the cases can get evalueted.
I meant with my post above, the cases need to be simple in their forms , can be integers or strings too ; but no support for the logical expressions to yet.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.