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.
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:
What is the meaning of three dots (...) in PHP?
(9 answers)
Closed 5 years ago.
I'm learning PHP http://php.net/manual/en/migration70.new-features.php and in the following example, I don't understand ... prepended with the $ints parameter in the function definition.
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
Can anybody please tell me what those dots are for?
Thanks.
that means that when you call that function, you can pass X integers and the function will process them, doesn't matter how many are they. If you call sumOfInts(3,4,6,2,9) or sumOfInts(3,2,9), the function works, no matter how many arguments you pass
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:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
I want to know about meaning of $$val; what is the actual meaning is?i tried to find meaning of this in google but not understand properly. Please help me in this situations.
For example: suppose i have one variable which has $$value;
meaning of $$value?
You didn't put the language, but I'll assume you mean PHP
That's a variable variable.
That means you ware asking for the value of the variable whose name.is the first variable.
Here's an example, since that's quite confusing:
$foo = "Hi";
$bar = "world";
$world = "Hello!";
echo $$bar; // "Hello!"
php fiddle: http://ideone.com/Ve4YOO
Reference: https://secure.php.net/manual/en/language.variables.variable.php
This question already has answers here:
What does it mean to start a PHP function with an ampersand?
(3 answers)
Closed 7 years ago.
I'm using a CMS package written in PHP. In one of it's core files I saw following line that is for defining a function in a class body.
public static function &getLib($sClass, $aParams = array()) {
// Code
}
I didn't understand why the function name 'getLib' has been prepended with the ampersand(&) sign? I've never seen such thing before.
Can someone please explain me in detail why such thing has been done and what's the benefit it has over simply using the function name?
It means the function should return a reference to a variable rather than just the value itself.