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.
Everything that is described in curly braces after the 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.
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.
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
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.
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.
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.
languages.lessons.show.chat.guest
Your exercise will be checked with these tests:
1<?php
2
3namespace HexletBasics\DefineFunctions\Define;
4
5use PHPUnit\Framework\TestCase;
6
7class SolutionTest extends TestCase
8{
9 public function test()
10 {
11 require 'index.php';
12
13 $expected = 'Winter is coming';
14 $this->expectOutputString($expected);
15 printMotto();
16 }
17}
18
Teacher's solution will be available in: