Tutorials for PHP Function with Parameters or Arguments

What is Function with Parameters or Arguments?

The information or variables included within the function’s parentheses are referred to as parameters. These are used to keep track of values that can be used during runtime. The comma(,) operator allows the user to input as many arguments as he wants. These options are used to accept inputs during runtime. When transferring numbers, such as during a function call, arguments are used. A parameter is a container for parameters that are given to a function. A value supplied to a function is referred to as an argument. In layman’s terms, both parameter and statement have the same meaning. It’s crucial to keep in mind that each parameter must be accompanied with an argument.

PHP functions,Parameters and arguments

Syntex:-

function function_name($first_parameter, $second_parameter) {
    executable code;
}

Example:-

Setting Default Values for Function parameter:-

In PHP, we may define default arguments for function parameters. If no argument is supplied for a parameter with a default value, PHP will call the function with the parameter’s default specified value.

The default value for the parameter $num in the previous example is 12; if no value is supplied for this parameter in a function call, the default value is used. It’s also required because the option $str has no default value.

Functions with return values:-

Functions can also return values to the portion of the program where they were called. The value is returned to the program section from where it was named using the return keyword. It is possible to return any type of value, including arrays and objects. The return statement also marks the conclusion of the function, stops it from running, and returns the value.

Parameter passing to Functions:-

In PHP, there are two ways to pass an argument to a function.

Pass by Value:– Moving by value changes the value of an argument within a function, while the original value outside the function stays unaffected. This means that a statement that is a duplicate of the original value is transmitted.

Pass by Reference:– As statements are passed via proxy, the original value is transmitted. As a result, the starting value has changed. Using the ampersand symbol in pass by reference, we just transmit the value’s address, where it is kept (&).

Example:-

Tagged : / / / / / / / / / / / / / /