Jump to content

Recommended Posts

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!

 

Link to comment
https://forums.phpfreaks.com/topic/209963-cant-pass-variable-out-of-function/
Share on other sites

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		 

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.

 

 

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;

 

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

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.

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!

 

 

 

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

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.

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

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.