PHP Arguments

PHP Function arguments

Information may be passed to functions via the argument list.
PHP arguments are specified inside a parenthesis after function_name.
The PHP arguments are evaluated from left to right.
You can give multiple number of arguments as you want by separating them with comma(,).
PHP supports passing arguments by value, passing by reference, and default argument values.


Passing PHP arguments by value


In above example function sum takes two arguments $a, $b.
When function called the $a is assigned by 10 and $b will assigned by 20.
Then it will perform summation and output addition.

When function sum call second time then it will perform addition of $a = 2 and $b = 4.


Passing arguments by reference

Lets see an example,

As you can see in above example, a value of $a is not change outside the function.
Hence to allow a function to modify its arguments, they must be passed by reference.

The ampersand(&) is use before argument name in function definition to pass arguments by reference.

In this example, a variable $str1 is passed to the function con().
The value of str1 is concatenate with the value of str2.

Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.