In this article I want to talk about interesting, but not very common basic features of the PHP language. Everything that is written further in one form or another is present in the documentation for PHP in a fragmented form. The author is well aware that most professional developers know all this very well, but beginners can learn something new.
The content of the article has little to do with my previous article about using expressions in PHP. We are not talking about the complexity of the code, rather the opposite.
Team BREAK.
Many for some reason believe that break can be used exclusively in switch constructions. It's a delusion. In addition to switch, it is used in cycles. In addition, the team has an optional parameter - the number of levels from which to exit. For example:
')
while ($ exp1) {
while ($ exp2) {
if ($ exp3) {
// complete loop exit
break 2;
}
else {
// exit only while ($ exp2)
break;
}
}
}
Command CONTINUE
Like break, there is a parameter - the number of return levels to the loop.
while ($ exp1) {
while ($ exp2) {
if ($ exp3) {
// return to while ($ exp1)
continue 2;
}
else {
// return to while ($ exp2)
continue;
}
}
}
By the way, in the switch construction, the continue command has the exact same effect as break. Therefore, if you have a switch statement inside the loop, and you try to continue inside it, then you will simply break from the switch, and not return to the beginning of the loop. In this case, to get the expected one, you must
continue 2 ;
Include command
Did you know that the include script works in the same way as a function call, namely it can return the values ​​returned by the return command during the execution of this script? That is, you can do this:
// i.php file
Return “hello, world!”
// file index.php
// prints “hello, world!”
echo include (“i.php”);
Of course, this applies to other similar commands (include_once, require, require_once).
Cycle for
The fact that you can list expressions in for in the first and third parameters is already mentioned in the article on
using expressions in PHP . There is an example.
Operator $ {}.
The $ {} operator is treated as a variable whose name is defined as a string, obtained from an expression in curly braces. This can greatly facilitate life when working with the properties and methods of classes in OOP. For example:
$ myClass = new stdClass ();
$ myClass-> myParam = new stdClass ();
function calcThis ($ method) {
return "my". $ method;
}
// You cannot do this, as you will see this as a class method:
$ myClass-> calcThis ('Param') -> c = 3;
// But you can do it like this:
$ myClass -> {calcThis ('Param')} -> c = 3;
In this example, there is no $ sign, because it is not required when referring to a method or property of a class.
DECLARE command
We can rightly say that this is the most rarely used basic construct in PHP. It is needed to set interpreter directives when executing code. At the moment there is only one directive - ticks.
Declare (ticks = n) - tells the interpreter (creates an event) that it needs to process something every n ticks. Tick ​​is the execution of a single basic php action. In addition, using the special function register_tick_function () to set a function that will be executed when an event occurs, otherwise the design is meaningless.
In practice, declare can be used for profiling scripts. Those. thanks to ticks, you can measure the time spent on each of your operations (or their groups) in the script.
How this is done, see here -
http://www.php.net/manual/ru/control-structures.declare.php