1

The following function is part of code written into the core of a plugin I am reverse engineering. The problem with it is that I need to do an str_replace on it and I cannot because it is already set to echo.

The function is.

function similar_posts($args = '') {
    echo SimilarPosts::execute($args);
}

I call it in my pages using similar_posts(), but what I really need to do in my theme is call $related = similar_posts(), however that function is set to echo. How do I change that.

I tried this.

 function get_similar_posts($args = '') {
        SimilarPosts::execute($args);
    }

But that did not produce any results.

1
  • try return SimilarPosts::execute($args);, then you can do the $related = similar_posts() Commented Dec 14, 2011 at 14:03

6 Answers 6

2
function get_similar_posts($args = '') {
    return (SimilarPosts::execute($args));
}
Sign up to request clarification or add additional context in comments.

5 Comments

return is a keyword, not a function. Please don't wrap whatever being returned in ().
It always looks wrong to me when it's not, and I don't see any reason why I shouldn't.
It's prone to future errors, if you (as an example) would like to return a variable my reference but wrap it in (), it will not work.
PHP's own manual advises against returning by reference unless you absolutely have to anyway. Objexts are always passed by reference these days, and things like arrays utilise Copy On Write so there's rarely anything to be gained by returning by reference.
PHP's own manual also advises using return without (), php.net/manual/en/function.return.php
2

If you want to use the value SimilarPosts::execute ($args) returns, you'll need to use the keyword 'return' inside your get_similar_posts.

function get_similar_posts ($args = '') {
  return SimilarPosts::execute($args);
}

If you are unable to change the definition of get_similar_posts there are ways to snatch the content printed by similar_posts even though it's "set to echo".

This can be accompished by using the Output Control Functions available in PHP.

function echo_hello_world () {
  echo "hello world";
}

$printed_data = ""; 

ob_start (); 
{
  echo_hello_world (); 

  $printed_data = ob_get_contents (); 
}
ob_end_clean (); 

echo "echo_hello_world () printed '$printed_data'\n";

output

echo_hello_world () printed 'hello world'

Comments

1

Use return instead of echo.

So that you have:

 return SimilarPosts::execute($args);

instead of:

 echo SimilarPosts::execute($args);

Comments

1

Wrap the function inside another in which you use output buffering.

1 Comment

The OP is able to change the code as he is reveries engineering it. SO buffer control is the wrong way I reckon.
1

Done it..

function get_similar_posts($args = '') {
    return SimilarPosts::execute($args);
}

and on the page get_similar_posts();

Should have thought of that.

Comments

1

return from the function:

function get_similar_posts($args = '') {
     return  SimilarPosts::execute($args);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.