PHP (DBC)
- Francis Korsah |
- Edward Yeboah |
This module equips students with the fundamentals of PHP with extensive practice in coding of functional web applications.
It entails the fundamentals of PHP and teaches students advanced PHP development tools and techniques, helps them in understanding the PHP data objects.
Course Information
CORE PHP OBJECTIVES
At the end of this module, students will be able to:
Understand how server-side programming works on the web.
Acquire knowledge in PHP Basic syntax for variable types and calculations.
Create conditional structures
Store data in arrays
Use PHP built-in functions and create custom functions
Understand POST and GET in form submission.
How to receive and process form submission data.
Reading and writing cookies.
Security tips (i.e. SQL Injection)
Create a database in phpMyAdmin.
Read and process data in a MySQL database.
CORE PHP MODULES
Topics to be covered;
Install and configure a PHP and MySQL package (XAMPP suggested)
Statements, variables, operators, comments, functions, scope
Database concepts and SQL, MySQL
-
PhpMyAdmin
Deployment
CORE PHP METHODOLOGY
Instructor led hands on training with supervised labs /Projects /Online
Sessions
CORE PHP COURSE MATERIAL
Beginning PHP 7.1 - Matt Doyle
video (https://www.youtube.com/watch?v=OK_JCtrrv-c)
weblink (https://www.w3schools.com/php/default.asp)
https://www.w3resource.com/php-exercises/
CORE PHP RESOURSES
VS Code, Visual Studio, note++, etc, Hosting Service, NetBeans, Eclipse, API for bulk SMS, Payment gateways, WhatsApp, Facebook.
CORE PHP PERSONAL ASSESEMENT
PHP LAB PROJECT WEEK ONE
1. Write a PHP script to get the PHP version and configuration information.
2. Write a PHP script to display the following strings.
Sample String :
'Tomorrow I \'ll learn PHP global variables.'
'This is a bad command : del c:\\*.*'
Expected Output :
Tomorrow I 'll learn PHP global variables.
This is a bad command : del c:\*.*
3. $var = 'PHP Tutorial'. Put this variable into the title section, h3 tag and as an anchor text within an HTML document.
4. Create a simple HTML form and accept the user name and display the name through PHP echo statement.
5. Write a PHP script to get the client IP address.
6. Write a simple PHP browser detection script.
Sample Output : Your User-Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
7. Write a PHP script to get the current file name.
8. Write a PHP script, which will return the following components of the url 'https://www.w3resource.com/php-exercises/php-basic-exercises.php'.
List of components : Scheme, Host, Path
Expected Output :
Scheme : http
Host : www.w3resource.com
Path : /php-exercises/php-basic-exercises.php
9. Write a PHP script, which changes the color of the first character of a word.
Sample string : PHP Tutorial
Expected Output :
PHP Tutorial
10. Write a PHP script, to check whether the page is called from 'https' or 'http'.
11. Write a PHP script to redirect a user to a different page .
Expected output : Redirect the user to https://www.w3resource.com/
12. Write a simple PHP program to check that emails are valid.
Hints : Use FILTER_VALIDATE_EMAIL filter that validates value as an e-mail address.
Note : The PHP documentation does not say that FILTER_VALIDATE_EMAIL should pass the RFC5321.
13. Write a PHP script to display string, values within a table.
Note : Use HTML table elements into echo.
14. Write a PHP script to display source code of a webpage (e.g. "http://www.example.com/").
15. Write a PHP script to get last modified information of a file.
Sample filename : php-basic-exercises.php
Sample Output : Last modified Monday, 26th June, 2017, 12:43pm
16. Write a PHP script to count number of lines in a file.
Note : Store a text file name into a variable and count the number of lines of text it has.
17. Write a PHP script to print current PHP version.
Note : Do not use phpinfo() function.
18. Write a PHP script to delay the program execution for the given number of seconds.
19. Arithmetic operations on character variables : $d = 'A00'. Using this variable print the following numbers.
Sample Output :
A01
A02
A03
A04
A05
20. Write a PHP script to get the last occurred error.
21. Write a PHP function to test whether a number is greater than 30, 20 or 10 using ternary operator.
22. Write a PHP script to get the full URL.
23. Write a PHP script to compare the PHP version.
Note : Use version_compare() function and PHP_VERSION constant.
24. Write a PHP script to get the name of the owner of the current PHP script.
25. Write a PHP script to get the document root directory under which the current script is executing, as defined in the server's configuration file.
26. Write a PHP script to get the information about the operating system PHP is running on.
27. Write a PHP script to print out all the credits for PHP.
28. Write a PHP script to get the directory path used for temporary files.
29. Write a PHP script to get the names of the functions of a module.
Note : Find XML, JSON functions etc.
30. Write a PHP script to get the time of the last modification of the current page
PHP LAB PROJECT WEEK TWO
31. Write a PHP program to swap two variables.
32. Write a PHP program to check whether a number is an Armstrong number or not. Return true if the number is Armstrong otherwise return false.
Note: An Armstrong number of three digits is an integer so that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number since 1**3 + 5**3 + 3**3 = 153
33. Write a PHP program to convert word to digit.
Input: zero;three;five;six;eight;one
Output: 035681
34. Write a PHP program to check the bits of the two given positions of a number are same or not.
112 - > 01110000
Test 2nd and 3rd position
Result: True
Test 4th and 5th position
Result: False
35. Write a PHP program to remove duplicates from a sorted list.
Input: (1,1,2,2,3,4,5,5)
Output: (1,2,3,4,5)
36. Write a PHP program to test if a given string occurs at the end of another given string.
37. Write a PHP program to compute the sum of the prime numbers less than 100.
Note: There are 25 prime numbers are there in less than 100.
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 and sum of all these numbers is 1060.
38. Write a PHP program to valid an email address.
39. Write a PHP program to get the size of a file.
40. Write a PHP program to calculate the mod of two given integers without using any inbuilt modulus operator.
41. Write a PHP program to print out the multiplication table upto 6*6.
Output:
1 2 3 4 5 6 2 4 6 8 10 12 3 6 9 12 15 18 4 8 12 16 20 24 5 10 15 20 25 30 6 12 18 24 30 36
42. Write a PHP program to find the first non-repeated character in a given string.
Input: Green
Output: G
Input: abcdea
Output: b
43. Write a PHP program that multiplies corresponding elements of two given lists.
44. Write a PHP program to print out the sum of pairs of numbers of a given sorted array of positive integers which is equal to a given number.
45. Write a PHP program to compute the sum of the digits of a number.
46. Write a PHP program to find heights of the top three building in descending order from eight given buildings.
Input:
0 = height of building (integer) = 10,000
Output:
Heights of the top three buildings: 45 25 24
47. Write a PHP program to compute the digit number of sum of two given integers.
Input:
Each test case consists of two non-negative integers x and y which are separated by a space in a line.
0 = x, y = 1,000,000
Output:
Digit number of sum of two given integers: 2
48. Write a PHP program to check whether three given lengths (integers) of three sides form a right triangle. Print "Yes" if the given sides form a right triangle otherwise print "No".
Input:
Integers separated by a single space.
1 = length of the side = 1,000
Output:
YES
49. Write a PHP program which solve the equation:
ax+by=c
dx+ey=f
Print the values of x, y where a, b, c, d, e and f are given.
Input:
a,b,c,d,e,f separated by a single space.
(-1,000 = a,b,c,d,e,f = 1,000)
Output:
Values of x and y: -1.684 2.737
50. Write a PHP program to compute the amount of the debt in n months. The borrowing amount is $100,000 and the loan adds 5% interest of the debt and rounds it to the nearest 1,000 above month by month.
Input:
An integer n (0 = n = 100) .
Output:
Amount of debt: 137000
51. Write a PHP program which reads an integer n and find the number of combinations. of a,b,c and d (0 ≤ a,b,c,d ≤ 9) where (a + b + c + d) will be equal to n.
Input:
n (1 = n = 50)
Output:
Number of combinations of a,b,c and d: 56
52. Write a PHP program to print the number of prime numbers which are less than or equal to a given integer.
Input:
n (1 = n = 999,999)
Output:
Number of prime numbers which are less than or equal to n: 618
53. Write a PHP program to compute the radius and the central coordinate (x, y) of a circle which is constructed by three given points on the plane surface.
Input:
x1, y1, x2, y2, x3, y3 separated by a single space.
Output:
Central coordinate(x,y) and radius of the circle: (1.000 1.000) 1.414
54. Write a PHP program to compute and print sum of two given integers (more than or equal to zero). If given integers or the sum have more than 80 digits, print "overflow".
Output:
46 overflow overflow ... overflow overflow
55. Write a PHP program that accepts six numbers as input and sorts them in descending order.
Input: Input consists of six numbers n1, n2, n3, n4, n5, n6 (-100000 = n1, n2, n3, n4, n5, n6 = 100000). The six numbers are separated by a space.
Output:
After sorting the said integers: 9 8 7 6 4 2
56. Write a PHP program to test whether two lines PQ and RS are parallel.
The four points are P(x1, y1), Q(x2, y2), R(x3, y3), S(x4, y4).
Input:
2
1.0 0.0 3.0 2.0 2.0 2.0 0.0 0.0
4.0 3.0 10.0 7.0 14.0 5.0 8.0 10.0
Output:
PQ and RS are parallel. PQ and RS are not parallel.
57. Write a PHP program to find the maximum sum of a contiguous subsequence from a given sequence of numbers a1, a2, a3, ... an. A subsequence of one element is also a continuous subsequence.
You can assume that 1 = n = 5000 and -100000 = ai = 100000.
Input numbers are separated by a space.
Input 0 to exit.
Sample Input:6
-4
-2
5
3
8
Sample Output:
16
58. There are two circles C1 with radius r1, central coordinate (x1, y1) and C2 with radius r2 and central coordinate (x2, y2)
Input numbers (real numbers) are separated by a space.
Write a PHP program to test the followings -
"C2 is in C1" if C2 is in C1
"C1 is in C2" if C1 is in C2
"Circumference of C1 and C2 intersect" if circumference of C1 and C2 intersect, and
"C1 and C2 do not overlap" if C1 and C2 do not overlap.
Sample Input:
2
0.0 0.0 6.0 0.0 0.0 5.0
0.0 0.0 3.0 5.1 0.0 3.0
Sample Output:
C2 is in C1. Circumference of C1 and C2 intersect.
59. Write a PHP program to that reads a date (from 2016/1/1 to 2016/12/31) and prints the day of the date. Jan. 1, 2016, is Friday. Note that 2016 is a leap year.
Two integers m and d separated by a single space in a line, m ,d represent the month and the day.
Sample Input:
9 15
Sample Output:
The day is: Thursday
PHP LAB PROJECT WEEK THREE
60. Write a PHP program to print mode values from a given a sequence of integers. The mode value is the element which occurs most frequently. If there are several mode values, print them in ascending order.
A sequence of integer's ai (1 = ai = 100). The number of integers is less than or equals to 100.
Sample Input:
6
7
4
6
9
8
6
4
10
8
4
5
Sample Output:
Mode values (in ascending order): 4 6
61. Write a PHP program which reads a text (only alphabetical characters and spaces.) and prints two words. The first one is the word which is arise most frequently in the text. The second one is the word which has the maximum number of letters.
Note: A word is a sequence of letters which is separated by the spaces.
Input:
A text is given in a line with following condition:
a. The number of letters in the text is less than or equal to 1000.
b. The number of letters in a word is less than or equal to 32.
c. There is only one word which is arise most frequently in given text.
d. There is only one word which has the maximum number of letters in given text. Sample Input:Thank you for your comment and your participation.
Sample Output:
your participation.
62. Write a PHP program which reads the two adjoined sides and the diagonal of a parallelogram and check whether the parallelogram is a rectangle or a rhombus.
According to Wikipedia-
parallelograms: In Euclidean geometry, a parallelogram is a simple (non-self-intersecting) quadrilateral with two pairs of parallel sides. The opposite or facing sides of a parallelogram are of equal length and the opposite angles of a parallelogram are of equal measure.
rectangles: In Euclidean plane geometry, a rectangle is a quadrilateral with four right angles. It can also be defined as an equiangular quadrilateral, since equiangular means that all of its angles are equal (360o/4 = 90o). It can also be defined as a parallelogram containing a right angle.
rhombus: In plane Euclidean geometry, a rhombus (plural rhombi or rhombuses) is a simple (non-self-intersecting) quadrilateral whose four sides all have the same length. Another name is equilateral quadrilateral, since equilateral means that all of its sides are equal in length. The rhombus is often called a diamond, after the diamonds suit in playing cards which resembles the projection of an octahedral diamond, or a lozenge, though the former sometimes refers specifically to a rhombus with a 60o angle (see Polyiamond), and the latter sometimes refers specifically to a rhombus with a 45o angle.
Input:Two adjoined sides and the diagonal.
1 = ai, bi, ci = 1000, ai + bi > ci
Sample Input:
4,5,6
6,6,9
Sample Output:
This is rectangles.
63. Write a PHP program to replace a string "Python" with "PHP" and "Python" with "PHP" in a given string.
Input:English letters (including single byte alphanumeric characters, blanks, symbols) are given on one line. The length of the input character string is 1000 or less.
Sample Input:
PHP is popular than Python
Sample Output:
Python is popular than PHP.
64. Write a PHP program to find the difference between the largest integer and the smallest integer which are created by 8 numbers from 0 to 9. The number that can be rearranged shall start with 0 as in 00135668.
Input: The difference between the largest integer and the smallest integer.
Sample Input:
1
34567829
Sample Output:
Difference between the largest integer and the smallest integer: 75308643
65. Write a PHP program to compute the sum of first n given prime numbers.
Input: n ( n = 10000). Input 0 to exit the program.
Sample Input:
25
0
Sample Output:
Sum of first 25 prime numbers:1060
66. Write a PHP program that accept an even number (n should be greater than or equal to 4 and less than or equal to 50000, Goldbach number) from the user and create a combinations that express the given number as a sum of two prime numbers. Print the number of combinations.
Goldbach number: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes.[4] Since four is the only even number greater than two that requires the even prime 2 in order to be written as the sum of two primes, another form of the statement of Goldbach's conjecture is that all even integers greater than 4 are Goldbach numbers.
The expression of a given even number as a sum of two primes is called a Goldbach partition of that number. The following are examples of Goldbach partitions for some even numbers:
6 = 3 + 3
8 = 3 + 5
10 = 3 + 7 = 5 + 5
12 = 7 + 5
...
100 = 3 + 97 = 11 + 89 = 17 + 83 = 29 + 71 = 41 + 59 = 47 + 53
Sample Input:
100
0
Sample Output:
Number of combinations: 6
67. if you draw a straight line on a plane, the plane is divided into two regions. For example, if you pull two straight lines in parallel, you get three areas, and if you draw vertically one to the other you get 4 areas. Write a PHP program to create maximum number of regions obtained by drawing n given straight lines.
Input: (1 = n = 10,000)
Sample Input:
5
Sample Output:
Maximum number of regions: 16
68. There are four different points on a plane, P(xp,yp), Q(xq, yq), R(xr, yr) and S(xs, ys). Write a PHP program to test whether AB and CD are orthogonal or not.
xp,yp, xq, yq, xr, yr, xs and ys are -100 to 100 respectively and each value can be up to 5 digits after the decimal point It is given as a real number including the number of.
Sample Input:
5
Sample Output:
Maximum number of regions: 16
69. Write a PHP program to sum of all numerical values (positive integers) embedded in a sentence.
Sentences with positive integers are given over multiple lines. Each line is a character string containing one-byte alphanumeric characters, symbols, spaces, or an empty line. However the input is 80 characters or less per line and the sum is 10,000 or less.
Sample Input:
5 apple and 10 orange are rotten in the basket
Sample Output:
Maximum number of regions: 16
70. Record section of ABC company records the customer number and the trading date for each month. Write a PHP program to find the customer number that has traded for the second consecutive for the second consecutive month from last month and the number of transactions.
The data of this month and the data of last month are separated by a blank line of one line and given.
Transaction format:
c1 , d1
c2 , d2
...
...
ci (1 = ci = 1,000) is an integer represents the customer number, di (1 = di = 31) is an integer represents the trading date.
Sample Input:
125,10
55,12
34,14
125,3
55,4
125,5
Sample Output:
Sum of the numeric values: 15
71. There are 10 vertical and horizontal squares on a plane. Each square is painted blue and green. Blue represents the sea, and green represents the land. When two green squares are in contact with the top and bottom, or right and left, they are said to be ground. The area created by only one green square is called "island". For example, there are five islands in the figure below.
Write a PHP program to read the mass data and find the number of islands.
A single data set is represented by 10 rows of 10 numbers representing green squares as 1 and blue squares as zeros.
Sample Input:
1100000111
1000000111
0000000111
0010001000
0000011100
0000111110
0001111111
1000111110
1100011100
1110001000
Sample Output:
Number of islands: 5
72. When character are consecutive in a string , it is possible to shorten the character string by replacing the character with a certain rule. For example, in the case of the character string YYYYY, if it is expressed as # 5 Y, it is compressed by one character.
Write a PHP program to restore the original string by entering the compressed string with this rule. However, the # character does not appear in the restored character string.
Note: The original sentences are uppercase letters, lowercase letters, numbers, symbols, less than 100 letters, and consecutive letters are not more than 9 letters.
Input:
Multiple character strings are given. One string is given per line
Sample Output:
88888888 + 1 = 100000000
73. A convex polygon is a simple polygon in which no line segment between two points on the boundary ever goes outside the polygon. Equivalently, it is a simple polygon whose interior is a convex set. In a convex polygon, all interior angles are less than or equal to 180 degrees, while in a strictly convex polygon all interior angles are strictly less than 180 degrees.
Write a PHP program that compute the area of the polygon . The vertices have the names vertex 1, vertex 2, vertex 3, ... vertex n according to the order of edge connections. However, n is 3 or more and 20 or less. You can also use the following formula to calculate the area S from the lengths a, b, and c of the triangle's three sides.
Input:
1.0, 0.0
0.0, 0.0
1.0, 1.0
2.0, 0.0
-1.0, 1.0
Sample Output:
Area of the polygon: 1.5
74. Internet search engine giant, such as Google accepts web pages around the world and classify them, creating a huge database. The search engines also analyze the search keywords entered by the user and create inquiries for database search. In both cases, complicated processing is carried out in order to realize efficient retrieval, but basics are all cutting out words from sentences.
Write a PHP program to cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters.
Input:
English sentences consisting of delimiters and alphanumeric characters are given on one line.
Sample Input:
The quick brown fox
Sample Output:
Original string: The quick brown fox Words of 3 to 6 characters length: The quick brown fox
75. Arrange integers (0 to 99) as narrow hilltop, as illustrated in Figure 1. Reading such data representing huge, when starting from the top and proceeding according to the next rule to the bottom. Write a PHP program that compute the maximum value of the sum of the passing integers.
Input:
A series of integers separated by commas are given in diamonds. No spaces are included in each line. The input example corresponds to Figure 1. The number of lines of data is less than 100 lines.
Sample Input:
8
4, 9
9, 2, 1
3, 8, 5, 5
5, 6, 3, 7, 6
3, 8, 5, 5
9, 2, 1
4, 9
8
Sample Output:
64
76. Your task is to develop a small part of spreadsheet software.
Write a PHP program which adds up columns and rows of given table as shown in the following figure:
Input:
n (the size of row and column of the given table)
1st row of the table
2nd row of the table
:
:
n th row of the table
The input ends with a line consisting of a single 0.
Sample Input:
4
25 69 51 26
68 35 29 54
54 57 45 63
61 68 47 59
0
Sample Output:
The table with sum of rows and columns: 25 69 51 26 171 68 35 29 54 186 54 57 45 63 219 61 68 47 59 235 208 229 172 202 811
77. Write a PHP program which reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers.
The number of pairs of a word and a page number is less than or equal to 1000. A word never appear in a page more than once. The words should be printed in alphabetical order and the page numbers should be printed in ascending order.
Sample Input:
apple 5
banana 6
Sample Output:
The word and a list of the corresponding page numbers: apple 5 banana 6
78. Write a PHP program to create a function that returns true for all elements of an array, false otherwise.
Sample Output:
1 0 0
79. Write a PHP program to deep flatten an given array.
Sample Output:
( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
80.Write a PHP program to create a new array with n elements removed from the left.
Array ( [0] => 2 [1] => 3 ) Array ( [0] => 3 [1] => 4 )
81. Write a PHP program to get the last element for which the given function returns a truth value.
Sample Output:
3 4
82. Write a PHP program to get the index of the last element for which the given function returns a truth value.
Sample Output:
2 3
83. Write a PHP program to group the elements of an array based on the given function.
Sample Output:
Array ( [3] => Array ( [0] => one [1] => two ) [5] => Array ( [0] => three ) [4] => Array ( [0] => four ) )
84. Write a PHP program to check a flat list for duplicate values. Returns true if duplicate values exists and false if values are all unique.
Sample Output:
1 0
85. Write a PHP program to get the head of a given list.Sample Output:
1 2
86. Write a PHP program to get the last element of a given list.
Sample Output:
3 2
87. Write a PHP program to retrieve all of the values for a given key.
Sample Output:
Array ( [0] => Computer [1] => Laptop )
88. Write a PHP program to mutate the original array to filter out the values specified.
Sample Output:
Array ( [0] => b [1] => b )
89. Write a PHP program to filter the collection using the given callback. Sample Output:
Array ( [0] => Red )
90. Write a PHP program to return all elements in a given array except for the first one.
Sample Output:
Array ( [0] => 2 [1] => 3 )
Coaches
Francis Korsah
Edward Yeboah