Results 1 to 4 of 4

Thread: variable to function back

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default variable to function back

    How can I get the name of a variable that is passing its value to a function or how do I return/ make it global so the new value takes affect? Thanks.

    PHP Code:
    sanatize($email);
    sanatize($first_name);
    sanatize($last_name)
    function 
    sanatize($input){
        return 
    $input;

    Last edited by bluewalrus; 11-22-2010 at 12:15 AM.
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    From what you said, I think you're talking about passing by reference, it's deprecated but this is how you'd do it without using global variables:

    PHP Code:
    <?php

    $email 
    'test@example.com';
    $first_name 'Joe';
    $last_name 'Bloggs';

    sanatize($email);
    sanatize($first_name);
    sanatize($last_name);

    echo 
    $email '<br />';
    echo 
    $first_name '<br />';
    echo 
    $last_name '<br />';

    // Use the & sign to pass by reference, which will affect the variable you pass in
    function sanatize(&$input){
        
    $input $input '_alterted';
        return 
    $input;



    ?>
    I added the "_altered" bit just so you can see the variables are affected outside the function. Let me know if this isn't what you meant.

  3. The Following User Says Thank You to Schmoopy For This Useful Post:

    bluewalrus (11-22-2010)

  4. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I don't think passing by reference is depreciated, just the practice of doing it in the function call (though I may be reading it wrong, the wording is a little confusing):
    PHP Code:
    $var 'something';

    alter($var);

    function 
    alter(&$input){
        
    $input $input '_alterted';
        return 
    $input;

    is valid, while
    PHP Code:
    $var 'something';

    alter(&$var);

    function 
    alter($input){
        
    $input $input '_alterted';
        return 
    $input;

    is not. Both methods work, but the second issues the "depreciated" warning.

  5. The Following User Says Thank You to traq For This Useful Post:

    bluewalrus (11-22-2010)

  6. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Yup that did it thanks.
    Corrections to my coding/thoughts welcome.

Bookmarks

Posting Permissions

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