Results 1 to 5 of 5

Thread: [RESOLVED] Quick function question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Resolved [RESOLVED] Quick function question

    I want to do something like

    $value = update_text($value)

    function text_process($value){
    $value = trim(strip_tags($value));
    $value = addslashes($value);
    $value = htmlspecialchars($value);
    }

    Do I need to return $value in order to have it available to the code that calls the function or is it already loaded into the calling value "$value ="

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Quick function question

    You will need to return it. Or, if desired, you could pass $value by reference by making this change:

    Code:
    function text_process(&$value){
    If you did it that way, no return is necessary, and you'd only need this when calling it:

    Code:
    text_process($value);
    (Your sample function call is different from your function definition here - I'm sticking with "text_process" for both...)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Re: Quick function question

    Quote Originally Posted by SambaNeko View Post
    You will need to return it. Or, if desired, you could pass $value by reference by making this change:

    Code:
    function text_process(&$value){
    If you did it that way, no return is necessary, and you'd only need this when calling it:

    Code:
    text_process($value);
    (Your sample function call is different from your function definition here - I'm sticking with "text_process" for both...)
    Oops sorry, still playing around with names to get some sort of logic into what functions are called.

    So if I modify the variable I pass in the function list the changes are reflected in the calling script?

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Quick function question

    Quote Originally Posted by KiwiDexter View Post
    So if I modify the variable I pass in the function list the changes are reflected in the calling script?
    You're not really modifying the variable, just changing what the function does with the variable...

    The default behavior of a function is to take input variables by value. Like this:

    Code:
    function addTwo($number){
      return $number + 2;
    }
    
    $num = 1;
    $newNum = addTwo($num);
    At the end of that block of code, $newNum is 3, but $num is still 1, because the function took the value of $num, added two, then put the result into $newNum. It didn't change the value of $num.

    Adding the & prior to a variable in the function definition makes the function take the variable by reference instead. Which changes things...

    Code:
    function addTwo(&$number){
      $number += 2;
    }
    
    $num = 1;
    addTwo($num);
    In this case, the value of $num at the end of the code is 3, because the function took a reference to the variable $num, and added two to the variable that was referenced.

    So, as said, you could do it either way - you can add a return to get the altered value from your function, or add the & to have it alter your variable directly.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Re: Quick function question

    Thanks for the answer dude, think I'll settle on the "return" option as it makes it more readable.

Posting Permissions

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



Click Here to Expand Forum to Full Width