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
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.