6 HTTP GET Variables

There is something called the Collatz Conjecture. It’s not known whether it is true or false. Start with any positive integer. If it’s odd, you multiply it by 3 and add 1 to it. If it’s even, you divide it by two. Keep doing this and the Collatz Conjecture claims you’ll always eventually arrive at 1. Let’s see this in action with 5.

5 is odd. 3*5+1 = 16. 16 is even. 16/2 = 8. 8 is even. 8/2 = 4. 4 is even. 4/2 = 2. 2 is even. 2/2 = 1.

So, 5 was not a counter example. Let’s code the logic of the Collatz Conjecture before we connect to the web. Our first bit of code:

<?php

$value = 17;

while($value!=1) {
    if($value%2==1) {
        // echo the first part of the statement
        echo "$value is odd. 3*$value+1 = ";
        // actually change $value to the new value
        $value=3*$value+1;
        // echo the last part with $value update
        echo "$value\n";
    }
    else {
        // echo the first part of the statement
        echo "$value is even. $value/2 = ";
        // actually change $value to the new value
        $value=$value/2;
        // echo the last part with $value update
        echo "$value\n";
    }
}

Run it a few times in the console with different values. Everything I’ve tried has gone to 1 after some number of steps. Once you’ve gotten a sense of things, let’s connect to the web. Instead of “$value = 17;” we’re going to replace the code on line 3 with this:

if(isset($_GET["value"])) {
    $value = $_GET["value"];
} else {
    $value = rand(1,100);
}

Name your file something like collatz.php and serve it by typing the following in your terminal:

>php -S localhost:8080 collatz.php

In your web browser, head to http://localhost:8080 and give it whirl. I’ve set it up so it will generate a random positive integer if one isn’t provided. Things might be looking pretty messy. For web, instead of “\n” we’ll want to use “
” the HTML element for break. Replace instances of “\n” with “
” and refresh your browser. You’ll end up with a different looking page every time you refresh because it is dynamically generated. Refresh it once more and based on a new random integer, you’ll see another entirely different page.

We can actually input a number now. In your browser, if you go to the url: http://localhost:8080?value=81 it will run for the value you provided through the URL. Give that a try now. Since we’re utilizing the web now, let’s take advantage. Let’s put a couple lines of code between the if-else-statement handling the $_GET variable and the start of the while-loop. Something like:

echo '<a href="/?value='.($value+1).'">Try '.($value+1).'</a> | ';
echo "<a href=\"/\">Try another random integer</a><br>";

Writing combined HTML and PHP like this can be really confusing. You know how “<?php” needs to be at the beginning of our PHP files for a file to execute properly? Now is a good time to show another way to write PHP and HTML together. Take a look at this example file:

<?php

$value_0 = true;
$value_foo = "bar";

// we can close the php tag with and just write plain HTML for a bit
?>

<h1>Heading 1</h1>
<p>This is a paragraph.</p>

<?php 

// we can begin an if-statement in one PHP block ...

if($value_0) { 

?>

The value of $value_foo is: <?php echo $value_foo; ?></p>

<?php 

// ... and end it in another

} 

?>

Exercises:

  1. What happens if you put a very large integer into the Collatz Conjecture example? What happens if you put in a negative integer? What happens if you put in something like “potato”? When writing “real-life” applications, at best, you’ll get bad input from users, and at worst, hackers will attempt to maliciously exploit holes in your application. Can you think of ways you could already improve this example web application?

  2. Try to create a multi-lingual site where the language can be passed in as a URL parameter. For example: localhost:8080/?lang=es might load in Spanish, and available languages might be shown as flags that can be clicked.

  3. Try and write a choose your own adventure story that works in the browser. You can use page numbers in the URL, or get creative with things like localhost:8080/?you_are=running_away_from_a_troll