The parameters
We have just seen an example of a procedure which did not contain any parameter. Parameters are variables that are placed between parentheses. These parameters used data on which the function. Generally, the parameters imply that it will return something (we will be very likely to have functions and procedures). We see later that the move by reference parameters (do not worry if you do not understand yet what I say, you will understand the bottom of this page) can usually escape the return of a value.
Example of a function calculating the product of two numbers and returning the result:
Code:
<? php
function product ($ number1, $ number2)
{
return number1 $ * $ number2;
}
? >
Here it will return the product of two numbers. Note that using the keyword return to return a value. You can also use the keyword return to stop the execution of the function (by returning a value). Everything after return will not be executed.
If you want to display the product of 10 and 2 for example, you can do this:
Code:
<?
php echo product (10, 2);
? >
We can assign values to default settings. Why assign default values? it's simple, assign a value to a default setting, you can do without the passage of setting when you call the function (attention, this is not general). Here's an example:
Code:
<? php
function product (number1 = $ 10, $ number2 = 2)
(
return $ number1 * $ number2;
)
>
Here, we assign as defaults numbers 10 and 2. If we now call our function like this:
Code:
<? php
Product echo ();
>
We will get the same result as above, because both settings are not mandatory here. As against attention, if you contact a setting, it will be a priority, which means that if you call the function like this:
Code:
]
<? php
echo product (5, 2);
>
Here, we multiply 5 by 2 and 10 by 2. Give a default settings should be when you want not to have to add parameters when you tell PHP "performs this function me." Attention with the following example:
Code:
<? php
echo product (5);
>
There is only one parameter! then where will it replace? Well this is the first parameter. In fact, when you leave all the settings, PHP will replace the parameters in order.
Bookmarks