Jump to content

function question


ngreenwood6

Recommended Posts

I have a quick question that I am confused about. I am trying to set a variable inside of a function and make it stick within that page. Here is an example:

 

<?php

$my_data = "not there";

this_data($my_data);

function this_data($data)
{
$data = "hello";
my_data($data);
}

function my_data($data)
{
$my_data = $data;
}

echo $my_data;

?>

 

However I want $my_data to return "hello" instead of "not there". Any help is appreciated.

Link to comment
Share on other sites

It will stay within the function unless you return something.  You can always return an array if you want to have more values sent out.

 

<?php

$my_data = "not there";

$my_data = this_data($my_data);

function this_data($data)
{
$data = "hello";
return my_data($data);
}

function my_data($data)
{
$my_data = $data;
return $my_data;
}

echo $my_data;

?>

 

I changed about 4 lines of code in there.  Let me know if that works for you.

Link to comment
Share on other sites

Sorry but I dont think I gave enough information. The this_data() function I am not able to run. However when this runs I want to be able to get the data variable contains before it is finished running that function. I want it to be accessible throughout the whole page. Something like this:

 

<?php
$my_data = "hello";

function this_data($data)
{
$some_data = "world"; // need this before it is run
get_data($data);
$some_data = "not now";
}

function get_data($data)
{
$my_data = $data;
}

echo $my_data; // should be world

Link to comment
Share on other sites

<?php
function this_data($data) {
    $some_data = "world"; // need this before it is run
    return $some_data = "not now";
}

$my_data = this_data('a pointless string');

echo $my_data; // should be world

 

As KingPhilip said you want to use a global, or you could pass a variable as reference.

Link to comment
Share on other sites

Ok I dont think any of you are actually reading the post. I do not have access to the function. It is being called from another page and I do not want to mess with that function. However there is a variable that is used inside of there that I want to use somewhere else on that page. I do not get the globals thing. I tried it and it doesnt seem to work.

 

$my_string = "hello";

function this_data()
{
$my_data = "world";
global $my_string;
$my_string = $my_data;
}
echo $my_string; // should be world

 

Still is outputting "hello"

Link to comment
Share on other sites

No, it should be "not now"

 

I didn't change his comments, I'm not sure he understand what he wants anyway.

 

And yes, we are reading your posts, if you read some of ours you would've come up with this solution already, in actual fact you, have, you're just not calling the function.....

 

 

Link to comment
Share on other sites

Make sure you're calling it before you echo the variable, otherwise it obviously wont have been changed.

 

That's like driving to a gas station and asking why the tank is empty before you fill it up, think about it!

Link to comment
Share on other sites

Actually, if you go by original comment

 

Make sure you're calling it before you echo the variable, otherwise it obviously wont have been changed.

 

What I was saying is you have a starting point and an end point. You can't expect the end point to have happened before you change anything (before you call the function, before you fill up your car).

 

The analogy stands

Link to comment
Share on other sites

Yeah this is related to the other topic. Here is the email:

 

Hello nick, the topic "testt" at "mysite" has received a new reply

since your last visit. You can view this post by clicking on the following

link:

http://mysite.com/forums/viewtopic.php?f=85&t=621&p=3078&e=3078

 

Topic: testt

Author: Rukus

Message:

 

 

 

If you want to view the topic, click the following link:

http://mysite.com/forums/viewtopic.php?f=85&t=621

 

If you no longer wish to watch this topic, either click the "Unsubscribe

topic" link found in the forum above, or click the following link:

 

http://mysite.com/forums/viewtopic.php?uid=53&f=85&t=621&unwatch=topic

--

Thanks, The Team

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.