Jump to content

Function / Switch Questions


pengu

Recommended Posts

2 Files.

 

functions.inc.php ->

 

<?PHP

Function myLevel($my_xp)
{

switch($my_xp)
{

	case ($my_xp > 500): // Level 2
		$stuff = 'happens';
		$my_level = 1;			

	case ($my_xp > 1000): // Level 3
		$stuff = 'happens';

	case ($my_xp > 2000): // Level 4
		$stuff = 'happens';
		$my_level = 4;

	case ($my_xp > 3500): // Level 5
		$stuff = 'happens';

	case ($my_xp > 5500): // Level 6
		$stuff = 'happens';

}

}

?>

 

stats.php

<?PHP

$my_xp = 2002;

include('functions.inc.php');

myLevel($my_xp);

echo $my_level;

?>

 

Alrighty a few questions.

 

I have removed the 'break;' from the switch, not sure if it's needed, but I thought it'd see that the first case was TRUE and break away and not bother going any further.  Is this correct?

 

How does switch handle when multiple things are true, will it take the last case down the list and use all the information from that, keeping in mind I'll be repeating certain information. (by info I mean variables)

 

Finally It's not actually working, haha.  So I want to include that list of functions, more will be added and I assume I am calling it correctly.  I want to include it, set '$my_xp' then use the function to check the $my_xp and adjust information accordingly.

 

Regards,

Pengu.

 

P.S It may look like a bad idea to do each level individually but there is method to my madness, method I tell you!

Link to comment
Share on other sites

Switch won't work for what you're trying to do at that point.

 

Here is a simpler approach:

 

function myLevel($my_xp)
{
global $my_level, $stuff; 

/*the above references the $my_level variable out of the function's scope without it, $my_level will be a separate variable specific only to this function in this scope (when it is being run this specific time*/

$xp_table = array(  array('level' => 0, 'req_xp' => 0),
					array('level' => 1, 'req_xp' => 500), 
					array('level' => 2, 'req_xp' => 1000), 
					array('level' => 3, 'req_xp' => 2000), 
					array('level' => 4, 'req_xp' => 3500), 
					array('level' => 5, 'req_xp' => 5500)
				);

//loop backwards
for($i = count($xp_table)-1; $i >= 0; $i++)
{
	if($my_xp >= $xp_table[i]['req_xp'])
	{
		$my_level = $xp_table[i]['level'];
		break;
	}
}

//now switch
switch($my_level)
{
	case 0:
		$stuff = 'happens';
		break;
	case 1:
		$stuff = 'happens';
		break;
	case 2:
		$stuff = 'happens';
		break;
}


//possible return value

}

Link to comment
Share on other sites

 

It won't let me edit that one but here is a cleaner version

 

function myLevel($my_xp)
{
global $my_level, $stuff;
/*the above references the $my_level variable out of the function's scope.
without it, $my_level will be a separate variable specific only to this function
in this scope (when it is being run this specific time) */

$xp_table = array(
		array('level' => 0, 'req_xp' => 0),
		array('level' => 1, 'req_xp' => 500), 
		array('level' => 2, 'req_xp' => 1000), 
		array('level' => 3, 'req_xp' => 2000), 
		array('level' => 4, 'req_xp' => 3500), 
		array('level' => 5, 'req_xp' => 5500)
);

//loop backwards
for($i = count($xp_table)-1; $i >= 0; $i++)
{
	//check if their XP matches the XP req of each level starting with the highest
	if($my_xp >= $xp_table[i]['req_xp'])
	{
		$my_level = $xp_table[i]['level'];
		break; //stop looping if we found the level
	}
}

//now switch between level which was already determined
switch($my_level)
{
	case 0:
		$stuff = 'happens';
		break;
	case 1:
		$stuff = 'happens';
		break;
	case 2:
		$stuff = 'happens';
		break;
}


//possible return value

}

Link to comment
Share on other sites

Thank you Jake.

 

Next question is they will start at level 1.. so I pretty much want to count from 2 onwards, will this have any effect?

I mean I can easily get around it by making there starting $my_xp at 500.

 

Edit:

 

On second glance, Global is what I was missing, thanks heaps Jake.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.