Jump to content

Recommended Posts

I have a question regarding functions returning value to other functions in php. Up until now I just make everything happen in one function, but I am finding more and more that I will need to do the same thing 5 times and rather than having the same code five times in my function i figured i would just write a function for that process and pass the value back to the original location. So here is my question, how in the world do you do that in php.

 

In C++ to pass by reference you have something like:

 

int main() {
   passReference(int & value1, int & value2);
   return 0;
}
void passReference(int & value1, int & value2) {
    .....processes here
}

 

Is this done the same way in php? what would the basic outline of a reference function look like. Use my example above and I should be able to figure it out from there. Thanks a ton!

 

Proxximo

Link to comment
https://forums.phpfreaks.com/topic/53155-solved-pass-by-reference-in-php/
Share on other sites

The OOP section is better suited for this.. as with C++ you can use pointers in PHP,

 

example

 

in PHP

<?php
/* REVERSE.PHP */

$str = ""; /* Buffer to hold reverse string */

reverse("cat", &$str); /* Reverse the string "cat" */
printf("reverse (\"cat\") = %s\n", $str); /* Display result */
reverse("noon", &$str); /* Reverse the string "noon" */
printf("reverse (\"noon\") = %s\n", $str); /* Display result */

function reverse($before, $after) {
$after=$before; /* Expand $str to length, preventing array */
$len = strlen($before);
for ($j=$len-1, $i=0; $j>=0; $j--, $i++)
$after{$i} = $before{$j};
}
?>

 

in C

/* REVERSE.C */

#include <stdio.h>

/* Function Prototype */
reverse ();

main () {
char str [100]; // Buffer to hold reverse string

reverse("cat", str); // Reverse the string "cat"
printf("reverse (\"cat\") = %s\n", str); // Display
reverse("noon", str); // Reverse the string "noon"
printf("reverse (\"noon\") = %s\n", str); // Display
}

reverse (before, after)
char *before; // A pointer to the source string
char *after; // A pointer to the reversed string
{
int i;
int j;
int len;

len = strlen (before);

for (j = len - 1, i = 0; j >= 0; j--, i++) // reverse loop
after[i] = before[j];
after[len] = NULL // NULL terminate reversed string
}

My problem to this point is I am running a function that is supposed to pull a string value from a separate function, to use later. I also use an index for all of my functions called index.php .

 

The error I am getting is "Table does not exist"

 

From what I can see the monsterDatabase function is not sending any value back or sending a blank value.  The script tests fine when the code from monsterDatabase is put right above the doquery function, however I am wanting to write it as its own function, since I have that piece of script (500 lines long) written 4 other times in the same script.

 

I tried searching for a solution via the web but only come up empty handed. Any help would be greatly appreciated.

 

Here is the code in the index.php script

 

elseif ($do[0] == "monsterDatabase") { include('fight.php'); monsterDatabase(&$monsterdb); }

 

The main function that is calling for the value is called fight()

 

function fight() {

   if ($userrow["currentfight"] == 1) {
   
     $newmonsterdb = monsterDatabase(&$monsterdb);

     $monsterquery = doquery("SELECT * FROM {{table}} ORDER BY RAND() LIMIT 1", $newmonsterdb);
     //The doquery function is working fine, tests great when I just post the script from monsterDatabase          
     //above it
}

}

 

The function that is supposed to return the value is obviously monsterDatabase and looks like this (shortened from 500 lines down to just make the point)

 

function monsterdatabase(&$monsterdb){

   if ($userrow["currentarea"] == "Outside Proxximos Fortress") {
		$monsterdb = "monsterproxximos";
   } elseif ($userrow["currentarea"] == "Training Grounds") {
		$monsterdb = "townmonster2";
   } elseif ($userrow["currentarea"] == "Outside Oasis of Otium") {
		$monsterdb = "townmonsteroasis";
   } elseif ($userrow["currentarea"] == "Outside Skyfall Peak") {
		$monsterdb = "townmonsterskyfall";
   } elseif ($userrow["currentarea"] == "Outside Aloren") {
		$monsterdb = "townmonsteraloren";
   }

   return $monsterdb;

}

 

If any more information is needed please do not hesitate to respond and I will get it up as quickly as possible. Thanks

 

Proxximo

Sorry for last responce, been busy..

 

ok just want to point somethin out

 

$newmonsterdb = monsterDatabase(&$monsterdb);

passes nothing as $monsterdb isn't set..

 

also

 

function monsterdatabase(&$monsterdb){

   if ($userrow["currentarea"] == "Outside Proxximos Fortress") {

$userrow["currentarea"]

isn't set either it should be

$monsterdb["currentarea"]

as that was passed

I figured it out!

 

Needed to have the global variables added into each function like:

 

<?php
function fight() {
  global $userrow;

   if ($userrow["currentfight"] == 1){
         $monsterdb = "";
         $monsterdb = getDatabase(monsterdb);
   }
}

function getDatabase(&$monsterdb) {
  global $userrow;
    process .......
  return monsterdb;

}

?>

 

 

Works great now, thanks to all that helped me along :)

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.