PHP: Comments
Besides the code itself, source files often contain comments. These are lines that the interpreter does not process. Programmers write them to explain how the code works, mark bugs, or remind themselves of what still needs to be done.
<?php
// Remove the line below after implementing the registration task
print_r(10);Single-line comments in PHP start with the // or # characters, which can be followed by any text. Everything written after them until the end of the line is ignored by the interpreter.
// comment ──→ [ skipped by interpreter ]
print_r('hello'); ──→ [ executed → hello ]
// another one ──→ [ skipped by interpreter ]A comment can take up an entire line:
<?php
// For Winterfell!
// For Lanisters!Or it can be placed at the end of a line with code:
<?php
print_r('I am the King'); // For Lannisters!If you need to leave a long explanation, several lines with // are used:
<?php
// The night is dark and
// full of terrors.
print_r('I am the King');For such cases, PHP also has a special kind — multiline comments. They start with /* and end with */, and each line between them is conventionally started with the * character:
<?php
/*
* The night is dark and
* full of terrors.
*/
print_r('I am the King');The interpreter ignores comments. Thanks to them, developers can understand someone else's code faster and not forget important details in their own.
Service Comments
While working, you will come across this kind of code in our editor:
// BEGIN
// ENDBEGIN and END here are ordinary comments that do not affect the program's behavior in any way. They show where to write the task's code.
// BEGIN
<your solution here>
// ENDWhen you see BEGIN and END, write your code between them and leave the rest unchanged.
Instructions
You are writing a program and realize that one part needs to be finished later. To avoid forgetting, programmers leave themselves notes right in the code — TODO comments.
Add the following comment to the file:
// TODO: add a greeting functionWhen you come back to this place later, the comment will remind you that there is still unfinished work here.
Tips
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.
Your exercise will be checked with these tests:
<?php
namespace HexletBasics\Basics\Comments;
use HexletBasics\Exercise\TestCase;
class SolutionTest extends TestCase
{
public function test()
{
$this->assertOutput('');
$content = file_get_contents('Solution.php');
$this->assertStringContainsString('// TODO: добавить функцию приветствия', $content);
}
}Teacher's solution will be available in:
20:00
