Intro to Programming Database Internet of Things IT Project Management Networking Web Development Security For Research Students

Introduction

This part of the website is a series of hyperlinks to tutorials on PHP and exercises designed to get students familiar with PHP on the web. We principally rely on the W3Schools PHP tutorial for lesson materials.

Any questions or comments should be directed to: The creator's email

Standard PHP Structure

Prerequisites

Students need to know the introduction to PHP syntax to do this exercise.

Exercise

Output the words "Hello World!" on screen in PHP.

An example is below.

Mathematical Operations

Prerequisites

Students need to know:

Exercise

Take in two variables, and sum the result. At this stage, there is no need to do error checking so assume they are numbers.

An example is below. You do not have to develop the user interface, just the PHP part.




The answer is:
0

Calculate the Length of a String

Prerequisites

Students need to know:

Exercise

Given a string input, tell us how long it is.

An example is below. You do not have to develop the user interface, just the PHP part.



The length of the string is:
0

Is a Word a Palindrome

Prerequisites

Students need to know:

Exercise

Given a string input, determine if it is a palindrome, i.e., a word that is spelled the same backwards and forwards. Radar, and racecar are palindromes. Robot and race are not.

An example is below. You do not have to develop the user interface, just the PHP part.



Is the string input a palindrome?

Count Consonants and Vowels

Prerequisites

Students need to know:

Exercise

Given a string input, (1) count the number of a, e, i, o and u in the string and (2) count the number of other characters in the string, ignoring spaces.

An example is below. You do not have to develop the user interface, just the PHP part.



Calculating a Loan Payback

Prerequisites

Students need to know:

Exercise

Calculate the number of years required to pay back a loan based on compound interest. Compound interest works as follows. You borrow some money at a particular annual interest rate. Interest is paid in periods per year. The interest per period is the annual interest rate divided by the number of periods. Every period, you pay a certain amount, which reduces your debt. But every period, interest is then calculated on the remaining amount. The question is, "How many years does it take to pay off the loan?"

The algorithm is as follows:

  Let loanamt be the loan amount
  Let period be the number of periods
  Let annint be the annual interest rate as a percentage number
  Let payback be the amount to pay back each period 

  Let periodint=(annint/period)/100;
  if the interest payment after the first payback > payback 
    say 'This loan cannot be paid off!'
  else 
    Let x=0
    Let calcamt be loanamt
    while calcamt >0
      increment x
      say For period x after payback, the loan size is
      subtract payback from calcamt
      say calcamt formatted to two decimal places
      say . After interest, the amount is
      add interest to calcamt
      say calcamt formatted to two decimal places
      say .<br />
    end while calcamt >0
    say It will therefore take
    if x divided by period has a remainder of 0
      say x divided by period
    else
      say x divided by period +1
    end if x divided by period has a remainder of 0
      say years to pay back the loan.
  end if else the interest payment after the first payback > payback

An example is below. You do not have to develop the user interface, just the PHP part.



%




Alphabetically Sort a Comma Delimited List

Prerequisites

Students need to know:

Exercise

Given a comma delimited list of words without spaces, sort the list alphabetically.

An example is below. You do not have to develop the user interface, just the PHP part.



Convert a Text File to HTML

Prerequisites

Students need to know:

Exercise

Certain characters on a keyboard cannot be rendered in HTML directly, but must be substituted with other characters. These include:

Transform a text file to be rendered by HTML. Sample text files you can use include the version of Gulliver's Travels at http://cecilchua.online/phpex/gulliver.txt and La Belle Dame Sans Merci at http://cecilchua.online/phpex/sansmerci.txt

An example is below. You do not have to develop the user interface, just the PHP part.



Convert Between Celsius, Farenheit and Kelvin

Prerequisites

Students need to know:

Exercise

Given a temperature in Celsius, Farenheit or Kelvins, convert to another user-specified temperature measure.

The formula for converting from celsius to farenheit is:

$$ {}^\circ F=\frac{9}{5}{}^\circ C+32$$

The formula for converting from farenheit to celsius is:

$$ {}^\circ C=\frac{5}{9}(^\circ F-32)$$

The formula for converting from celsius to kelvin is:

$$ {}^\circ C={}^\circ K - 273.15$$

An example is below. You do not have to develop the user interface, just the PHP part.





Sort a List of Books

Prerequisites

Students need to know:

Exercise

Add the following line to your code:

  $booklist=json_decode(file_get_contents('http://cecilchua.online/webdev/getbook.php'));

          

This creates an array of books in your code. Each object in the array is defined as part of the class book, which looks like this:

  class book{
    public $isbn;
    public $title;
    public $category_code;
    public $category_name;
    public $listprice;
    public $discount;
  }
          

Develop code to sort this list of books by any of the attributes in ascending order.

An example is below. You do not have to develop the user interface, just the PHP part.



Implement Fibonacci Recursively

Prerequisites

Students need to know:

Exercise

The Fibonacci Sequence is a sequence of numbers where the next number in the sequence is the sum of the two numbers preceding it. The sequence looks like this:
0, 1, 1, 2, 3, 5, 8, 13...

Implement the Fibonacci sequence in PHP such that Fibonacci(0)=0, Fibonacci(1)=1 and otherwise if n>1, Fibonacci(n)=Fibonacci(n-1)+Fibonacci(n-2).

An example is below. You do not have to develop the user interface, just the PHP part.