Verification: a143cc29221c9be0

Php call function with args

PHP Built-In Functions

PHP has thousands of built-in functions. Each of those functions is focused to perform a specific task. We have already discussed some of them.

  • echo() - to output a string
  • define() - to define a constant
  • var_dump() - to dump data of a variable

You will learn about the other built-in functions step-by-step in this tutorial.

PHP User-Defined Functions

The function declaration starts with the function keyword. Then, the function name and arguments.

function (, , ....) {

PHP Naming User-Defined Functions

Function naming is almost the same as variable naming except for the $ sign at the beginning. Functions do not have the $ sign.

  • A function name should start with a letter or underscore.
  • A function name cannot start with a number.
  • Letters, numbers, and underscores can be used after the first letter in a function.
  • A function name is case-insensitive (Both boom() and Boom() refers to the same function.)
  • Tip: Always name functions with a name that describes the usage of the function.

PHP Declaring User-Defined Functions

Let's create our first function.

  • First, we declare the function greet() using the function syntax.
  • The block of code inside the curly braces ({}) is the function code. This code is executed when we call the function.
  • Then, we call our function using its name and parentheses: greet();

Note: In PHP, parentheses are used to call a function.

PHP User-Defined Function Example

Run Example ››

PHP Function Arguments

We can get different results from a function using arguments.

Function arguments are the values passed into a function. An argument is a variable inside the function.

  • Arguments are defined inside the parentheses which are there immediately after the function name.
  • A function can have any number of arguments, separated them with commas.
  • An argument name should obey the same rules as a variable since arguments are variables.

Let's see an example.

  • First, we declare a function named myName with one argument, $name.
  • Then, we call the function several times with different argument values.
  • Note the value inside the parentheses of the function call. That value is assigned to $name when the function executes.

PHP Function Arguments Example

'; // line break

Run Example ››

Functions can have multiple arguments.

PHP Function Multiple Arguments Example

		My age is $age 
My country is $country

"; } myDetails('Joe', 22, 'USA'); myDetails('Adam', 25, 'United Kingdom'); myDetails('David', 30, 'France');

Run Example ››

PHP Function Arguments - Passing By Reference

By default, arguments are passed into functions by value. See the following example to understand it.

PHP Passing Arguments By Value Example

'; // outputs "Hyvor Developer"
$rootName = 'Hyvor';
echo 'Outside the function: ' . $rootName; // it is stil 'Hyvor'

Run Example ››

In the above example,

  • $rootName variable is set to "Hyvor".
  • Then, it is passed into changeName() function as the $name argument.
  • Inside the function, "Hyvor" is changed to "Hyvor Developer".
  • But, after executing the function the global variable $rootName still holds the value "Hyvor".

You can change a variable from a function by passing it into the function by reference. Just prepend an & sign to the argument name in the function definition.

PHP Passing Arguments By Reference Example

'; // outputs "Hyvor Developer"
$rootName = 'Hyvor';
echo 'Outside the function: ' . $rootName; // now it's 'Hyvor Developer'

Run Example ››

PHP Function Arguments - Default Values

To specify a default value for an argument, just assign that with the basic assignment operator (=) in the function definition. If the argument is not provided when calling the function, this default value is used.

PHP Functions - Default Values for Arguments Example


printNumber(); // will print 10, the default value

Run Example ››

PHP Function Arguments - Type Declaration

Type declaration (also known as type hinting) can be used to specify a data type for each argument. PHP will throw an error on incorrect data types. The data type should be added before the argument to specify type declaration for it.

PHP Functions - Type Declaration in Arguments

		My age is $age 
My country is $country

"; } myDetails('Joe', 22, 'USA'); myDetails('Adam', 25, 'United Kingdom'); myDetails('David', 30, 'France'); # myDetails('John', 'N/A', 'Australia'); this will cause an error

Run Example ››

Valid Types For Type Declaration

PHP Functions - Returning

There are two uses of return statements.

  • To return a value from a function.
  • To stop the execution of a function when a certain condition is true.

PHP Functions - Returning Values

PHP Functions - Returning Values Example

echo '4 + 3 = ' . sum(4,3) . '
'; echo '8 + 1 = ' . sum(8,1) . '

Run Example ››

PHP Functions - Type Declaration for Returning Values

You can define the data type of the returning value.

The types are as same as the types in argument type declaration. The type should be specified after the function name, adding a semicolon :.

PHP Functions - Returning Values of a Specific Data Type

Run Example ››

The above function should return a float. Otherwise PHP will cast it to float if possible. For example, if the result is an integer, it will be casted into float and then returned. But, if you return a string, PHP will throw an error.

PHP Variable Functions

If a variable which contains a string has parentheses appended to it, PHP checks for a function with the same name, and will execute it. This concept is called variable functions.

PHP Variable Functions Example

Run Example ››