Displaying GET values as a learning tool

Continuing the discussion from Action="" or action="?", how do they really differ?.

@fullstackdev,

Care to post your PHP code here?

I would like to add that to my library - very useful to learn how URLs and PHP work!! :+1:

1 Like

Sure! This is the PHP which is inside the div tag with id “answer”:

<?php
if ($_GET['color'] == "red") {
        echo "<span style=\"color:red;\">The query string had color set to red.</span>";
} else if ($_GET['color'] == "blue") {
        echo "<span style=\"color:blue;\">The query string had color set to blue.</span>";
} else {
        echo "Take a look at the URL in your browser's address bar! There's no query string.";
}
?>

But please don’t use that as exemplary code, since I threw it together pretty quickly! :blush: Usually I wouldn’t use echo to output HTML.

Attached is the full PHP file in case you want to look at the whole thing.

test-post.php (1.3 KB)

1 Like

@fullstackdev,

You are helping us to learn, so it is exemplary! :wink:

Thanks for this nifty little tool - I’m adding it to my library for future reference and to help others!

1 Like

Glad you liked the demo!

To be a stickler about it, usually I don’t like to put HTML into PHP, so I’d do it like this:

<?php
if ($_GET['color'] == "red") {
?>
<span style="color:red;">The query string had color set to red.</span>
<?php
} else if ($_GET['color'] == "blue") {
?>
<span style="color:blue;">The query string had color set to blue.</span>
<?php
} else {
?>
Take a look at the URL in your browser's address bar! There's no query string.
<?php
}
?>

I find it easier to read and edit the HTML outside of PHP. But opinions differ on that. In this case, there were just a few quotes, so I used echo and escaped the quotes.

And FWIW there are some really interesting questions in this community. Even just trying to answer some of them is a learning experience for me!

3 Likes

It’s a good demo. :slight_smile:
It’s not quite ‘exemplar-y’ because it generates errors (It should check for the existance of $_GET[‘color’] before attempting to access an array index. See isset.), but it’s still a good demonstration of the action’s behavior.

1 Like

If I may, that whole chunk of code can be written in two lines. It also addresses the point @m_hutley made about errors.

$x = $_GET['color'] ?? '';
echo $x ? "<span style='color: $x;'>The query string had color set to $x.</span>" : "Take a look at the URL in your browser's address bar! There's no query string.";
1 Like

Apart from value existing I far prefer the first version, so much easier to fathom out what is happening rather than a one liner.

You find it difficult to fathom ONE null coalese and ONE ternary and would rather write duplicate and redundant code? What if you had 20 colors? What about 100 colors? 500? Are you going to write endless if/elses for all colors and then hope you don’t miss one?

How about this then?

$x = $_GET['color'] ?? '';
$t = "<span style='color: $x;'>The query string had color set to $x.</span>";
$f = "Take a look at the URL in your browser's address bar! There's no query string.";
echo $x ? $t : $f;

Complication arises with very long lines.

I prefer this method because it is easier to read at a single glance:

    $x = $_GET['color'] ?? NULL;     // || FALSE || '';
    echo $x 
          ? "<span style='color: $x;'>The query string had color set to $x.</span>"
          : "Take a look at the URL in your browser's address bar! There's no query string."
          ;

1 Like

tbh, i’d prefer the original over either of the coalesces from a readability standpoint, in terms of a demonstration of the code flow.

Null Coalescence is 1) Available in PHP 7 only. 2) Brand new to most people, and 3) Not intuitively readable for non-experienced coders. Or even most experienced ones, because of #1 and #2.

2 Likes

No, that’s what a switch statement is for.

Seriously @m_hutley? You would write hundreds to thousands to even millions of switch statements to cover every single color? Sorry, but that is just ridiculous. There are 16,777,216 RGB color combinations. Have fun writing a switch statement for that.

All excuses.

  1. Excuse for not coding to current coding standards or updating outdated code
  2. Excuse for not learning and keeping up with your craft
  3. See #2

Here is a php5 version. Your down to two excuses now. :wink:

$x = isset($_GET['color']) ? $_GET['color'] : '';     
 echo $x 
          ? "<span style='color: $x;'>The query string had color set to $x.</span>"
          : "Take a look at the URL in your browser's address bar! There's no query string."
          ;

* I am speaking from a coding perspective, not from what this example was intended for.

@John_Betong, yes , I agree.

1 Like

Which just says “I’m talking to hear myself talk”. The purpose was to demonstrate THIS point, not pontificate about how things ‘should be from my coding perspective’.

  1. Find me a standard that says you must use the null coalescence operator.
  2. We go back to the conversation we had in private. STOP ASSUMING EVERYONE WHO COMES TO THIS FORUM IS A PROFESSIONAL CODER WITH YEARS OF EXPERIENCE. You CANNOT assume that any viewer of a thread is ‘keeping up with their craft’.
  3. That is the form of the first line that I was saying should be used as an example (though I still dont like the readability to the ternary in that style). So… you havent ‘removed one of my excuses’, you’ve proven my point. Thanks.

At some point, a good developer will refactor! Nice generalization… but, let’s not forget to validate the color value that is input - something I didn’t do. What happens if the value of $_GET['color'] is "rouge"? :wink:

2 Likes

Sure. I understand the point was just to quickly demonstrate the action differences. The only reason I chimed in was because because someone called the code “exemplary”. Seemed like a good time to talk about it. :grinning:

1 Like

Well, not really… I myself averred that the code was not exemplary - but then, I have never been afraid to show my crap code, and that’s not going to change lol. @UpstateLeafPeeper said it’s exemplary “in the sense that it’s helping people to learn” (so, the demo is an “example” of how to do something, not necessarily coded the best way), and also @m_hutley said that it’s “not exemplar-y” either because it produces an error when there’s no query string. I don’t think anyone is saying this is a great coding example, we all agree :slight_smile: but I think we also all agree that’s not the point.

My only point in putting the code there is to demonstrate something about forms using a POST method and query strings. Now, I do hope people won’t be afraid to show their code because they might get critiqued on it when demonstrating something. There’s nothing wrong with getting code critiqued.

As long as it’s civil, it’s nice to see discussion about what is the “best” way to write something or improvements that can be made… This goes outside of the point of the original topic, but that’s okay. It’s like having a friendly code review - how can we improve this code? It’s rare to get such feedback, in my experience! So thanks for the input.

4 Likes

@fullstackdev, you are a a gentleman and a scholar. :grinning:

1 Like