Jump to content
xisto Community
sonesay

Arrays Outside A Function Need to have arrays available to all functions.

Recommended Posts

I've got a bunch of arrays that i want to use for more then 1 function. when i declear the arrays outside a function i cant use it in a function.

This code was originally written in javascript by another person but since I plan to use it and extend it with php I had to change it from javascript to php code. In the javascript code the arrays were decleared outside the functions with 'var arrayname' I read somewhere that declearing javascript variables with 'var' gives it global access. Any ideas on how I can go about declearing 1 set of these arrays to be used by as many functions as I need without copying them into every function?


below is current state with arrays inside GetHP();

function GetHP($raceIndex, $mjobIndex, $sjobIndex, $mainLevel, $subLevel) {			// Hume                D  D  D  D  D  D  D  D  D	$raceGrades[0] = array(4, 4, 4, 4, 4, 4, 4, 4, 4);	// Elvaan              C  E  B  E  C  F  F  B  D	$raceGrades[1] = array(3, 5, 2, 5, 3, 6, 6, 2, 4);	// Taru                G  A  F  D  E  C  A  E  D	$raceGrades[2] = array(7, 1, 6, 4, 5, 3, 1, 5, 4);	// Mithra              D  D  E  A  E  B  D  E  F	$raceGrades[3] = array(4, 4, 5, 1, 5, 2, 4, 5, 6);	// Galka               A  G  C  D  A  E  E  D  F	$raceGrades[4] = array(1, 7, 3, 4, 1, 5, 5, 4, 6);			$jobGrades[0]  = array(4, 0, 4, 4, 4, 6, 4, 4, 2);	// BST                 C  x  D  C  D  F  E  E  A	$jobGrades[1]  = array(3, 0, 4, 3, 4, 6, 5, 5, 1);	// BLM                 F  B  F  C  F  C  A  E  D	$jobGrades[2]  = array(6, 2, 6, 3, 6, 3, 1, 5, 4);	// BLU                 D  D  E  E  E  E  E  E  E	$jobGrades[3]  = array(4, 4, 5, 5, 5, 5, 5, 5, 5);	// COR                 D  x  E  C  E  C  C  E  E	$jobGrades[4]  = array(4, 0, 5, 3, 5, 3, 3, 5, 5);	// DRK                 C  F  A  C  C  D  C  G  G	$jobGrades[5]  = array(3, 6, 1, 3, 3, 4, 3, 7, 7);	// DRG                 C  x  B  D  C  D  F  E  C	$jobGrades[6]  = array(3, 0, 2, 4, 3, 4, 6, 5, 3);	// MNK                 A  x  C  B  A  F  G  D  E	$jobGrades[7]  = array(1, 0, 3, 2, 1, 6, 7, 4, 5);	// NIN                 D  x  C  B  C  B  D  G  F	$jobGrades[8]  = array(4, 0, 3, 2, 3, 2, 4, 7, 6);	// PLD                 C  F  B  E  A  G  G  C  C	$jobGrades[9]  = array(3, 6, 2, 5, 1, 7, 7, 3, 3);	// PUP                 D  x  E  B  D  C  E  F  C	$jobGrades[10] = array(4, 0, 5, 2, 4, 3, 5, 6, 3);	// RNG                 E  x  E  D  D  A  E  D  E	$jobGrades[11] = array(5, 0, 5, 4, 4, 1, 5, 4, 5);	// RDM                 D  D  D  D  E  E  C  C  D	$jobGrades[12] = array(4, 4, 4, 4, 5, 5, 3, 3, 4);	// SAM                 B  x  C  C  C  D  E  E  D	$jobGrades[13] = array(2, 0, 3, 3, 3, 4, 5, 5, 4);	// SMN                 G  A  F  E  F  D  B  B  B	$jobGrades[14] = array(7, 1, 6, 5, 6, 4, 2, 2, 2);	// THF                 D  x  D  A  D  B  C  G  G	$jobGrades[15] = array(4, 0, 4, 1, 4, 2, 3, 7, 7);	// WAR                 B  x  A  C  D  C  F  F  E	$jobGrades[16] = array(2, 0, 1, 3, 4, 3, 6, 6, 5);	// WHM                 E  C  D  F  D  E  E  A  C	$jobGrades[17] = array(5, 3, 4, 6, 4, 5, 5, 1, 3);			$hpScale[0] = array( 0, 0, 0, 0);	$hpScale[1] = array(19, 9, 1, 3);	$hpScale[2] = array(17, 8, 1, 3);	$hpScale[3] = array(16, 7, 1, 3);	$hpScale[4] = array(14, 6, 0, 3);	$hpScale[5] = array(13, 5, 0, 2);	$hpScale[6] = array(11, 4, 0, 2);	$hpScale[7] = array(10, 3, 0, 2);			$mpScale[0] = array( 0, 0,   0);	$mpScale[1] = array(16, 6,   4);	$mpScale[2] = array(14, 5,   4);	$mpScale[3] = array(12, 4,   4);	$mpScale[4] = array(10, 3,   4);	$mpScale[5] = array( 8, 2,   3);	$mpScale[6] = array( 6, 1,   2);	$mpScale[7] = array( 4, 0.5, 1);		$statScale[0] = array(0,    0,    0);	$statScale[1] = array(5, 0.50, 0.10);	$statScale[2] = array(4, 0.45, 0.20);	$statScale[3] = array(4, 0.40, 0.25);	$statScale[4] = array(3, 0.35, 0.35);	$statScale[5] = array(3, 0.30, 0.35);	$statScale[6] = array(2, 0.25, 0.40);	$statScale[7] = array(2, 0.20, 0.40);				$baseValueColumn = 0;	$scaleTo60Column = 1;	$scaleOver60Column = 3;	$scaleOver30Column = 2;	$hpIndex = 0;		  // Set up temp variables	$mainLevelE;	$mainLevelX;	$mainLevelXXX;	$mainLevelLX;	$mainLevelL;	$subLevel;	$subLevelX;	  	$mainLevelUpTo60;	$mainLevelOver10;	$mainLevelOver30;	$mainLevelOver60;	$mainLevelOver50andUnder60;	$subLevelOver10;	$subLevelOver30;		$raceHP;	$jobHP;	$sJobHP;	$bonusHP = 0;		$grade;// Get level breaks for changes in the stat formulas    //#####################################  // Primary HP levels    // First scale up to 60  $mainLevelUpTo60 = $mainLevel - 1;  if ($mainLevelUpTo60 > 59)    $mainLevelUpTo60 = 59;  // Different scale over level 60  $mainLevelOver60 = $mainLevel - 60;  if ($mainLevelOver60 < 0)    $mainLevelOver60 = 0;  // +1 per level over 30 (depends on HP rating)  $mainLevelOver30 = $mainLevel - 30;  if ($mainLevelOver30 < 0)    $mainLevelOver30 = 0;  if ($mainLevelOver30 > 30)    $mainLevelOver30 = 30;    //#####################################  // Bonus HP levels    // +2 per level over 10 (all)  $mainLevelOver10 = $mainLevel - 10;  if ($mainLevelOver10 < 0)    $mainLevelOver10 = 0;  // +2 per level over 50 and under 60  $mainLevelOver50andUnder60 = $mainLevel - 50;  if ($mainLevelOver50andUnder60 < 0)    $mainLevelOver50andUnder60 = 0;  if ($mainLevelOver50andUnder60 > 10)    $mainLevelOver50andUnder60 = 10;      //#####################################  // Subjob bonus levels    // +1 per level over 10 (/2)  $subLevelOver10 = $subLevel - 10;  if ($subLevelOver10 < 0)    $subLevelOver10 = 0;  if ($subLevelOver10 > 20)    $subLevelOver10 = 20;  // +1 per level over 30  $subLevelOver30 = $subLevel - 30;  if ($subLevelOver30 < 0)    $subLevelOver30 = 0;//---------------------------------------------------  // Calculate HP    // For race  $grade = $raceGrades[$raceIndex][$hpIndex];      $raceHP = floor($hpScale[$grade][$baseValueColumn] +    $hpScale[$grade][$scaleTo60Column]   * $mainLevelUpTo60 +    $hpScale[$grade][$scaleOver30Column] * $mainLevelOver30 +    $hpScale[$grade][$scaleOver60Column] * $mainLevelOver60);  // For job  $grade = $jobGrades[$mjobIndex][$hpIndex];    $jobHP = floor($hpScale[$grade][$baseValueColumn] +    $hpScale[$grade][$scaleTo60Column]   * $mainLevelUpTo60 +    $hpScale[$grade][$scaleOver30Column] * $mainLevelOver30 +    $hpScale[$grade][$scaleOver60Column] * $mainLevelOver60);  // Bonus increments  $bonusHP = 2 * $mainLevelOver10 +            2 * $mainLevelOver50andUnder60;    // Bonus for monk (main job)  if ($mjobIndex == 7) //"mnk"  {    // HP bonus of 30 at levels 15 and 35, bonus of 60 at levels 55 and 70    if ($mainLevel >= 15)      $bonusHP += 30;          if ($mainLevel >= 35)      $bonusHP += 30;    if ($mainLevel >= 55)      $bonusHP += 60;    if ($mainLevel >= 70)      $bonusHP += 60;  }  // For subjob  if ($subLevel > 0)  {    $grade = $jobGrades[$sjobIndex][$hpIndex];        $sjobHP = $hpScale[$grade][$baseValueColumn] +      $hpScale[$grade][$scaleTo60Column] * ($subLevel - 1) +      $subLevelOver10 + $subLevelOver30;          $sjobHP = floor($sjobHP / 2);        $sjobHP = $sjobHP + $hpScale[$grade][$scaleOver30Column] * $subLevelOver30;        // Bonus for monk (sub job)    if ($sjobIndex == 7) //"mnk"    {      // HP bonus of 30 at levels 15 and 35      if ($subLevel >= 15)        $bonusHP += 30;              if ($subLevel >= 35)        $bonusHP += 30;    }  }  else  {    $sjobHP = 0;  }    echo $raceHP . " ";  echo $jobHP . " ";  echo $sjobHP . " ";  echo $bonusHP . " ";  return ($raceHP + $jobHP + $sjobHP + $bonusHP);}$galka_war_hp = GetHP(4, 7, 16, 75, 37);echo "Galka WAR HP: ".$galka_war_hp;

If I cut and paste the arrays in the above code outside the GetHP(); the function stops to work correctly.

original java script: http://forums.xisto.com/no_longer_exists/
working example: http://forums.xisto.com/no_longer_exists/
Edited by sonesay (see edit history)

Share this post


Link to post
Share on other sites

JavaScript and PHP both handle things differently. In JavaScript, variables outside a function can be used within a function and vice versa. JavaScript also doesn't need to be in a specfic order in order to pick other functions up, where in PHP everything is read from top to bottom. Like jlhaslip said, the variables that hold these arrays, declare them as a global. To do so, at the begining of the function, type in global then the variables afterwards, for example:

function GetHP($raceIndex, $mjobIndex, $sjobIndex, $mainLevel, $subLevel) {global $arr1, $arr2;//rest of the coding...}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.