11Tami Posted April 16, 2007 Share Posted April 16, 2007 Hello, can you put a function on a variabe in php? Such as: $t = function a(){} Do something with $t? Please let me know, thanks. Quote Link to comment Share on other sites More sharing options...
wqamar Posted April 16, 2007 Share Posted April 16, 2007 You can retrn value from function and store in $t variable but not direct define. Quote Link to comment Share on other sites More sharing options...
btherl Posted April 16, 2007 Share Posted April 16, 2007 $in = 5; $out = f($in); print "f($in) = $out\n"; function f($in) { return $in + 5; } Does that example help? I'm a little unclear on what your question is. Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted April 16, 2007 Share Posted April 16, 2007 Why would you need to define a function on a variable? Just define the function, and then when you need to use it, call it. Like: function foo(){ echo "Sup Foo!"; } foo(); Quote Link to comment Share on other sites More sharing options...
11Tami Posted April 16, 2007 Author Share Posted April 16, 2007 I'm getting hung up on a few things because everytime I turn around the php wants me to use a variable $. I guess this is what I need. You can retrn value from function and store in $t variable but not direct define. How would I do that? How would I return value from separate function and then store it in $t variable? function a(){ echo "this is a"; } a(); How to get a() to $t? Quote Link to comment Share on other sites More sharing options...
btherl Posted April 16, 2007 Share Posted April 16, 2007 You must use "return" in your function, as in my earlier post. function a(){ return "this is a"; } $t = a(); echo $t; Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted April 16, 2007 Share Posted April 16, 2007 You must use "return" in your function, as in my earlier post. function a(){ return "this is a"; } $t = a(); echo $t; Yes, if you want to define a variable using the function, but it's not a must to use "return" in the function. I have many functions that do not "return" a value, but are useful nonetheless. A simple example: function showLoginForm(){ echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST"> Username: <input type="text" name="username" /><br> Password: <input type="password" name="password" /><br> <input type="submit" name="login" value="Log In!"> </form>'; } // Show the login form showLoginForm(); ... but again, yes you can define a variable by using a function return value. Take the above example, modified to return a value: function showLoginForm(){ $value = '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">' .'Username: <input type="text" name="username" /><br>' .'Password: <input type="password" name="password" /><br>' .'<input type="submit" name="login" value="Log In!">' .'</form>'; return $value; } $t = showLoginForm(); // Show the login form echo $t; ... not the most efficient way in this case though ... :-\ Quote Link to comment Share on other sites More sharing options...
11Tami Posted April 16, 2007 Author Share Posted April 16, 2007 Thanks a lot everyone, that should help. Helps me find a way to give the variables that so many codes want so bad. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.