Interesting features of the PHP language

    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 separate documentation in PHP documentation. The author is fully 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 last article about using expressions in PHP. This is not about complicating the code, rather the opposite.


    BREAK Team


    For some reason, many people think that break can only be used in switch constructs. It's a delusion. Besides switch, it is used in loops. In addition, the team has an optional parameter - the number of levels to exit. For example:

    while ($ exp1) {
        while ($ exp2) {
            if ($ exp3) {
                // complete exit from the loop
                break 2;
            }
            else {
                // exit only while ($ exp2)
                break;
            }		
        }
    }


    CONTINUE Team


    Like break, it has 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 construct, the continue command has exactly the same effect as break. Therefore, if you have a switch statement inside the loop, and you try to execute 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 you need to do continue 2 ;

    Include command


    Do you know that the include script works just like a function call, namely it can return the values ​​returned by the return command in the process of running 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).

    For loop


    The fact that in for in the first and third parameter you can list expressions, I already mentioned in the article using expressions in PHP . There is an example.

    The $ {} operator.


    The $ {} operator is considered as a variable whose name is defined as a string obtained from an expression in curly brackets. This can greatly simplify life when working with class properties and methods in OOP. For example:

    $ myClass = new stdClass ();
    $ myClass-> myParam = new stdClass ();
    function calcThis ($ method) {
        return "my". $ method;
    }
    // You cannot do this, since you will consider it 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 when accessing a method or class property, it is not required.

    DECLARE Team


    It can rightly be said that this is the most rarely used basic construct in PHP. It is needed to set directives to the interpreter 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. A tick is the execution of one basic php action. In addition, using the special register_tick_function () function, set the function that will be executed when an event occurs, otherwise the construction is meaningless.

    In practice, declare can be used to profile scripts. Those. thanks to ticks, you can measure the time spent on each of your operations (or their group) in the script.

    How to do it, see here -http://www.php.net/manual/ru/control-structures.declare.php


    Also popular now: