It was evening, echo "there was nothing"

    Good afternoon, Habr. In the article I will tell how, using the php syntax, you can write a function to output a string variable passed to it, which does not use the characters “a-zA-Z0-9 <>?”

    In general. Links to articles about using undefined variable have already slipped , so I’m most likely not discovering anything new.

    In php, as in any language with dynamic typing, there are moments hidden from the final developer, we will play them.

    Let's start from the end. We can always turn to any callable - there would be a string to call. Functions from the SPL are no exception, although this is extremely rare.

    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4

    But we cannot turn to echo or print in this way, for they are language constructs, not functions. One could use printf for example, but I did not like this approach. Therefore, we will use the assert function for output.
    assert("print someShit");

    By the way, replacing print with echo in the expression above does not work, they are both not functions, but print at least tries to seem like that.

    The output line will be given to us when called. It remains a little. Get the strings “assert” and “print” using the character set available to us.

    For this, we use a number of features of the language.
    1) In php, we can access the undefined variable via _ (underscore). It is easy to achieve a narrowing of the type and get 0

    2) When casting an array to a string, it will be converted to a string with an Array value
    //инициализируем пустой массив
    // В данном случае _ приводится к строке "_"

    array(2) {
    string(6) "Array_"

    3) As in most other languages, a string is actually an array of characters. Symbols can be incremented.

    Combining all of the above, we get such a code.

    /usr/bin/php /var/www/garbage/brainfuck.php
    было нечего
    Process finished with exit code 0

    Practical benefit 0 in principle ... but there is something =)

    PS Recommended reading

    Also popular now: