Menu

Beginner Excercises to Learn PHP Programming

I’ve been on a quest to try my hand at a real programming language. I get the feeling that I’m really going to like what I learn. Said another way, I really hope that I like what I learn. My brain kind of works like a computer and if I can just figure out how a computer operates on the nano level, I’ll be one step closer to understanding myself.

That was a joke.

After a thoroughly inexhaustive consideration of available languages, I chose PHP to learn a few things. Since I’ve been doing work in WordPress (written in PHP) and have had a few people express the need for database functionality in their webpages (PHP and MySQL can handle almost anything you can throw at it) AND I’m working on a site based on Pligg (also written in PHP), it seemed like the right direction to go. It also seems fairly simple to pick up (comparatively) and the support/community/documentation is quite robust (one of the few words I use despite the fact that I hate it).

Despite the many pages dedicated to PHP tutorials, how-tos, and tips/tricks, I have not been very successful finding exercises I can practice with. I’ve been a student for longer than I care to admit, I need homework to function properly!! So I searched high and low and put together this list of beginner PHP exercises. I’m also including a link to my results so people can see the results (no guarantee of accuracy).

Zend’s Beginner’s Guide to PHP is a  good place to start if you need a refresher or need some help.

From the Digital Point forums

  1. Define a variable called ‘name’ containing the text ‘Mario’ and then write “I am Mario.” using the variable to write the ‘Mario’ part. You may only use two commands max!
  2. Use the code from the previous example but add a check using an ‘if’ condition to check whether the name is ‘mario’. Place the text from the previous example in the ‘if’ condition.
  3. Try changing the $name variable to ‘awaken’. You’ll notice that the previous script writes nothing (If it doesn’t, go back and redo #2, it’s not complete!)
  4. Use a while-loop that starts at 0 and keeps running while the number is lower then 5. Write the variable to the screen after each run.
  5. Modify the previous example to use a ‘for’ loop instead of a ‘while’ loop

From Huzilla.org’s PHP book

A quick note about this site. The “book” is very useful but on every page you’ll see a big headline that tells you that that particular version in not the most current. The current version took a minute to load and was in wiki format. I didn’t immediately see any of the quizzes or exercises so I’m linking to the old version.

I’m only listing the first few sections of quizzes (the beginner information). Each section listed in the table of contents [http://www.hudzilla.org/phpbook/index.php] has its own exercise bank. URL links have been removed because their server is terribly slow at times and times out occasionally. Keep trying, it’s there (unless you get a 404).

  • Quiz 1 (Introduction to PHP) [http://www.hudzilla.org/phpbook/read.php/2_10_0]
  • Quiz 2 (Variables and Operators) [http://www.hudzilla.org/phpbook/read.php/3_14_0]
  • Quiz 3 (Functions) [http://www.hudzilla.org/phpbook/read.php/4_25_0]
  • Quiz 4 (Arrays) [http://www.hudzilla.org/phpbook/read.php/5_13_0]
  • Quiz 5 (Objects) [http://www.hudzilla.org/phpbook/read.php/6_21_0]
  • Quiz 6 (Forms) [http://www.hudzilla.org/phpbook/read.php/7_10_0]
  • Quiz 7 (Files) [http://www.hudzilla.org/phpbook/read.php/8_18_0]
  • Quiz 8 (Databases) [http://www.hudzilla.org/phpbook/read.php/9_21_0]
  • Answers to all quizzes [http://www.hudzilla.org/phpbook/read.php/24_4_0]

From Iowa State University

“Download the yeast.txt file from the website. Write a script that displays the names of all the protein-fragments contained in the file [the block of capital letters only].

“Adapt [the script above] to construct an array while you read through the file that contains all the titles of fragments. Now encapsulate that functionality in a PHP function GetFragmentTitles($fn) whereby $fn is name of the file and the return result is an array containing all the titles. The pseudocode for this function is as follows:

function GetFragmentTitles($fn) {
// open file $fn
// create a new array $titles
// for each $line in file $fn
// if $line represents a title
// add the title to array
// close the file
return $titles;
}

“Construct a form where the end-user can enter (part of) the name of a fragment. A second script subsequently searches the titles to see if (part of) the name occurs in the file. Report on how many fragments are found.

“Simulate a BLAST-search interface: a user can enter part of a sequence and your script should report if and where the subsequence is found. Use really small fragments to see if they show up multiple times. What do you report if no fragments are found?

  • Extra: What if the user enters an empty string?
  • Extra: Can you think of a good use of the strtolower() function in this scenario?
  • Extra: Can you highlight the found sequence?”

W3 Schools PHP Quiz

Replies

Total: 5

Your email address will not be published. Required fields are marked *


  1. Rex

    June 11, 2013 at 8:51 am  •  Reply

    Rex says:

    Thank you for this page. I hope I get to talk to someone on this page in case I want to ask something and get to know some PHP Gurus.


  2. luis ramirez

    January 17, 2012 at 4:34 pm  •  Reply

    luis ramirez says:

    deseo aprender mas a fondo sobre php

    • Josh

      January 27, 2012 at 1:27 pm  •  Reply

      Josh says:

      Google Translate tells me you said “I want to learn more deeply about php” to which I reply… Good luck, it’s a fun language to use!


  3. jn2

    November 2, 2010 at 4:36 pm  •  Reply

    jn2 says:

    I can see this is an old post, but are you still looking for PHP exercises? We’ve just put up a site with 26 PHP exercises (http://phpexercises.com/). No tutorial, just exercises.

    So far, they cover variables, loops, conditional statements, forms, arrays and functions, as well as some other things. You see the problem first, then click a button for the answer script and a link to the script’s output.

    Come try it out and let us know how it works for you.

    BTW, that Zend tutorial you’ve linked to at the top of the page is a really good one.

    • Josh

      November 3, 2010 at 5:12 am  •  Reply

      Josh says:

      Great resource! Thanks for the link!

"Policy"

If you've got something to say about the above, now is your time. I moderate for spam, relevance, and abuse but, aside from that, this is an open forum. I will not publish your email address but feel free to be anonymous. If you just have a general question or want to get in touch, my contact form is the best place for that. Thanks in advance!