Category: 05. Functions
-
Global Variables
In PHP, any variable that can be accessed from anywhere in a PHP script is called as a global variable. If the variable is declared outside all the functions or classes in the script, it becomes a global variable. While global variables can be accessed directly outside a function, they aren’t automatically available inside a function.…
-
Local Variables
Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types − Local Variables A variable declared in a function is considered local; that is, it can be referenced solely in that function. Any assignment outside of…
-
Arrow Functions
Arrow functions were introduced in PHP 7.4 version. Arrow functions provide a simpler and more concise syntax for writing anonymous functions. With PHP 7.4, a keyword “fn” has been introduced for defining arrow functions, instead of the conventional use of the “function” keyword. Example The following example demonstrates how you can use the arrow function…
-
Anonymous Functions
What are Anonymous Functions? PHP allows defining anonymous functions. Normally, when we define a function in PHP, we usually provide it a name which is used to call the function whenever required. In contrast, an anonymous function is a function that doesn’t have any name specified at the time of definition. Such a function is also called closure or lambda…
-
Strict Typing
PHP is widely regarded as a weakly typed language. In PHP, you need not declare the type of a variable before assigning it any value. The PHP parser tries to cast the variables into compatible type as far as possible. For example, if one of the values passed is a string representation of a number,…
-
Variable Scope
In PHP, the scope of a variable is the context within which it is defined and accessible to the extent in which it is accessible. Generally, a simple sequential PHP script that doesn’t have any loop or a function etc., has a single scope. Any variable declared inside the “<?php” and “?>” tag is available…
-
Type Hints
PHP supports using “type hints” at the time of declaring variables in the function definition and properties or instance variables in a class. PHP is widely regarded as a weakly typed language. In PHP, you need not declare the type of a variable before assigning it any value. The PHP parser tries to cast the…
-
Recursive Functions
A recursive function is such a function that calls itself until a certain condition is satisfied. In PHP, it is possible to defines a recursive function. Calculation of Factorial using Recursion The most popular example of recursion is calculation of factorial. Mathematically factorial is defined as − It can be seen that we use factorial…
-
Passing Functions
To a function in PHP, in addition to scalar types, arrays, and objects, you can also pass a function as one of its arguments. If a function is defined to accept another function as an argument, the passed function will be invoked inside it. PHP’s standard library has certain built-in functions of this type, where…
-
Returning Values
A PHP function can have an optional return statement as the last statement in its function body. Most of the built-in functions in PHP return a certain value. For example the strlen() function returns the length of a string. Similarly, a user defined function can also return a certain value. A function is an independent,…