Functions and language constructs
In the previous articles you already saw the language construct echo and the function date(). Let's talk a little bit more about these constructs and functions.
What's a function
A function consists of some code that can be reused. Functions have names, so that we know how to call them. They also may accept input in the form of parameters (any number of parameters). A function will perform differently depending on what you pass to it as parameter.
For example date() is function. When you call the function, passing 'Y', it will return the current year. If you pass 'Y-m-d', it will return the date with the year, month and day, separated by a dash.
echo date('Y'); // prints 2007
echo date('Y-m-d'); // prints 2007-09-18
Functions usually return a value. Sometimes they may do what they do and don't return anything, but most of the time they return something. A function can only return one value, not more. In the example above the date() function returned some text, a string value. Then we used echo to output to the browser whatever the function has returned.
Optional and multiple function parameters
If a function should receive multiple parameters, those should be separated by a comma.
Functions may have required parameters (that you have to pass) and optional parameters. The function takes care of using some default value for optional parameters in case you don't pass (supply) them.
So far we've called the function date() with only one parameter. But if you look up the documentation, you'll see that it accepts another parameter, an optional timestamp. Actually the primary job of the function date() is to nicely format a given date. The first parameter is the format. The second is the actual date. If the second parameter is skipped, the function assumes you want to work with the current date. But what if you don't?
In case you're not familiar a UNIX timestamp is the number of seconds since midnight, December 31, 1969, also known as Unix epoch. Timestamp 0 means 0 seconds since the Unix epoch. So if we pass 0 as a second parameter, the function will return Dec 31, 1969, formatted as specified in the first parameter.
echo date('Y-m-d'); // prints 2007-09-18
echo date('Y-m-d', 0); // prints 1969-12-31
echo date('Y-m-d', 123456789); // some random time, 1973-11-29
Language constructs
As you know already, echo is used to print some output. It can also be used with the syntax of a function:
echo('Hello!');
Although it can be used as a function, echo is actually a language construct. Most of the times, it makes no difference if a language construct is used with the syntax of a function, but sometimes it does. So it's a good idea to adopt the habit of using the constructs without parentheses.
Further in this tutorial, it will be noted when we come across other language constructs.
Built-in and custom functions
The date() function we looked at comes with PHP, so let's call it a built-in function. There are tons of functions in PHP, that's why PHP is so easy to learn, for a lot of things you'll try to do, most likely there'll be an existing function already.
You can also create your own functions, here's an example:
// defining a function
function sayHi() {
echo 'Hello, Holla, Salut!';
}
sayHiis the name of the function- the empty parentheses mean that it doesn't expect parameters
- the part between the curly brackets is the body of the function that does the actual work
To call the function, you call it by name and empty brackets:
sayHi(); // will print "Hello, Holla, Salut!" sayHi(); // same thing again
This function didn't return a value, it simply echo-ed what it needed to. Let's modify it to return a value instead of printing:
// defining a function
function sayHi() {
return 'Hello, Holla, Salut!';
}
Now if we call it by simply typing sayHi();, nothing visible won't happen. To make it work as before, we need to print the value when we call the function:
sayHi(); // doesn't print anything echo sayHi(); // prints "Hello, Holla, Salut!"