Continuing the discussion from https://www.sitepoint.com/community/t/action-or-action-how-do-they-really-differ/343321/7.
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!!
Continuing the discussion from https://www.sitepoint.com/community/t/action-or-action-how-do-they-really-differ/343321/7.
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!!
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! 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)
You are helping us to learn, so it is exemplary!
Thanks for this nifty little tool - Iâm adding it to my library for future reference and to help others!
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!
Itâs a good demo.
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.
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.";
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."
;
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.
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.
Here is a php5 version. Your down to two excuses now.
$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.
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â.
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"
?
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.
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 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.