christofurr Posted June 22, 2007 Share Posted June 22, 2007 If a variable stores a function, could I execute the function by typing the variable alone? Or maybe by echoing it? <?php /* Set variable */ $math = round(23.89); /* Execute variable? */ $math; /* Echo variable? */ echo $math; ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 once you set $math as a value it can be used in anystatment as a parameter. Thus you can say $array[$math] = "Something"; and it uses the $math as the array key or you can say echo $math; however just saying $math; will not get you anywhere Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted June 22, 2007 Share Posted June 22, 2007 <?php /* Set variable by executing it */ $math = round(23.89); /* Echo variable */ echo $math; ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 22, 2007 Share Posted June 22, 2007 If a variable stores a function, could I execute the function by typing the variable alone? Or maybe by echoing it? <?php /* Set variable */ $math = round(23.89); /* Execute variable? */ $math; /* Echo variable? */ echo $math; ?> u are not storing the function its the result of the function that is being stored in the varialble ASTIG!!! Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 now if you make your own function say Function test() { $rounded = round(23.95); return $rounded; } and then say $math = test(); and say echo $math; it will equal 23.95. This is a good way to test if a function gets to completness by returning a parameter. if you return multiple paramets $math would be an array in this case with the keys being indexed values. Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 22, 2007 Share Posted June 22, 2007 i think the issue is this If a variable stores a function, could I execute the function by typing the variable alone? Or maybe by echoing it? ????? f a variable stores a function, could I execute the function by typing the variable alone? Or maybe by echoing it? variable dont store function remember ASTIG Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 variables store the return parameters of functions (user defined or php defined like round) if the function return is NULL or !ISSET then it simply executes said function (kinda pointless, because you can just say function_name(); to execute a function with no returns). Variables can also store strings and integers, and be linear or arrical (is that a word? i don't know but i think you get what i'm saying). in manner. Variables can be used anywhere that a parameter needs to be supplied and can be used in conjuction with static integers/strings and with other variables for example: <?php $num1 = 5.23; $math = round($num1); echo $math; //returns 5 ?> Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 22, 2007 Author Share Posted June 22, 2007 i think the issue is this If a variable stores a function, could I execute the function by typing the variable alone? Or maybe by echoing it? ????? f a variable stores a function, could I execute the function by typing the variable alone? Or maybe by echoing it? variable dont store function remember ASTIG LOL. I saw your first response, Teng. I understand now. Thanks for explaining. But, I have no idea what ASTIG is... :-\ Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 22, 2007 Share Posted June 22, 2007 YA Astig is a filipino term for cool ASTIG !!! Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 22, 2007 Author Share Posted June 22, 2007 ...Oh. Well, ASTIG back at ya. And thanks to everyone else. Was expecting a simple "Yapp!" or "Nooo", but this'll do. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 I don't like simple responses, because they always lead to follow ups Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 22, 2007 Author Share Posted June 22, 2007 I'd have probably been confused by your response if Teng weren't around, anyway. And speaking of follow ups... If the function was mysql_connect() instead of round(), it would still work if I echoed it, correct? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 check your php manual on php.net it all depends on the function return parameters, but yes its does: Return Values Returns a MySQL link identifier on success, or FALSE on failure. so if you say $var = mysql_connect(); if ($var) { //Do mysql stuff } else { echo "connection failed"; } however this function has its own error outputting so it be pointless Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 the error reporting is done by saying $var = mysql_connect() or die (mysql_error()); Quote Link to comment Share on other sites More sharing options...
trq Posted June 22, 2007 Share Posted June 22, 2007 ASTIG !!! Seriously... if I see that one more time im gonna crawl through my monitor. This is not some kiddy forum. :-) Anyway... back OT. You can store a function in a variable ready for execution.... simply.... <?php function foo() { echo "hello\n"; } $var = 'foo'; $var(); ?> This can come in pretty handy when you want to dynamically call functions or classes, though I'm not sure this is really what you meant. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 22, 2007 Author Share Posted June 22, 2007 Ugh! Confused... This is why I prefer simple responses. :-X I'll just use mysql_connect(). Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 22, 2007 Share Posted June 22, 2007 ASTIG !!! Seriously... if I see that one more time im gonna crawl through my monitor. This is not some kiddy forum. :-) Anyway... back OT. You can store a function in a variable ready for execution.... simply.... <?php function foo() { echo "hello\n"; } $var = 'foo'; $var(); ?> This can come in pretty handy when you want to dynamically call functions or classes, though I'm not sure this is really what you meant. ok im not gonna use it again and get serious but are u sure about that look you are not storing the function you store the string and use the string as a function I dont think you will use it in your coding basically that useless Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 when you get up to higher uses of php and you need to know if functions fail/pass you will come to love the $var = function and yeah i forgot about that how a var can store a function call, but thrope i trumped you earlier on that guy with the timmer question so we even tonight Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 yeah it works, because it takes the string 'foo' as a literal interputation as a parameter. sorta like using the $i++; idea you can say $var = "++" and do $i.$var; pointless but doable Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 22, 2007 Share Posted June 22, 2007 <?php function foo() { echo "hello\n"; } $var = 'foo'; $var(); ?> are you gonna use that why??? no offense but i dont understand the logic $var = 'foo'; $var(); people dont get mad Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 trust me you never see a use to something until it becomes helpful to you. Store it in the back of your php knowledge base and it will come to use one day Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 22, 2007 Share Posted June 22, 2007 ok lets cut this thing thanks to you at least i learn your way of coding Quote Link to comment Share on other sites More sharing options...
trq Posted June 22, 2007 Share Posted June 22, 2007 I dont think you will use it in your coding basically that useless Not at all useless. There can be many times when this is a godsend. I'm currently building a package manager for a Linux distro using php just for fun. The build scripts are self contained classes. My package management system excepts command line arguments, from these arguments I need to instantiate the relevent class and then start calling its methods. Something like... <?php $p = $argv[2]; if (file_exists("/usr/pkgs/$p")) { require_once "/usr/pkg/{$p}.class.php"; $pkg = new $p; } // use the $pkg object. ?> If the above method wasn't available I would need to define the object instantiation for every package available on the system. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 thats the joys of opensource and freedoms of php, where as .asp is very strict compared to php and you can't do stuff like that. Alot of the the fun array building in php that involves strings + $i can't be done as easily in other languages because of strict variable placement rules. Quote Link to comment Share on other sites More sharing options...
corbin Posted June 22, 2007 Share Posted June 22, 2007 To add on to thorpe's post: I once coded a database class that could do MySQL, MSSQL, and maybe some other things (that's my crappy memory for ya). I did something like, class database { private var functions = array('mysql' => array('connect' => 'mysql_connect', 'query' => 'mysql_query', 'fetch_assoc' => 'mysql_fetch_assoc'), 'mssql' => array('blah')); //well pretend I typed out the mssql equivalent as well. function database($type, $host, $user, $pass) { if(!is_array($this->functions[$type])) { echo "incorrect db choice"; return; } $this->function[$type][$connect]($host, $user, $pass); } } Well, that's a pretty weird example, and it might actually be more efficient to if/then the hell out of it, but that's how I remember coding it lol. 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.