Jump to content

Executing a variable?


christofurr

Recommended Posts

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!!!

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

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...  :-\

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.