Free PHP course. Sign Up for tracking progress →

PHP: Creating (defining) functions

Defining your own functions makes it much easier to write and maintain programs. Functions allow you to combine several complex operations into one. For example, sending an email on a website is a complex process that involves interacting with external systems over the Internet. Since you can define functions, you can hide all the complexity behind one simple function:

<?php

// A hypothetical example
// The place where the function is taken from
namespace Some\Email\Package\send;

$email = 'support@hexlet.io';
$title = 'Help';
$body = 'I wrote a success story, how can I get a discount?';

// One little call and loads of logic inside
send($email, $title, $body);

A call like this contains a lot of logic. It connects to the mail server, generates a valid request based on the message header and body, then sends it, and then closes the connection.

Let's create our first function. Its job is to print out a greeting:

Hello, Hexlet!
<?php

// Defining the functions
// The definition doesn't call or execute a function
// We're just declaring this function exists now
function showGreeting()
{
    $text = 'Hello, Hexlet!';
    print_r($text);

}

// Function call
showGreeting(); // => 'Hello, Hexlet!'

Unlike normal data, functions perform actions, so their names almost always have to be verbs: "build something", "draw something", "open something", etc.

Anything described below the indented function name is called the body of the function. It can contain any code. Consider it a small independent program, a set of whatever statements are needed. The body is executed when the function is called. And each function call executes the body independently of other calls.

The body of the function can be empty:

<?php

// Minimum function definition. This doesn't do anything
function noop() {};

noop(); // the call is there, but it's pointless
// This function can also be useful,
// but this refers to advanced topics

The notion of "creating a function" has many synonyms, such as "implement" or "define". All of them are encountered in everyday practice.

Instructions

Implement a function, printMotto(), which prints the phrase Winter is coming.

<?php

printMotto(); // => "Winter is coming"

In tasks where you have to implement a function, you don't need to call it. Automated tests will call this function to check if it works. The example call above is shown just to give you an idea of how your function will be used.

The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.

Tips