This question already has answers here:
How do I immediately execute an anonymous function in PHP?
(9 answers)
Closed 8 years ago.
Currently I am doing this:
$f = create_function(null, somecode);
$f();
Is there anyway to not assign it to a variable, but instead directly execute it?
I'm thinking of something like
create_function(null, somecode)();
But this is not working unfortunately. Do not ask me why I want to do this, there is no special reason, I was just wondering this the other day.
You can just execute an anonymous function...
call_user_func(function(){
echo 'I am a function!';
});
PHP VERSION > 5.3
Normal closures, > PHP 5.3:
$func = function($str) { echo $str; };
$func('hello world');
If you directly want to execute code, why would you even want to put it into a function?
Turning the above code into the following is equivalent to instantly executing:
echo 'hello world';
Related
This question already has answers here:
Can I create a PHP function that I can call without parentheses?
(6 answers)
Closed 6 years ago.
all php functions need () in the end. However, exit doesnt need that.
Can I create a function manually, which I can later execute without () ?
Even more, If I have full access to php installation?
p.s. please dont tell me answers "exit is not function" or etc (My question is not if "exit" is function or not). I want to know HOW TO ACHIEVE like that.
No you can't. You have to edit Base of PHP language to accomplish this.
exit , echo , print and etc are not function .
This question already has answers here:
Stop using `global` in PHP
(6 answers)
Closed 7 years ago.
The $GLOBALS array of PHP provides access to all global variables, like
<?php
$foo = 'hello';
function myFunc() {
echo $GLOBALS['foo']; // prints "hello"
}
?>
Now I have to work on some code from other people that directly adds elements to the array to have it available globally (without having an according variable), like so:
<?php
function doSomething() {
// $newData is NOT existing anywhere!
$GLOBALS['newData'] = 'Hello World!';
// now $GLOBALS['newData'] is available anywhere else w/o actual variable
}
?>
Until now I never saw this specific usage of the $GLOBALS array and was wondering if it is considered "safe" or "good"? The PHP manual makes no statement about writing to this array, only about reading from it.
Opinion perhaps, but I would think it's better instead to use define instead of the $GLOBALS array if you're looking to define a constant for use in other parts of your application.
PHP:define - Manual
<?php
function doSomething() {
$_POST['__newData'] = 'Hello World!';
}
doSomething();
echo $_POST['__newData'];
?>
This question already has answers here:
Does function definition order matter?
(7 answers)
Closed 1 year ago.
The following code runs in PHP
<?php
$foo = "Chocolate milkshake";
go($foo);
function go($param) {
echo $param;
}
?>
// Output: chocolate milkshake
See this Demo http://codepad.viper-7.com/ToApZa
This code runs without errors and prints specified output, why?
I thought this "function hoisting" only occurred in JavaScript
It doesn't matter where you declare your functions in PHP in most cases, as you've just proved :)
Take a look at this page for more details. The key point:
Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.
This question already has answers here:
How to mathematically evaluate a string like "2-1" to produce "1"?
(9 answers)
Closed 9 years ago.
I'm kinda new to this but I was wondering if it is possible to add some dynamic calculation into a function as a parameter.
The thing is inside my function I am formatting stuff in a consistent way but each time I want to add a certain different calculation into the parameter.
<?php
function dynamicCalculator($calculation){
$result = $calculation;
//some formatting
return $result;
}
echo dynamicCalculator('(3x5)+1');
This doesn't work of course but if anyone has an idea how this could work I would love to hear it.
What you are looking for is the eval function.
eval('$result = (3*5)+1');
But beware to make sure you're not passing possibly harmful code to that function.
Your looking for RPN (Reverse Polish Notation)
Here is one example
http://pear.php.net/package/Math_RPN/
which would allow you to use
$expression = "(2^3)+sin(30)-(!4)+(3/4)";
$rpn = new Math_Rpn(); echo $rpn->calculate($expression,'deg',false);
And not have to use Eval
Use eval. eval("10+2") should return 12. Be careful though, you can also run PHP code with eval.
Someone else had a similar question on StackOverflow.
You could use this:
function dynamicCalculator($calculation) {
$result = eval($calculation);
return $result;
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Immediately executing anonymous functions
I want to immediately evaluate an anonymous function rather than it appearing as a Closure object in method args. Is this possible?
For example:
$obj = MyClass;
$obj->Foo(function(){return "bar";}); // passes a Closure into Foo()
$obj->Foo(function(){return "bar";}()); // passes the string "bar" into Foo()?
The 3rd line is illegal syntax -- is there any way to do this?
Thanks
You could do it with call_user_func ... though that might be a bit silly when you could just assign it to a variable and subsequently invoke the variable.
call_user_func(function(){ echo "bar"; });
You might think that PHP 5.4 with it's dereferencing capabilities would make this possible. You'd be wrong, however (as of RC6, anyway).