View Full Version : Resolved variable to function back
bluewalrus
11-21-2010, 09:28 PM
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.
sanatize($email);
sanatize($first_name);
sanatize($last_name)
function sanatize($input){
return $input;
}
Schmoopy
11-21-2010, 09:36 PM
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
$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.
I don't think passing by reference (http://php.net/manual/en/language.references.pass.php) 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):
$var = 'something';
alter($var);
function alter(&$input){
$input = $input . '_alterted';
return $input;
} is valid, while
$var = 'something';
alter(&$var);
function alter($input){
$input = $input . '_alterted';
return $input;
}is not. Both methods work, but the second issues the "depreciated" warning.
bluewalrus
11-22-2010, 12:15 AM
Yup that did it thanks.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.