This question already has answers here:
What is the difference between a language construct and a "built-in" function in PHP?
(4 answers)
Closed 8 years ago.
PHP has a large number of batteries-included functions, e.g. functions on arrays. Some of these, like each, are present in get_defined_functions()['internal']. Others, like reset and many others, are not present at all. However, they are treated as functions in every other way: they are not documented as "language constructs" or keywords; I can call them using the "variable function" feature; function_exists("reset") returns true; if I try to redefine them (e.g. function reset() { ... }), I get an error about redeclaration, rather than a syntax error; and so on.
Why are these functions not listed by get_defined_functions? Are they not actually functions? If not, what are they? If they are functions, then what actually is it that get_defined_functions is listing? In either case, how do I list the things that don't appear in get_defined_functions?
Quite a short answer: Reset is present in get_defined_functions()['internal'].
Look at [1532] in this fiddle: http://phpfiddle.org/main/code/h5n-ndx
Related
This question already has answers here:
php - Why can't you define a constant named EMPTY
(4 answers)
Closed 4 years ago.
Could you please tell me why this code throws a parse error if the name of the constant is EMPTY, but if I change it to EMPTY2 or SUBSTR it does work.
define('EMPTY', '');
if (empty(EMPTY)) {
echo 'hello world';
}
Because, as stated in this Quora answer, PHP function names are case-insensitive, so EMPTY collides with the built-in function empty().
PHP manual mentions this in a small note right after Example #3 in the subsection about User-defined functions:
Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.
This question already has answers here:
Is it possible to curry method calls in PHP?
(8 answers)
Closed 5 years ago.
Don't necessarily have a problem with how PHP does this or anything, more just a question out of curiosity. I am familiar with functional programming but am by no means an expert. I am writing functions currently and although I have no requirement for them to be functional, that may change in the future. So best to be prepared I say.
My question is, how would you curry a function like in_array?
From what I understand we have two parameters, needle and haystack.
Both seem to be required when the function is called.
We must know the array we are searching at the start of the function, and we must also know what we are searching for.
To me it seems hard to force any partial application or currying solution whereby we might know one or the other at a later point.
I suppose you could have a non-generic function whereby you specify the needle within the function. I did see something about spattering in Google. How would you handle this if asked to rewrite the function and curry it? I know I specified PHP but I suppose any language is really fine as long as the specs are the same.
Well, it's relatively straightforward in PHP - almost the same as in any other language that treats functions as values. For example:
function create_search_by_array($arr) {
return function($needle) use ($arr) {
return in_array($needle, $arr);
};
}
$search_in_1_to_10 = create_search_by_array(range(1, 10));
var_dump($search_in_1_to_10(1)); // true
var_dump($search_in_1_to_10(10)); // true
var_dump($search_in_1_to_10(11)); // false
The only caveat here is use ($arr) construct: without it, the inner function won't be able to see the corresponding variable from an outer scope.
This question already has answers here:
Should an array be declared before using it? [closed]
(7 answers)
Closed 7 years ago.
In most languages, I have to initialize an associative array before I can use it:
data = {}
data["foo"] = "bar"
But in PHP I can just do
data["foo"] = "bar"
Are there any repercussions to doing this? Is this "the right way" to write PHP?
Is the same, but is not a good idea, the next is a copy-paste from php documentation.
If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize variable by a direct assignment.
Basically it's the same, and no you won't find any problem or repercussion.
But if you like you can do this:
$a = array();
You can read more in the PHP page
This question already has answers here:
Parentheses altering semantics of function call result
(2 answers)
Closed 8 years ago.
end(array_keys(array(0))) says PHP Strict standards: Only variables should be passed by reference ( http://3v4l.org/CNLVT )
end((array_keys(array(0)))) on the other hand, just works ( http://3v4l.org/168fi ). Why?
The VLD decompiler shows the same opcodes being ran the only difference is in the ext column but I can't find documentation on what that means.
What's likely happening is array_keys is passing the result back by reference. As such, PHP is throwing you a notice that you shouldn't do that.
Wrapping in parenthesis actually changes the reference and forces PHP to evaluate the statement inside first. As such, it removes the reference. One of those weird things that doesn't look like it makes a difference but actually does.
More on the weirdness here http://phpsadness.com/sad/51
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
Can you please explain to me the differences between two functions:
function &a(){
return something;
}
and
function b(){
return something;
}
Thanks!
The first returns a reference to something, the second a copy of something.
In first case, when the caller modify the returned value, something will be modified as a global variable do.
In the second case, modifying a copy as no effect to the source.
An ampersand before a function name means the function will return a reference to a variable instead of the value.
According to this LINK
Returning by reference is useful when you want to use a function to find to which
variable a reference should be bound. Do not use return-by-reference to increase
performance. The engine will automatically optimize this on its own. Only return
references when you have a valid technical reason to do so.