How to take the name of the variable used in the function argument?

Asked

Viewed 1,501 times

4

  • Script1.php

Here script1.php calls the function that is in script2:

sendData($variavel);
  • Script2.php

The function takes the value and includes script 3 to receive argument values:

sendData(){
    $getFirstArgumentValue = func_get_arg(0);
    // $getVariableName = ...
    sendVariableNameAndValue($getFirstArgument);
    // global $variavel;
    include('script3.php');
}
  • Script3.php

In script3.php the variable called in script1.php is declared, and it is noticeable that an error message appears:

echo($variavel);

Error message appears in script3 warning that $variable has not been declared.

The question is:

How do I declare in script3, the same variable that was declared in script1, passing the function declared in script2 and receiving that value in script3?

In this case the sendData function would have to take the value of the argument and also the name of the variable used to send this value?

It would be possible to send the value and variable name?

  • 3

    Just out of curiosity, where would you wear this?

  • I have detailed my answer better, see if any of the options fits your question...

  • There are better solutions to pass arguments... if you can post what application you are doing

  • As an alternative solution, I’m using an array. $data['variavel'], however it is still necessary to declare $data in script3.php. And I wanted to declare in script3.php, any variable that I want, but has the same name as the variable in script1.php

  • Describe the function of your page script3.php, what she does exactly... The example you gave is very superficial

  • Then use $GLOBALS['variable'] it is super global and does not need to be declared and will have the same value of $variable as script1.php

  • 1

    user14319, a hint put all code!

Show 2 more comments

5 answers

1


With the PHP 5.6 update, the "Variadic functions via ..."
I was able to create a clean, clear code:

  • Script1.php

    $variavel = 'foo';
    sendData($variavel);
    
  • Script2.php

    public function sendData(...$variables){
        foreach($variables as $variableValue){
            foreach($GLOBALS as $globalName => $globalValue){
                if($globalValue === $variableValue){
                    global $$globalName;
                }
             }
        }
    
        include('script3.php');
    }
    
  • Script3.php

    echo($variavel);
    

For this code to work, the function must be called with the variable in a global scope. For example: if the function is called inside another function this code does not work, unless you declare the sent variable as global. eCode:

sampleMethod(){
    $variavel = 'bar';
    global $variavel;
    sendData($variavel);
}

There is another method to get the variable name with debug_backtrace(), but I think the question has already been answered.

0

I think it is not possible, the only alternative that occurs to me, would be to pass the variable name as string in the parameter:

$variavel = 'Olá mundo!';

sendData('variavel');

function sendData(){
    $getFirstArgumentValue = func_get_arg(0);
    global $$getFirstArgumentValue;
    include('script3.php');
}

script3.php

<?php

echo($variavel);

?>

But anyway you will be limited to variable always have this name: $variavel and I think your problem is that you need to apply the function sendData() to other variables, so you should use something like this:

$outra_variavel = 'Olá mundo!';

sendData($outra_variavel);

function sendData($data){
    include('script3.php');
}

script3.php

<?php

echo($data);

?>

Note that in this example you do not need to define the variable $data as global, because by doing the include('script3.php'); within the function sendData she will be in the same scope, ie it is the same as:

$outra_variavel = 'Olá mundo!';

sendData($outra_variavel);

function sendData($data){
    echo($data);
}

That by the way would be a great solution, I particularly never used includes within functions, because in addition to getting disorganized, every call of the function PHP will reload the file from disk...

  • 1

    Actually this does not solve. The solution would be to send the variable name recursively'.

  • Thanks for the contribution, but the question has already been finalized (answered)!

0

in script3 declare the variable with the same name global, class or not that will access script3 will work!

  • Doesn’t solve the problem.

  • Thanks for the contribution, but the question has already been finalized (answered)!

0

What you probably want to do is have a template script3.php and load the necessary information through the function sendData, if that is the case you can do as follows.

function sendData(array $data = array()) {
    extract($data);
    include 'script3.php';
}

You’ll have to define a array style:

$myData = array(
    'var1' => 'valor',
    'var2' => 'xpto',
);

So you can call the function sendData($myData);

So you can use the variables you need on script3.php, ex:

echo $var1;

if (isset($var2)) {
    echo 'var2 também existe com o seguinte valor: ' . $var2;
}

PS: If you want the function sendData return the output generated by script3 as a string, you can do the following:

/**
 * Sends some data
 * @param  array  $data Variáveis a utilizar
 * @return string       Resultado
 */
function sendData(array $data = array()) {
    ob_start();
    extract($data);
    include 'script3.php';
    return ob_get_clean();
}
  • Thanks for the contribution, but the question has already been finalized (answered)!

0

Try it like this:

script1.php

$data['variavel'] = 1;
getData($data);

or else

getData(array('variavel'=>1));

script2.php

function getData($arr){
     extract($arr);
     include 'script3.php';
}

script3.php

echo $variavel;

Any input you set in the array that goes as parameter for the function getData() will be created by the command extract() in the script3.php file

Documentation: http://php.net/extract

  • Thanks for the contribution, but the question has already been finalized (answered)!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.