0

I have a php file with different functions in it. I need to get data from strings in a function, but the strings have been specified in a different function. How can this be done please?

... To clarify, I have two functions.

function a($request) { $username = ...code to get username; } 

the username is over retreivable during function a.

function b($request) { } 

function b need the username, but cannot retrieve it at the point its called, so need it from function a. I am very much a beginer here (so bear with me please), I tried simply using $username in function b, but that didn't work.

Can you please explain how I can do this more clearly please. There are another 5 strings like this, that function b needs from function a so I will need to do this for all the strings.

...Code:

    <?php
class function_passing_variables {

    function Settings() {       
        //function shown just for reference...
        $settings = array();
        $settings['users_data'] = array( "User Details", "description" );
        return $settings;
    }

    function a( $request ) {
        //This is the function that dynamically gets the user's details.
        $pparams = array();
        if ( !empty( $this->settings['users_details'] ) ) {
            $usersdetails = explode( "\n", Tool::RW( $this->settings['users_data'], $request ) );

            foreach ( $usersdetails as $chunk ) {
                $k = explode( '=', $chunk, 2 );
                $kk = trim( $k[0] );
                $pparams[$kk] = trim( $k[1] );
            }
        }

        $email=$pparams['data_email'];
        $name=$pparams['data_name'];
        $username=$pparams['data_username'];
       //These variables will retrieve the details
    }

    function b( $request ) {
        //Here is where I need the data from the variables
        //$email=$pparams['data_email'];
        //$name=$pparams['data_name'];
        //$username=$pparams['data_username'];
    }
}
?>
2
  • 5
    Your question currently doesn't make any sense. Please try to clarify your question, or ideally post some code to show what you mean. Commented Nov 21, 2011 at 0:41
  • 1
    You have variable scoping problems here. It's difficult to give an accurate answer without more information, but a generic answer would be to allow your function to take in parameters and pass the strings in from the other function. Commented Nov 21, 2011 at 0:49

2 Answers 2

1

@A Smith, let me try to clarify what you mean.

  1. You have several variables, example : $var1, $var2, etc.
  2. You have two function (or more) and need to access that variables.

If that what you mean, so this may will help you :

global $var1,$var2;  

function a($params){

   global $var1;
   $var1 = 1;
}

function b($params){  

   global $var1,$var2;

   if($var1 == 1){
      $var2 = 2;
   }
}

Just remember to define global whenever you want to access global scope variable accross function. You may READ THIS to make it clear.

EDITED

Now, its clear. Then you can do this :

class function_passing_variables{
   // add these lines
   var $email = "";
   var $name = "";  
   var $username = "";  
  // ....  

Then in your function a($request) change this :

$email=$pparams['data_email'];
$name=$pparams['data_name'];
$username=$pparams['data_username'];

to :

$this->email=$pparams['data_email'];
$this->name=$pparams['data_name'];
$this->username=$pparams['data_username'];

Now, you can access it in your function b($request) by this :

echo $this->email;
echo $this->name;
echo $this->username;
Sign up to request clarification or add additional context in comments.

12 Comments

Hi, thanks for the link. But the string will be defined in function a, so its that string I need to reference surely, and not a global string that will be outside of the function that actually gets the username, no?
when you need to access variable accross the function, you still need to define it as global, have you try it? try just add global in front of your variable.
this didn't work, when I print_r/exit the string in function a, it displays the username, when I do it for function b, it's blank. I have double checked that the globals have been set correctly too.
why dont you post both of your function here, then we can see what's probably wrong?
The code has been shown in the original question now. It's prefferable not to use Global Variables though, after reading on them more...
|
0

In the functions where the string has been set:

Global $variable;
$variable = 'string data';

Although you really should be returning the string data to a variable, then passing said variable to the other function.

1 Comment

To clarify, I have two functions. function a($request) { $username = ...code to get username; } the username is over retreivable during function a. function b($request) { } function b need the username, but cannot retrieve it at the point its called, so need it from function a. I am very much a beginer here (so bear with me please), I tried simply using $username in function b, but that didn't work. Can you please explain how I can do this more clearly please. There are another 5 strings like this, that function b needs from function a so I will need to do this for all the strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.