PHP, like most other programming languages has a small set of inbuilt operators for operations with numbers, strings and some other types of data (we’ll discuss them later). The number of operators in PHP is limited and can’t be extended. On the other hand, developers always face situations when having other operators would be useful. Let’s say we want to raise two to the power of three. We could do it like this:
<?php $number = 2 * 2 * 2;
However it doesn’t look very elegant, and imagine what it would look like in case of a big power. It would be perfect to have a corresponding operator (and PHP has it), but what if we didn’t have it? That’s when a developer would use a function. Functions can be inbuilt as well, but the beauty of it is that we can create our own. Before we move on to create our own functions, let’s first try out some pre-made ones. PHP has a function that raises one number to the power of another. Let’s call it:
<?php $result = pow(2, 3); // 8
We created a variable named
$result and instructed the interpreter to store in it the result of the
pow function. Notation
pow(2, 3) means that we call the function named
pow with arguments (or parameters) 2 and 3. Upon execution the function returned a result.
Arguments are data a function receives when it’s called. Functions calculate and return result based on this data.
pow function raises the first number to the power of the second number. In the example above, we raised 2 in the power of 3, and we got 8 stored in the
Modern PHP has a special power operator, so we can find the exponent in this way:
<?php $result = 2 ** 3; // 8
A function call is always marked by brackets
() after the function name. There may be any number of arguments in the brackets, and sometimes functions are called with no arguments at all. The number of arguments depends on the operation. Let’s look at the function
abs that finds the module of a number:
<?php $result = abs(-5); // 5 $result = abs(8); // 8
As you can see, this function receives one argument (it’s often called “parameter”) and returns its module. And here is an example of a function call with no arguments:
<?php $result = rand();
rand generates and returns a random number.
Mathematical operations - are a tiny part of all possible variety of functions. The power of the function mechanism is that we can create our own functions - and we will do so in the next module. Any action that you could possibly imagine is represented by functions in programming.
There was a man in the Seven Kindoms who had access to computers and could program - Sam Tarly. He was doing cartography and to help himself he wrote a function named
calculateDistance, that calculated distance (in leages) between cities. The function receives two string parameters - names of two cities and returns a number - the distance between them.
In the example below we use this function to calculate distance between Lannisport and Bayasabhad:
<?php use function HexletBasics\Functions\calculateDistance; $distance = calculateDistance('Lannisport', 'Bayasabhad'); print_r($distance);
The first line is a special code that makes the
calculateDistance function available in our program. You can use that function whithout knowing what’s inside it. It’s a common thing in programming: you know what a function does and how to use it, but you don’t know how exactly it works on the inside.
calculateDistance function, find and output the distance between the cities of Qarth and Vaes Dothrak. Don’t copy the example, create a variable with a different name and write the code from zero on your own.
Function — an operation that receives data and returns result; functions are called like this:
Argument — information passed to a function when it’s called. For example, in this case:
foo(42)we pass the argument
42to the function
Please sign in with your GitHub account, this is necessary to track the progress of the lessons. If you do not have an account yet, now is the time to create an account on GitHub.