Back to Home

#ifdef in PHP

php ifdef

#ifdef in PHP

    In my project, I needed to build different versions of the source. Moreover, so that different pieces of code would fit in different assemblies. if-else didn’t suit me here, because it was necessary for the code to either enter exactly or not enter the assembly.

    Decision


    So I decided to use the # ifdef / # endif directives. To implement them, I created a small program in C # that processes these directives. In the program, you need to specify the source file or folder, and the folder where you want to put the finished result. Only php files are processed for directives, the rest are simply copied.
    PHP Ifdef Collector PrintScreen


    Program work


    The program has a global array defines, which is created from the third field ("Defines (comma separated)"). This array can be supplemented with the “#define TEXT” directive in php code.
    The program can process the following directives: "#ifdef TEXT", "#ifndef TEXT". The first includes code if the directive is defined, the second includes if not defined. These directives must have a closing "#endif".
    The program is also able to handle the "#else" and nesting of these directives.

    Conclusion


    Since the php symbol “#" defines a comment, the code can be executed without cutting these directives.
    However, if you use "#ifdef - #else - #endif" with code that performs different actions with the same data, you need to comment out one of these actions to execute it without cutting directives in approximately the following way: You can download the program here: phpifdef. googlecode.com/files/Php%20Ifdef%20Collector.exe Program sources here: code.google.com/p/phpifdef

    #ifdef COPY

    $chat_page = new View('copy');
    $chat_page->SetVars(array('copyright' => $this->Copyright()));
    $this->html = $chat_page->Get();
    $this->Display();

    /*#else

    $this->html = View('no_copy')::Call();
    $this->Display();

    */#endif
    ?>



    Read Next