Читать книгу PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO) - Sergey D Skudaev - Страница 6

Function

Оглавление

A function is a piece of code that performs a specific manipulation with variables and then returns the result from that manipulation. Functions are useful because they can be used multiple times over. There are PHP functions that are built-in, but a function can also be created by the user. Let’s create one that converts Fahrenheit to Celsius. The PHP function declaration begins with the word function to be followed by the function name, which should be one word. Several parameters can be given to a function, but a function can only return one parameter in the return statement. Multiple parameters are enclosed together in parentheses and separated by comas. The entire piece of code inside a function is enclosed in curly brackets.

In our example, we pass one parameter, a temperature in Fahrenheit, and return a temperature in Celsius.


function convertFtoC ($F) {


$C = round (($F – 32) * 5/9);

return $C;

}

Now to convert Fahrenheit to Celsius you must call your function and pass the temperature in Fahrenheit.


$T= convertFtoC (80);

echo $T.”<br>”;


The Output: will be 27

The round is a built-in PHP function. It will round 26.6666666667 to 27.

PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO)

Подняться наверх