Computers and Technology
A PHP poem
We all know PHP is a lot of fun in on itself but let’s put some basic skills into making a creative script. Something, you probably wouldn’t even consider doing because it seems like a waste of time. Don’t laugh, I know what you’re thinking. And I know that you’ll laugh when you finish reading this article.
So how about we write a PHP poem.
Yeah, you think it’s silly, but so what? I found that making useless fun code is the best way to learn, at least for a beginner. It helps getting used to setting up the variables, doing switches, and making sense of forms. I wrote this piece of code when I started out with PHP a couple months ago. Now let’s start!
First, set up the variables. I decided to make my poem a bit “dynamic” (or rather interactive) so I used some different feelings that the user can choose from:
$happiness = "happy, and joyful about your life"; $sadness = "sad, and just plain down"; $loneliness = "lonely because no one is around"; $togetherness = "companionship because you love your friends"; $love = "love, pure love."; $depression = "depressed, because that happens sometimes, too";
Haha, excuse my poor code-to-wordpress integration skills, it seems that google syntax highlighting hates me. I’ll fix it later when I find a good plugin.
Anyways, here we go, I set up a few basic variables describing emotions that I want to use in my script. These are all classified as strings for those of you who don’t know.
Now let’s put them in an array:
$feelings= array( $love, $depression, $sadness, $happiness, $loneliness, $togetherness);
This will make it easier to echo out. Next, I’ll set an “if” statement to check if anything was submitted to the $_POST global variable. This variable is generally used when submitting form data, intrigued about what I’m going to do next?
if (!isset($_POST['submit'])){
Are you feeling:<br />
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<?php
foreach ($feelings as $feel) {
print "<input type=\"checkbox\" name=\"whatifeel[]\" value=\"$feel\">$feel<br>";
}
?>
<input type="submit" name="submit" value="Select" />
The “if (!isset($_POST['submit']))” piece of code is probably the most fundamental and amazing part of PHP. As you can see above, the code will be self-sustaining, it will not need another external file. The “if” statement makes sure there are no $_POST values submitted and when they’re not, it echoes out a form which will THEN submit a value to $_POST. I know, it’s ingenious.
Let’s move on though, what does the form above say? Let’s translate it to plain English because this is the most important part of the script:
If there are no values submitted to the $_POST variable,
write “Are you feeling:”
then, show a form that will submit its information to “PHP_SELF”, which is the site itself, using the “POST” method.
next, for every variable in the $feelings array, show the value of the variables and show a checkbox that will send data to the “whatifeel[]” array(the data will be stored in $_POST['whatifeel'].
Finally, show a button that will submit all of the information to the server.
Let’s move onto the action, getting the work actually done. The next part of this code will feature the “else” code as well as the final result after you submit the data into the server.
<?php
}
else {
foreach (($_POST['whatifeel']) as $f) {
echo "I'll tell you what is happening, <br>
and how you feel, <br>
and what is real<br><ul>";
switch ($f) {
case $happiness:
echo "<li>And when you envision yourself<br>
in love, with money and wealth<br>
you'll feel happy.<br><br>";
break;
case $loneliness:
echo "<li>And when you remember him or her, <br>
he or she does not prefer <br>
your company nor you.<br><br>";
break;
case $sadness;
echo "<li>And when you think that person or event<br>
the memory of sight and scent<br>
you remember your sadness.<br><br>";
break;
case $togetherness;
echo "<li>And when you look around and see <br>
you and your present company<br>
you'll be glad to be.<br><br>";
break;
case $love;
echo "<li>And when you see that one,<br>
she will become, <br>
and represent your love.<br><br>";
break;
case $depression;
echo "<li>And when you feel on the inside<br>
and you know you've tried<br>
you feel sad and depressed.<br><br>";
break;
}
}
}?>
There we go. Now let me explain the mechanics of this: First, I closed the “if” tag and introduced the “else”. Next, I used “foreach” to echo out the first stanza of the poem (I’ll tell you what is happening…). This stanza will repeat for every variable submitted into $_POST['whatifeel']. The second stanza depends on the variables submitted so I made a switch. This switch will echo out the next stanza depending on what variable it’s dealing with. If you submitted the value “$sadness”, it will echo out “And when you think….” as the second stanza. The process will repeat for every single value you submitted in the first part of this process. I added the “<li>” and “<ul>” tags to make the script look a bit more interesting when used. The script will also end up echoing out “steps”.
Here’s what the first part of the script looks like when parsed:
And after you select the feelings, here’s your poem:
Try it out for yourself! I uploaded the script. I’ll have a download version for you in a couple of days! I hope you enjoyed my rather simple and fun tutorial.
The point of these simple tutorials is to teach you the basics and how to apply them to do something fun. I don’t think you’ll ever use this exact script anywhere (and if you do, you better give me credit!
) but you’ll use parts of it like the !isset($_POST['submit']) and the forms, and the switch too of course. It’s easier to learn PHP if you apply to something you already know and what’s fun for you. I like writing so this presented an opportunity for me to express my writing in an experimental form. Try it out!
| Print article | This entry was posted by Admin on September 10, 2009 at 10:43, and is filed under PHP and Scripting, Tutorials. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |

about 12 months ago
This is genius! Thanks Dave. I am playing with PHP and poetry right now and will send you a link when I am done. For now, you might like this poem
Our Index.html
I think that if we went though all the iterations of our code
And finally figured out how to edit our HTML
We would still be left wondering around in the dark for that CSS document
We all call too
You see, there is more to us that is common then we pretend
I know that we all spend hours with our cracked version of macromedia
Only to realize in the end
It is our flash which causes the down fall of communication
If we could just lean to communicate with the tools that we were given from birth
We would not need to try to one up each other
This game of leap frog gets old when you start to pass
The same landmarks you started at
Even when you Photoshop them
from: http://philosophicalart.com/2011/01/our-index-html/