www.webdeveloper.com
Results 1 to 3 of 3

Thread: Function that echo values, how to set them to variable?

  1. #1
    Join Date
    Jan 2003
    Location
    Egypt
    Posts
    185

    Function that echo values, how to set them to variable?

    Hi,

    I have a class and there is a method of this class its output is get out by echo. What I need is to assign the echo value to a variable without printing it out!

    Is there any way to cache the output of this function into a variable? I don't want to change the coding of the function.

    For examle:

    class MyClass(){
    function result(){
    echo "Hello World";
    }
    }

    Usually the above example will print Hello World. I want something like the follwoing:

    $obj = new MyClass();
    $v = $obj->result(); // I don't want Hello World to be printed here???
    /* after some logic */

    echo $v; Here I want Hello World to be printed!!!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,537
    Change the echo to return.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2010
    Posts
    2,803
    PHP Code:
    class MyClass(){
         function 
    result(){
             return 
    "Hello World";
        }

    and then

    PHP Code:
    $obj = new MyClass();
    $v =  $obj->result();

    // add -  echo $v - wherever you want "Hello World" to appear 
    Last edited by tirna; 07-21-2010 at 09:20 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles