How to make a custom PHP function work

Discussion in 'PHP' started by egdcltd, Feb 10, 2007.

  1. #1
    I've got a file that has the same code repeated quite a few times in it. I'm trying to extract that code, make it into a function, then add the function to another file that's already included in the original file.

    I'm having problems passing whatever is needed though. I know how to call a function, and the rough layout for designing it, but I can't seem to get it to work.

    Here's the code I'm trying to make into a function:

    		$mp_consumned = '0';
    		if ( $rabbit_user['creature_ability'] == '1' )
    		{
    			if ( ( $rabbit_user['creature_health'] < $rabbit_user['creature_health_max'] ) && ( $rabbit_user['creature_health'] > 0 ) && ( $rabbit_user['creature_mp'] > $rabbit_general['regeneration_mp_need'] ) )
    			{
    				$mp_consumned = $rabbit_general['regeneration_mp_need'];
    				$pet_regen = $rabbit_general['regeneration_hp_give'];
    				$battle_message .= sprintf($lang['Rabbitoshi_Adr_battle_regen'], intval($pet_regen)).'<br />' ;
    			}
    			$sql = "UPDATE " . RABBITOSHI_USERS_TABLE . "
    				SET creature_health = creature_health + $pet_regen,
    			   	creature_mp = creature_mp - $mp_consumned
    			WHERE owner_id = $user_id ";	
    			if (!$result = $db->sql_query($sql)) 
    			{
    				message_die(GENERAL_ERROR, 'Could not update pet info', '', __LINE__, __FILE__, $sql);
    			}
    		}
    PHP:
    I think with it being in an external file, some stuff that would be in the original file isn't there.
     
    egdcltd, Feb 10, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    need more than that, perhaps you could post the entire page you're talking about, so we know where the data is coming from .....
     
    krakjoe, Feb 10, 2007 IP
  3. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    egdcltd, Feb 10, 2007 IP