PHP reference

lafelabs.org

/phpreference/

PHP, the recursive acronym:

P HypertextProcessor HypertextProcessor Hypertext Processor

PHP is used to communicate between the web browser, which runs on your machine, called the "client", and the machine that stores the web page information, called the "server". If you are viewing an HTML file on your own computer, not using a server, PHP doesn't do anything useful. But for pages stored on pretty much any server, PHP is such a widely used standard that we can generally assume any of the numerous choices for web hosting and serving will support it.

This is important for two things as far as Geometron is concerned: saving files to the server, allowing your work to remain permanent without saving on your local machine, and connecting to the physical aspects of the machine for robotics and measurement. In the latter case, PHP will be combined with python scripts and shell scripts to be discussed later. In the former case, everything is based on one template that we use over and over, called saver.php or something like it.

Here is saver.php:

                <?php
                /* javascript this pairs with:
                
                document.getElementById("editor").onkeypress = function(){//save on every key press
                    var httpc = new XMLHttpRequest();
                    var url = "saver.php";        
                    httpc.open("POST", url, true);
                    httpc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    httpc.send("text=" + document.getElementById("editor").value);//send text to saver.php
                }        
                */
                
                   $str = $_POST["text"]; //get text from textarea at /editor/editor.html 
                   $filename = "index.html";  //put it in main webpage there
                   $file = fopen($filename,"w");// create new file with this name
                   fwrite($file,$str); //write data to file
                   fclose($file);  //close file
                ?>
        

Of course this is a small subset of the PHP language. PHP can do just about anything any other language can do. But for Geometron we want only a tiny range of actions, for simplicity. The code above shows the PHP required to save a file(while creating that file). The only two things that I want in this reference taht are not in that code are joining strings together and running commands.

The joining of strings is mostly worth mentioning because it's so odd. In numerous languages including JavaScript, joining strings is done via a simple "+" sign just as in addition of numbers. In PHP, string variables are added with a "." as follows:

$outputstring = $string1.$string2;
    

Comments in PHP are either a "//" or "/* */" as with JavaScript. Variables of all types are denoted by a dollars sign before the name of the variable.

Now all that remains is to add a reference on running arbitrary programs on the command line from php. Here is an example of that.

        < ?php
        exec ("./pythonprogram.py");
    ?>

That's it! the command "exec" runs whatever goes in the quotes and parentheses, in this case a python program which has been made into an executable(see the Python section of the Geometron docs).

        <?php
        $basecube = file_get_contents('https://pastebin.com/raw/NQMMPPdT');
        $basecubearray = explode("\n", $basecube);
        for($index = 0;$index < count($basecubearray);$index++){
            $subarray = explode(":",$basecubearray[$index]);
            echo byteCode2string($subarray[1]);
        }
        
        function byteCode2string($inputbytecode) {
            $bytecodearray = explode(",", $inputbytecode);
            $outputstring = " ";
            for ($index = 0; $index < count($bytecodearray); $index++) {
                if(strlen($bytecodearray[$index]) > 0){
                    $outputstring .= chr(octdec($bytecodearray[$index]));
                }
            }
            return $outputstring;
        }
        function string2byteCode($inputstring){
            $outputcode = "";
            for( $index = 0; $index < strlen($inputstring);$index++){
                $outputcode .= "0".decoct(ord($inputstring[$index])).",";
            }
            return $outputcode;
        }
        ?>
        <?php
        $homepage = file_get_contents('https://pastebin.com/raw/NQMMPPdT');
        echo $homepage;
        ?>
        <?php
        $basecube = file_get_contents("origin.txt");
        $basecubearray = explode("\n", $basecube);
        $subarray = explode(":",$basecubearray[0]);
        $indexhtml = byteCode2string($subarray[1]);
        $indexhtml .= "\n";
        $indexhtml .= $basecube;
        $indexhtml .= "\n";
        $indexhtml .= file_get_contents('root.txt');
        $indexhtml .= "\n";
        $indexhtml .= file_get_contents('font.txt');
        $indexhtml .= "\n";
        $indexhtml .= file_get_contents('shapes.txt');
        $indexhtml .= "\n";
        $indexhtml .= file_get_contents('geometron.txt');
        $indexhtml .= "\n";
        $indexhtml .= file_get_contents('generators.txt');
        $indexhtml .= "\n";
        $indexhtml .= file_get_contents('scrolls.txt');
        $indexhtml .= "\n";
        for($index = 1;$index < count($basecubearray);$index++){    
            $subarray = explode(":",$basecubearray[$index]);
            $indexhtml .= byteCode2string($subarray[1]);
        }
        $file = fopen("index.html","w");
        fwrite($file,$indexhtml);
        fclose($file);
        
        ##############
        function byteCode2string($inputbytecode) {
            $bytecodearray = explode(",", $inputbytecode);
            $outputstring = " ";
            for ($index = 0; $index < count($bytecodearray); $index++) {
                if(strlen($bytecodearray[$index]) > 1){
                    $outputstring .= chr(octdec($bytecodearray[$index]));
                }
            }
            return $outputstring;
        }
        function string2byteCode($inputstring){
            $outputcode = "";
            for( $index = 0; $index < strlen($inputstring);$index++){
                $outputcode .= "0".decoct(ord($inputstring[$index])).",";
            }
            return $outputcode;
        }
        ?>

Here is the JavaScript required to run a php program, in this case creator.php:

        document.getElementById("create").onclick = function(){
            var httpc = new XMLHttpRequest();
            var url = "creator.php";
            httpc.open("GET", url, true);
            httpc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            httpc.send();
        }