I want to create my own function loop()
:
loop($x,$y){
//do something
}
it should work like for, foreach operators:
$y=count($array);
for($x=0; $x<$y; $x++){
//do something
}
How to create functions like this with { }
in PHP? (NOT a regular function)
what do you mean by {} syntax?
I think he just means, how do you create a function in php.
@MasterPeter {} like in for(){ } function
The problem with anonymous functions is that you have to write them every time you want to use them. If you write a function once, give it a name. The only real use I see for anonymous functions is to add more parameters to a function with function calls like array_map or array_walk.
he just worded it wrong, he means a new language construct. Why do you want to do that Colargol?
Probably because he learned to program in Lisp.
You can’t.
Constructs like for or foreach are part of php syntax.
If you want to create regular function just use function keyword:
There will be lambda functions – that’s what you want to do – in PHP 6.
Right now you have to stick with “normal” functions.
At a more advanced level, you could create a class and iterate that, in which you can pretty much change the execution flow during execution (while iterating).
Here’s the docs: http://php.net/iterator
Another thing which you can if you are really serious about your function/s you can write it a php extension which will you some flexibility…http://devzone.zend.com/node/view/id/1021 I must warn you though..it’s not as easy….