the_oliver Posted August 6, 2010 Share Posted August 6, 2010 Hi, Im trying to pass a variable ($in_this_instance) into, and then back out of a function. The variable goes into the function no problem, and is echo'ed out fine. However the echo after the close of the function, does not give anything out. $in_this_instance = 'boo'; function in_this_instance($data) { global $in_this_instance; echo $in_this_instance; $in_this_instance = 'hoo'; $rep_val = '[front banner]'; $test = strpos($data, $rep_val); if ($test === false) { return $data; } else { return $data;; } } add_filter('the_content', 'in_this_instance'); echo $in_this_instance; Could any one give me a pointer please? Many Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/209963-cant-pass-variable-out-of-function/ Share on other sites More sharing options...
trq Posted August 6, 2010 Share Posted August 6, 2010 Firstly globals are a terrible idea. Secondly your function returns $data. Thirdly, what is add_filter() meant to do? Your code is seriously all over the place. Quote Link to comment https://forums.phpfreaks.com/topic/209963-cant-pass-variable-out-of-function/#findComment-1095892 Share on other sites More sharing options...
joel24 Posted August 6, 2010 Share Posted August 6, 2010 ... and as Thorpe said, your function returns $data, however, you're not passing a $data variable to the function. should be something like $data = "testing string"; //function code here echo in_this_instance($data); Quote Link to comment https://forums.phpfreaks.com/topic/209963-cant-pass-variable-out-of-function/#findComment-1095896 Share on other sites More sharing options...
onlyican Posted August 6, 2010 Share Posted August 6, 2010 Dont know the point in this function As it does not matter what your "test" result is, u always return what you entered. $in_this_instance = 'boo'; function in_this_instance($data) { $in_this_instance = 'hoo'; $rep_val = '[front banner]'; $test = strpos($data, $rep_val); if ($test === false) { return $data; } else { return $data;; } //Dont need global constant // simply call as follows $newVar = in_this_instance($in_this_instance); echo $newVar; //Should echo hoo Quote Link to comment https://forums.phpfreaks.com/topic/209963-cant-pass-variable-out-of-function/#findComment-1095911 Share on other sites More sharing options...
the_oliver Posted August 6, 2010 Author Share Posted August 6, 2010 Thanks for you replies. I should have explained that "add_filter('the_content', 'in_this_instance');" is a wordpress hook which calls the function supplying $data. It requires that at $data is the returned by the function. All I what to be able to do is alter the value of "$in_this_instance" with in the function, and then be able to retrieve the altered value outside the function. Normally i would do this with a return but as its the function is being called by the hook i can't go down this route. Hope thats a better description? Many Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/209963-cant-pass-variable-out-of-function/#findComment-1095913 Share on other sites More sharing options...
the_oliver Posted August 6, 2010 Author Share Posted August 6, 2010 Perhaps this code makes more sense and will be more helpful! $in_this_instance = 'false'; function in_this_instance($data) { global $in_this_instance; $rep_val = '[front banner]'; $test = strpos($data, $rep_val); if ($test === false) { return $data; } else { return $data; $in_this_instance = 'true'; } } add_filter('the_content', 'in_this_instance'); echo $in_this_instance; Quote Link to comment https://forums.phpfreaks.com/topic/209963-cant-pass-variable-out-of-function/#findComment-1095915 Share on other sites More sharing options...
onlyican Posted August 6, 2010 Share Posted August 6, 2010 no, actually I am more confused. What is add_filter()? This is a function that you are calling Still, You pass data into your function, then return Data without changing the variable. Why???? I think you want something like the following function in_this_instance($data){ $rep_val = '[front banner]'; $test = strpos($data, $rep_val); if($test === false){ return false; }else{ return true; } $strToCheck = 'This is [front banner]'; //Check if true if(in_this_instance($strToCheck)){ //The test within function was true }else{ //The test within function was false } Note this code my have bugs, I have not tested it, I simply copied the above example and changed the lines required, I am trying to help with the bug not build the code for you Quote Link to comment https://forums.phpfreaks.com/topic/209963-cant-pass-variable-out-of-function/#findComment-1095924 Share on other sites More sharing options...
JasonLewis Posted August 6, 2010 Share Posted August 6, 2010 Perhaps this code makes more sense and will be more helpful! $in_this_instance = 'false'; function in_this_instance($data) { global $in_this_instance; $rep_val = '[front banner]'; $test = strpos($data, $rep_val); if ($test === false) { return $data; } else { return $data; $in_this_instance = 'true'; } } add_filter('the_content', 'in_this_instance'); echo $in_this_instance; You are returning data, then settings $in_this_instance to false. Doesn't work like that. Anything after return will not be executed. Also, you do realize you're setting it to (string) false rather then (bool) false. Same with when you set it to true. Remove the single quotes to make it a boolean. Quote Link to comment https://forums.phpfreaks.com/topic/209963-cant-pass-variable-out-of-function/#findComment-1095928 Share on other sites More sharing options...
the_oliver Posted August 6, 2010 Author Share Posted August 6, 2010 Hi onlyican, add_filter() is a word press hook that calls the function. It passes the content of the page being requested by a user, through the specified function. - In this case in_this_instance(). It passes the variable $data to the function, containing all the page content. The content then has to be returned, hence i am doing "return $data;" regardless of the out come. All i want to achieve with this function is to determine weather a string ($rep_val) is present in $data and, if it is, return true as "$in_this_instance". The return form the function has remain the same as what was originally passed to it. @ProjectFear. - Thanks for that, i have placed the "$in_this_instance = 'true';" above the return but the result is still not available out side the function. ($in_this_instance remains as false.) If i put an echo in just before each return, (just to debug), it shows that the "$test = strpos($ ...." bit is working. Ta! Quote Link to comment https://forums.phpfreaks.com/topic/209963-cant-pass-variable-out-of-function/#findComment-1095942 Share on other sites More sharing options...
onlyican Posted August 6, 2010 Share Posted August 6, 2010 OK, so you are not manipulating $data, so you do not need to return this If you want to check if value in String then simply have as I posted before, returning true or false You could even if you wanted to have $in_this_instance = in_this_instance($data); Which would then have your True / False Quote Link to comment https://forums.phpfreaks.com/topic/209963-cant-pass-variable-out-of-function/#findComment-1095953 Share on other sites More sharing options...
DavidAM Posted August 6, 2010 Share Posted August 6, 2010 add_filter() is a word press hook that calls the function. You do realize that add_filter('the_content', 'in_this_instance'); is most likely, not actually executing the function in_this_instance(). I have no experience with wordpress, but from your description, you are providing a callback function for the wordpress process to use. It will call that function when it is ready to - must likely NOT when you add it as a filter. As a result, your global variable is not going to get updated immediately after adding it to the filter list. Whatever it is you want to do when $in_this_instance is true, should probably be done inside your function. Quote Link to comment https://forums.phpfreaks.com/topic/209963-cant-pass-variable-out-of-function/#findComment-1095963 Share on other sites More sharing options...
KevinM1 Posted August 6, 2010 Share Posted August 6, 2010 First, again, 'false' is not the same as false. The first is a string which, if tested in a conditional, will actually return true. Logical true and false do not have quotes around them. Second, again, global is a bad idea. Instead of using it, rewrite the function in question to accept an extra parameter, like so: function in_this_instance($data, &$inInstance) { $rep_val = '[front banner]'; $test = strpos($data, $rep_val); if ($test === false) { $inInstance = false; return $data; } else { $inInstance = true; return $data; } } $in_this_instance = false; $data = in_this_instance($data, $in_this_instance); The '&' before $inInstance denotes a reference. This allows one to modify the value of a variable without returning it from a function, as you have a reference to the original variable rather than a copy (which is what you get with normal argument passing). More info: http://php.net/manual/en/language.references.pass.php Quote Link to comment https://forums.phpfreaks.com/topic/209963-cant-pass-variable-out-of-function/#findComment-1095966 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.