Jump to content

Variable outside function


Andy17

Recommended Posts

Heya!

 

I would like to ask how I can access a variable set in one function in another function. I know session would work, but I don't think that would be a smart choice. I did some research on globals and found it to be useless in this case (as far as I understood, I have never used them before)).

 

Here is some simple code to illustrate my problem:

 

Form with two buttons here ("mybutton" & "my2ndbutton")

<?php

function myfunction1()

{

      // MySQL query is done here and variables are set to store the results like this:

      $row = mysql_fetch_array($result);

      $title = $row['title'];

}

function my2ndfunction($title)

{

      // Here I need to access $title found in myfunction1(). Yes, I realize that that function needs to be called first. 

}


// If "mybutton" is clicked, myfunction1() is called
if (isset($_POST['mybutton']))

{

       myfunction1();

}

// If "my2ndbutton" is clicked, my2ndfunction is called and should make $title available in the function
elseif (isset($_POST['my2ndbutton']))

{

      my2ndfunction($title);

}

?>

 

I hope I made myself clear and that you can understand my problem.

 

Thank you in advance for your help.

Link to comment
https://forums.phpfreaks.com/topic/126496-variable-outside-function/
Share on other sites

You'd return the variable.

 

<?php
function func1() {
    //some stuff to get $title;
    $title = "test";
    return $title;
|
function func2($title) {
    echo $title;
}
$title = func1();
func2($title);

 

Or you could even code that last bit as:

func2(func1());

Thanks. I managed to get the variables out of there, but as a result, a new problem occurred; in the function where I find the variables I want to export from the function, I do a query and then echo the data. However, when I do like you suggested, my function would be called multiple times. For instance, it's called one time to find $title. Then when I do this...

 

$title = myfunction1();

 

... then the function is called once again, which is a problem cause I'm echo'ing the data in myfunction1() as well. So, for each variable I would export to use in another function, myfunction1() would be run one time. Maybe it's just me being stupid, I don't know. It wouldn't really be a problem if it wasn't because I'm outputting the data inside the function

 

Any way to get around this?

Except for functions that are specifically created to echo data, functions should always return the results they were crafted to produce. You can do anything with a returned value, assign it to a variable, pass it as a parameter to another function, or echo it. If your function echo's output, you are stuck with a function that will always echo output.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.