Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
PHP
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Functions === PHP defines a large array of functions in the core language and many are also available in various extensions; these functions are well documented online [https://www.php.net/docs.php PHP documentation].<ref name="php.net-2014a" /> However, the built-in library has a wide variety of naming conventions and associated inconsistencies, as described under [[#History|history]] above. Custom functions may be defined by the developer: <syntaxhighlight lang="php"> function myAge(int $birthYear): string { // calculate the age by subtracting the birth year from the current year. $yearsOld = date('Y') - $birthYear; // return the age in a descriptive string. return $yearsOld . ($yearsOld == 1 ? ' year' : ' years'); } echo 'I am currently ' . myAge(1995) . ' old.'; </syntaxhighlight> As of {{CURRENTYEAR}}, the output of the above sample program is "I am currently {{#expr: {{CURRENTYEAR}} - 1995}} years old." In lieu of [[function pointer]]s, functions in PHP can be referenced by a string containing their name. In this manner, normal PHP functions can be used, for example, as [[Callback function|callbacks]] or within [[function table]]s.<ref name="php.net-2014c" /> User-defined functions may be created at any time without being [[Function prototype|prototyped]].<ref name="php.net-2014a">{{cite web | url = https://www.php.net/manual/en/functions.user-defined.php | title = User-defined functions (PHP manual) | date = 2014-07-04 | access-date = 2014-07-07 | website = php.net }}</ref><ref name="php.net-2014c">{{cite web | url = https://www.php.net/manual/en/functions.variable-functions.php | title = Variable functions (PHP manual) | date = 2014-07-04 | access-date = 2014-07-07 | website = php.net }}</ref> Functions may be defined inside code blocks, permitting a [[dynamic dispatch|run-time decision]] as to whether or not a function should be defined. There is a <code>function_exists</code> function that determines whether a function with a given name has already been defined. Function calls must use parentheses, with the exception of zero-argument class [[constructor (computer science)|constructor]] functions called with the PHP operator <code>new</code>, in which case parentheses are optional.{{Citation needed|date=November 2023}} Since PHP 4.0.1 <code>create_function()</code>, a thin wrapper around <code>eval()</code>, allowed normal PHP functions to be created during program execution; it was deprecated in PHP 7.2 and removed in PHP 8.0<ref>{{cite web | url = https://www.php.net/manual/en/function.create-function.php | title = create_function() (PHP manual) | date = 2022-04-06 | access-date = 2022-05-04 | website = php.net }}</ref> in favor of syntax for [[anonymous function]]s or "[[Closure (computer programming)|closures]]"<ref>{{cite web | url = https://www.php.net/manual/en/functions.anonymous.php | title = Anonymous functions (PHP manual) | date = 2014-07-04 | access-date = 2014-07-07 | website = php.net }}</ref> that can capture variables from the surrounding scope, which was added in PHP 5.3. Shorthand arrow syntax was added in PHP 7.4:<ref>{{cite web | url = https://www.php.net/manual/en/functions.arrow.php | title = Arrow Functions (PHP manual) | access-date = 2021-01-25 | website = php.net }}</ref> <syntaxhighlight lang="php"> function getAdder($x) { return fn($y) => $x + $y; } $adder = getAdder(8); echo $adder(2); // prints "10" </syntaxhighlight> In the example above, <code>getAdder()</code> function creates a closure using passed argument {{code|lang=php|code=$x}}, which takes an additional argument {{code|lang=php|code=$y}}, and returns the created closure to the caller. Such a function is a first-class object, meaning that it can be stored in a variable, passed as a [[parameter (computer programming)|parameter]] to other functions, etc.<ref>{{cite web | url = http://wiki.php.net/rfc/closures | title = Request for Comments: Lambda functions and closures | date = 2008-07-01 | access-date = 2014-07-07 | author1 = Christian Seiler | author2 = Dmitry Stogov | website = php.net }}</ref> Unusually for a dynamically typed language, PHP supports type declarations on function parameters, which are enforced at runtime. This has been supported for classes and interfaces since PHP 5.0, for arrays since PHP 5.1, for "callables" since PHP 5.4, and scalar (integer, float, string and boolean) types since PHP 7.0.<ref name="php.net-2015b" /> PHP 7.0 also has type declarations for function return types, expressed by placing the type name after the list of parameters, preceded by a colon.<ref name="php.net-2015a" /> For example, the <code>getAdder</code> function from the earlier example could be annotated with types like so in PHP 7: <syntaxhighlight lang="php"> function getAdder(int $x): Closure { return fn(int $y): int => $x + $y; } $adder = getAdder(8); echo $adder(2); // prints "10" echo $adder(null); // throws an exception because an incorrect type was passed $adder = getAdder([]); // would also throw an exception </syntaxhighlight> By default, scalar type declarations follow weak typing principles. So, for example, if a parameter's type is <code>int</code>, PHP would allow not only integers, but also convertible numeric strings, floats or Booleans to be passed to that function, and would convert them.<ref name="php.net-2015b" /> However, PHP 7 has a "strict typing" mode which, when used, disallows such conversions for function calls and returns within a file.<ref name="php.net-2015b" />
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)