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

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > PHP Programming

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

sonesay
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();
CODE
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://www.datasync.com/~dsmith/FFXIStats/ffxistat.js
working example: http://www.datasync.com/~dsmith/FFXIStats

 

 

 


Reply

jlhaslip
declare them as global in the function the arrays are used in


Reply

truefusion
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:
CODE
function GetHP($raceIndex, $mjobIndex, $sjobIndex, $mainLevel, $subLevel) {
global $arr1, $arr2;

//rest of the coding...

}

Reply

sonesay
Thanks guys it works like a charm.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. cache:w4upuxxxakoj:www.trap17.com/index.php/wappys-php-snippets_t38482.html >> enter your keyword - 759.31 hr back. (1)
Similar Topics

Keywords : arrays, function, arrays, functions,

  1. PHP Function To Add Previous and Next Page Feature
    useful php function (5)
  2. Functions
    ??? (9)
    I created this topic mainly because I wanna get a clear interpretation about those listed PHP
    functions. The first function is while($row=mysql_fetch_array($query)) { ....
  3. Endif function?
    (6)
    As you get noticed before, I am studying PHP in examples like using the tutorials as well as books
    itself. Through my readings, I get this function CODE <?php endif; ?> a lot of times.
    So, what do you mean by this function, and what does it do exactly?....
  4. Php Email Validation
    A PHP data validation class with many functions (1)
    I've been reading through my old php book (PHP 4.1) and came across this data validation class.
    It can check a number of things ranging from telephone numbers , credit card number formats, email
    address and some others. I checked out some of the methods although I didnt expect it to work 100%
    because I've found source code errors thoughout the book and CD. I tested out a few of the
    methods to check and some of them did return expected results but some didnt either so the data
    validation class was not perfect and it didnt really bother me. The cool thing I found....
  5. Php Functions To Send Mail
    (9)
    Which other methods to send mail from a form? I just know mail function, like: mail(string to,
    string subject, string message) Has anyother?....
  6. How To Check If Fsockopen Function Is Enabled?
    (2)
    Hi, I have VPS (virtual private server) and I have access to php.ini file. Is there any script that
    will show that fsockopen function is enabled or where do I have to enable it? Searched google and
    here and couldn't find anything. Thanks! ....
  7. Php Explode Function Help
    (4)
    I am having trouble creating a script, all i want to achieve is to: 1. Select the variable from my
    mysql database, which is in a format of : id|id|id|id| and so on... 2. Split them into separate
    variables by using : $songexploded = explode("|",$ttyo ); 3. Then this is the bit I'm
    stuck on trying to create a while loop from the $songexploded variables. So(this might not be
    correct but you should get the idea).. CODE $x=1; while ($songexploded
    ==$result) echo $songexploded[$x].'<br>'; }....
  8. The Best Zip Function
    (1)
    hi my 6th code is very useful, you can zip your file by this: CODE <? class dZip{     var
    $filename;     var $overwrite;          var $zipSignature =
    "\x50\x4b\x03\x04"; // local file header signature     var
    $dirSignature = "\x50\x4b\x01\x02"; // central dir header signature
        var $dirSignatureE= "\x50\x4b\x05\x06"; // end of central dir
    signature     var $files_count  = 0;     var $fh;          Function
    dZip($filename, $overwri....
  9. Encrypt Functions
    (0)
    Here is my 3rd Code: By this code you can encrypt your text: CODE <?php $a =
    md5("hello"); $b = base64_encode("hello"); $c =
    base64_decode("hello"); print "md5 = ".$a." base64_encode =
    ".$b." base64_decode = ".$c; ?> EASY!....
  10. Mail() Clone
    A PHP mail() function clone (5)
    A lot of free web hosts have disabled the mail() function so you cannot send emails using PHP. Does
    anybody know of a script that makes a function "like" mail but is able to be installed in a web
    accessible directory and called included into another script and called like that? Or maybe you know
    how to make such a function? I just really need to find a way around the free hosts turning of the
    mail() function. I need to figure out a way to send emails.....
  11. [php](simple) Using Functions To Combine Values In A Form
    Really simple example on how to combine values with function (2)
    I just learned this simple method on how to use functions to combine two values from a form. First
    we create ourselves a simple POST form CODE <form method="POST"> Name:
    <input type="text" name="nickname"> Location: <input
    type="text" name="location"> <input type="submit"
    value="Input"> </form> Now we add this php to that same file CODE
    <?php $nick = $_POST['nickname']; $location =
    $_POST['location' ....
  12. Problem With Global Arrays
    (2)
    I've got a problem with global variables. /sad.gif" style="vertical-align:middle" emoid=":("
    border="0" alt="sad.gif" /> I've read and re-read the stuff about that on php.net and did that
    a few times more, and I'm still not able to make it work. Could somebody please fix my code?
    This is the primary page: include "getLvlFromHigh.func"; $user = $_GET ; $skill =
    $_GET ; $goal = $_GET ; $userXp = getLvlFromHigh($user); $xpTable =
    array( 31 => 14833, 32 => 16456, 33 => 18247, 34 => 20224, 35 => 22406, 36 =>....
  13. [php] Header Function
    (2)
    Header function Greetings we are going to use the header() funtion to redirect start making a
    file called page.php at the top of the file add CODE <?php ?> Example 1 After
    CODE header('Location: http://www.trap17.com'); the LOCATION means
    where you want it to go. Example 2 you also can define a file that you want to redirect to After
    CODE header('Location: index.php'); Example 3 you also can add a timer to
    it /laugh.gif" style="vertical-align:middle" emoid=":lol:" border="0" alt=....
  14. Error With Joomla Template
    cant find function (1)
    Hello! I am working on my template in Dreamweaver and i am using joomla extensions for
    dreamweaver! When i start my page with joomla stand alone server(jsas) i get this errors on the
    bottom of the page! QUOTE Warning:
    mosloadcomponent(w:/www/Joomla/components/com_banner/banner.php) : failed to open stream: No such
    file or directory in w:\www\Joomla\includes\frontend.php on line 66 Warning:
    mosloadcomponent(w:/www/Joomla/components/com_banner/banner.php) : failed to open stream: No such
    file or directory in w:\www\Joomla�....
  15. The Extract() Function
    Something I just found out (6)
    The extract() function is used in PHP to take an array and split it up into variables. MySQL
    queries can be parsed this way. Below is an example. CODE $query =
    mysql_query("select username, password from users where uid=1"); $result =
    mysql_fetch_array($query, mysql_assoc); extract($result); print "Your
    username is : $username"; The extract() function works for ANY array, including
    $_POST, and $_GET. Makes processing form data a LOT easier /biggrin.gif"
    style="vertical-align:midd....
  16. How Good Is This Data Cleaning Function?
    (2)
    Hi all, this is my first function and as part of a script and i just want to know a couple of
    things. here is the code for the function: CODE <? function
    clean($dirty_string) { $muddy_string = stripslashes($dirty_string);
    $murky_string = strip_tags($muddy_string); $clean_string =
    htmlentities($murky_string);      }; ?> So the first thing is how secure is
    it? the script this will be used in connects to a database and sends an email so it needs to stop
    SQL injections and any email ab....
  17. Explode Function Help
    need help from you programmers! (1)
    /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> Hi I am robert I
    need some help with some php coding. I am stuck with a explode function. Here is the code:
    $username = $check ; $query="SELECT `buildings` FROM `authuser` WHERE
    `uname` = '$username'"; $result=mysql_query($query);
    $result=mysql_result($result,0);
    list($building1,$building2,$building3,$building4,$building5,$building6
    ,$building7,$building8,$building9,....
  18. Basic Tutorial On Arrays
    (3)
    In php u can declare arrays by two methods 1. Using the Array function 2. Using the Array
    Identifier Array Function To define an array consisting of three elements using the array
    function we write the following code CODE $arrayname=
    array("basic","array","tutorial"); This declares an array with
    the name $arrayname containing 3 elements. To access these elements u have to use the array
    name along with the index in square brackets. ** Note: php arrays start from 0 index To print
    the elements of the above array....
  19. Gd Functions
    Questions (2)
    Hi all I want begin a new project , my new project is Photo blog ( weblog and image album ) in one
    script for example you can post many image in one post at once case . so its very good and very easy
    but we have some problems . if you can plz help me to fix this problems for example : we need
    change image size for page speed ( we dont want show 16 image in one page ) * we need GD functions
    to change image size to smaller and then show smaller image at page if you know source or tutorial
    about change image size plz post here . thankx we know we can change image size (w....
  20. Regexp Function Preg_match_all()
    preg_match_all() - Help me (0)
    Hi, I got a new problem which has caused me to go mad but no solution. preg_match_all() - is the
    problem. I have something like this: CODE [ol] [li]Test1[/li]
    [li]Test2[/li] [li]Test3[/li] [li]Test4[/li]
    [li]Test5[/li] [/ol] Some text.Some text.Some text. [ol]
    [li]Test1[/li] [ol] [li]Test1[/li]
    [li]Test2[/li] [li]Test3[/li] [li]Test4[/li]
    [li]Test5[/li] &....
  21. Wappy's Php Snippets
    I will place here usefull php snippets and functions that i learn/use (13)
    Here is a function you can use to generate a simple random password for whatever use ;-) CODE
    <? function rand_pass($numchar){ $string = str_shuffle
    ("abcdefghijklmnopqrstuvwxyz1234567890"); $password = substr ($string,
    1, $numchar); return ($password); } //example echo
    rand_pass('8'); // will return an 8 character long random password of numbers and
    letters like c8k4ss42 ?> CODE tags added. Here is an extremly usefull search function
    that will search a directory and....
  22. Converting Characters In A Variable To Individual Values In An Array
    turning variables into arrays (2)
    Say I have a variable such as $nav_item and it had to contents Home . IE: CODE
    $nav_item = 'Home'; How would I make so that $nav_item was an array and
    had the following contents? CODE $nav_item = array ('h', 'o',
    'm', 'e'); With the case changing (ie H would become h and U
    would become u ) EDIT: Okay found out that I could change the case with
    array_change_key_case ($nav_item, CASE_LOWER); ....
  23. Question About The Mail() Function
    (2)
    Hi, Is there any way of using the mail() function with an SMTP connection? Is there any way of
    sending messages let's say for example using an email of yahoo? Any help about this woul be very
    thankfull. Thanks in advance.....
  24. How To Enable Mail() Function In Php
    (1)
    im just trying to send mail by using a very simple php function mail() but it is not working.the
    format is CODE $to = "email@example.com"; $subject = "Hi!";
    $body = "Hi,\n\nHow are you?"; if (mail($to, $subject,
    $body)) {   echo("<p>Message successfully
    sent!</p>"); } else {   echo("<p>Message delivery
    failed...</p>"); } I think there is something wrong with php.ini
    setting..maybe something to do with SMTP ....
  25. Some Php Functions Explaination Required
    (2)
    Can some one please tell me what is the purpose of the following functions , although there's a
    little explaination with everyline but i cant understand, can some one exaplin it bit clearly and
    tell me that why it is needed in config.inc.php.. what is its purpose and will it work if i dont
    include these files in config.inc.php thanks QUOTE ### Url were Website has been installed, not
    '/' in end! define('C_URL','http://www.test.com/Website'); ### Internal
    path to Website directory define('C_PATH','Z:/home/www.test.com/www/....
  26. Include File.php?id=something
    using the include() function (13)
    Well, I am making a full CMS system for my site, and want to make the index.php file to include the
    view.php?id=1 file. I tried with this code, but it didn't work: CODE <?php include
    'view.php?id=1' ?> This is the error I get: CODE Warning:
    main(view.php?id=1) [function.main]: failed to open stream: Invalid argument
    in C:\server\xampp\htdocs\test\index.php on line 1 Warning:
    main() [function.include]: Failed opening 'view.php?id=1' for inclusion
    (i....
  27. How To Use A Link To Call Function In Php?
    (8)
    The title says it all, really. How do you call a function using in PHP? I'm doing a project
    and I stumbled upon this problem. I don't want to use query string in the href part like
    since that would mess up the other part of my code. Can anyone pleae help me? I've pasted the
    code below. /smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /> Thanksh.
    CODE <?php   function display($x){ //coding goes here.   } ?>
    <html> <body> <p align="center"> <a href="what g....
  28. php header() function help needed
    automatic re-direct (4)
    hey ppl, u seem to have real gud knowledge about php, i just wanted a little help...i designed this
    website, but i want that if i click on certain page, it should open for some few seconds and then
    browser should automatically redirect me to some other page....i tried this with header() function
    but i couldnt do the wait n redirect part, ... so somebody plz help.... -thanx in advance!....
  29. Need Help With Php
    GET function with timer (2)
    I need some help on creating a timer that every thirty minutes, refreshes on a URL. I know how to
    get the page, but I have no idea how to create a timer that initiates it. Could someone point me on
    a helpful direction?....
  30. Getting List Of Directories And Files Using Php
    PHP Function for Directory and File List (6)
    is there a php function that lists the content of some folder.... example: /New folder new.txt
    left.gif download.zip dc.exe ....so is there..? /rolleyes.gif' border='0'
    style='vertical-align:middle' alt='rolleyes.gif' /> ....

    1. Looking for arrays, function, arrays, functions,

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for arrays, function, arrays, functions,

*MORE FROM TRAP17.COM*
advertisement



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



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE