As we learned yesterday, PHP is a server-side scripting language. We will use it in this class to build web servers clients can request data from in different formats (plain text, JSON, etc.).
Today, we will practice writing simple PHP code. Next week, we will use these basics to create our own fully-functional API!
Overview of Section Topics:
First, let's make a new directory and index.php
.
Let’s stick a simple PHP script in there.
<!DOCTYPE html>
<html>
<head>
<title>Testing PHP</title>
</head>
<body>
<?php
echo "Hello!";
?>
</body>
</html>
Let's take a look.
The following PHP pages each contain one or more bugs. The bugs could be syntax errors or incorrect logic. Look at each page, find the bug(s) and correct the errors.
(go down to work on these!)
helloWorld
Write a PHP function helloWorld
which prints the following output:
Hello World!
Write your function in a PHP file called helloWorld.php
.
helloWorld
SolutionRunning solution here
Source code here
repeat
Write a function named repeat
that accepts a string and a number of
repetitions as parameters and returns the string concatenated that many times. For example,
the call of repeat("echo...", 3) returns
"echo...echo...echo...".
If the number of repetitions is 0 or less, return an empty string.
Write your function in a PHP file called repeat.php
and
after defining your function, make a call to it with the string and count parameter
values of your choice, then print the result to the output.
repeat
SolutionRunning solution here
Source code here
containsTwice
Write a function named containsTwice
that accepts a string and a character as
parameters and returns true if that character occurs two or more times in the string.
Your function should ignore case when comparing strings. For example, the call of
containsTwice("helLo", "l")
should return true because there are two
"l" characters in that string, but containsTwice("helLo", "e")
should return
false.
Write your solution in a PHP file called containsTwice.php
, and write
a call to your function with parameters of your choice, printing the result to the output.
containsTwice
SolutionRunning solution here
Source code here
arrayMystery
toString
indexOf
switchPairs
(challenge)(go down to work on these!)
arrayMystery
Consider the following PHP code:
function arrayMystery($arr) {
for ($i = 1; $i < count($arr); $i++) {
$arr[$i] = $arr[$i] + $arr[$i - 1];
}
return $arr;
}
Indicate in the right-hand column what values would be stored in the returned array after the
function arrayMystery
executes if the array in the left-hand column is passed as a parameter
to arrayMystery
. Include your answers in the format of [a, b,
c]
where a,
b, and c are numbers in the array result (for a 3-element array).
[8]
:
[6, 3]
:
[1, 2, 3, 4]
:
[7, 10, 12, 12, 17]
:
toString
Write a function toString
which takes an array as a parameter and prints
the array to the output in a single line, where each element is separated by a ","
followed by a space, and the entire array is surrounded by "[" and "]".
For example, if an array called $arr
stored the following values:
$arr = [1, 5, 4];
Your function should return the string "[1, 5, 4]"
Write your solution in a PHP file called toString.php
and then print the
string result of an example call to your function with an array of your choice. Remember
to test the empty array solution ("[]") and the single-element solution ("[1]").
toString
SolutionRunning solution here
Source code here
indexOf
Write a function named indexOf
that returns the index of a particular value in an array
of integers. The function should return the index of the first occurrence of the target
value in the array. If the value is not in the array, it should return -1. For example,
if an array called $arr stores the following values:
$arr = [42, 7, -9, 14, 8, 39, 42, 8, 19, 0];
Then the call indexOf($arr, 8)
should return 4 because the index of the first occurrence
of value 8 in the array is at index 4. The call indexOf($arr, 2)
should return -1
because value 2 is not in the array.
Write your solution in a PHP file called indexOf.php
and print out the
result of calling your function with an example array and index.
Use the
toString
function you wrote in the previous exercise to print your
array.
indexOf
SolutionRunning solution here
Source code here
switchPairs
(challenge problem)
Write a function named switchPairs
that switches the order of values in an array of
integers in a pairwise fashion. Your function should accept as a parameter an array of
integers and should switch the order of the first two values, then switch the order of
the next two, switch the order of the next two, and so on. For example, if the array
initially stores [1, 4, 8, -3, 2, 7 ]
, then your function should switch the first pair,
(1 and 4), the second pair (8 and -3) and the third pair (2 and 7) to yield [4, 1, -3,
8, 7, 2]
. You should return the new array state.
If there are an odd number of values in the array, the final element is not moved. For
example, if the original array had been [1, 2, 3, 4, 5 ]
, then the result returned would
be [2, 1, 4, 3, 5]
.
Write your solution in a PHP file called switchPairs.php
and print the
result of calling your function using an array of your choice. Use the
toString
function you wrote in the previous exercise to print your
array.
indexOf
SolutionRunning solution here
Source code here
Given a directory of character pictures (got.zip), write a PHP web service which vends these photos. These pictures represent characters in one of three "houses" (or families) in the Game of Thrones series: House Lannister, House Stark, and House Targaryen (yes, we know there are more, but you can always add more pictures!).
Your webservice should simply print out (in plain text) a list of images.
Expected Output:
images/lannister1.jpg
images/lannister2.jpg
images/lannister3.jpg
images/lannister4.jpg
images/stark1.jpg
images/stark2.jpg
images/stark3.jpg
images/stark4.jpg
images/stark5.jpg
images/targaryen2.jpg
images/targaryen3.jpg
images/targaryen4.jpg
When the PHP page is executed, all of the file names in the images
directory should be output, each on its own line.
After implementing the web service, add a variable in your code to be the string of one
of the three houses ("lannister", "stark", or "targaryen") and then modify your code to
print only the images which are related to a specific house.
Each .jpg
photo in images.zip
starts with the name of the character's house, followed by a
number value (e.g., lannister2.jpg
).
Print out the list of files including the images/
directory. This is loosely based on the assumption that your
php webservice and your HTML/CSS/JS application would be served out of the same directory where images/
sits.
Source (PHP)
Running Version (PHP)
Note: this exercise gives you the option of generating HTML content in a PHP page. Though this is a common practice throughout the web development industry, there are lots of reasons why generating HTML on the server side is suboptimal. For this class, this will be the only time that we ask you to write a PHP (or other server-side) program that generates HTML.
Given a folder of image files (puppy1.jpg, puppy2.jpg, etc.) write a PHP page that displays all images as seen here. You can get the images in this zip folder.
Solution (PHP)