The real power of PHP comes from its functions. Show
PHP has more than 1000 built-in functions, and in addition you can create your own custom functions. PHP Built-in FunctionsPHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task. Please check out our PHP reference for a complete overview of the PHP built-in functions. PHP User Defined FunctionsBesides the built-in PHP functions, it is possible to create your own functions.
Create a User Defined Function in PHPA user-defined function declaration starts with the word Syntax function functionName() { Note: A function name must start with a letter or an underscore. Function names are NOT case-sensitive. Tip: Give the function a name that reflects what the function does! In the example below, we create a function named "writeMsg()". The opening curly brace ( { ) indicates the beginning of the function code, and the closing curly brace ( } ) indicates the end of the function. The function outputs "Hello world!". To call the function, just write its name followed by brackets (): Example<?php writeMsg(); // call the function Try it Yourself » PHP Function ArgumentsInformation can be passed to functions through arguments. An argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name: Example<?php familyName("Jani"); Try it Yourself » The following example has a function with two arguments ($fname and $year): Example<?php familyName("Hege", "1975"); Try it Yourself » PHP is a Loosely Typed LanguageIn the example above, notice that we did not have to tell PHP which data type the variable is. PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error. In PHP 7, type declarations were added. This gives us an option to specify the expected data type when declaring a function, and by adding the In the following example we try to send both a number
and a string to the function without using Example<?php Try it Yourself » To specify In the following example we try to send both a number and a string to the function, but here we have added the Example<?php declare(strict_types=1); // strict requirement function addNumbers(int $a, int $b) { Try it Yourself » The PHP Default Argument ValueThe following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument: Example<?php declare(strict_types=1); // strict requirement setHeight(350); Try it Yourself » PHP Functions - Returning valuesTo let a function return a value, use the Example<?php
declare(strict_types=1); // strict requirement echo "5 + 10 = " . sum(5, 10) . "<br>"; Try it Yourself » PHP Return Type DeclarationsPHP 7 also supports Type Declarations
for the To declare a type for the function return, add a colon ( In the following example we specify the return type for the function: Example<?php declare(strict_types=1); // strict
requirement Try it Yourself » You can specify a different return type, than the argument types, but make sure the return is the correct type: Example<?php declare(strict_types=1); // strict requirement Try it Yourself » Passing Arguments by ReferenceIn PHP, arguments are usually passed by value, which means that a copy of the value is used in the function and the variable that was passed into the function cannot be changed. When a
function argument is passed by reference, changes to the argument also change the variable that was passed in. To turn a function argument into a reference, the ExampleUse a pass-by-reference argument to update a variable: <?php $num = 2; Try it Yourself » PHP ExercisesHow many types of functions are there in PHP?PHP provides us with two major types of functions: Built-in functions : PHP provides us with huge collection of built-in library functions. These functions are already coded and stored in form of functions.
What is function explain types of function in PHP scripting language?A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value. You already have seen many functions like fopen() and fread() etc. They are built-in functions but PHP gives you option to create your own functions as well.
What is the function name in PHP?Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$ . See also the Userland Naming Guide.
WHAT IS function and method in PHP?Function is a generic term to be used in procedural programming approach where as Method is a term to be used in Object oriented programming approach to define a class property.
|