Lab 11 - en
Lab 11 - en
Programming
Mitrea Dan Alexandru
[email protected]
PHP
is an acronym for "PHP: Hypertext Preprocessor"
Example
PHP Variables
A PHP variable starts with the $ sign, followed by the name of the variable
PHP variable names are case-sensitive
PHP variables don’t have data type, they are automatically converted to the correct data type.
Example
PHP Functions
A function is a block of statements that can be used repeatedly in a program.
A function will not execute immediately when a page loads.
A function will be executed by a call to the function.
A user defined function declaration starts with the word "function":
PHP Strings
A string is a sequence of characters
(Study string.php)
PHP Arrays
An array is a special variable, which can hold more than one value at a time.
The foreach loop works only on arrays, and is used to loop through each key/value pair in
an array.
function sumDigits($n) {
...........
}
echo sumDigits(123);
echo sumDigits(12301);
echo sumDigits(1010);
Output:
6
7
2
Example 1
Example 2
deleteElement($arrTest, 4);
deleteElement($arrTest, 9);
deleteElement($arrTest, 7);
Output:
[5, 7, 1, 8]
[5, 7, 1, 4, 8]
[5, 1, 4, 8]
Example 2
Example 3
Create a function that calculate factorial of a number. Use the recursive method.
function factorial($n) {
...........
}
factorial(4);
factorial(5);
factorial(6);
Output:
24
120
720
Example 3
Example 4
function reverses_case_letters($text) {
...........
}
reverses_case_letters("AvBsfGge");
reverses_case_letters("JavaScript");
reverses_case_letters("PHP");
Output:
aVbSFgGE
jAVAsCRIPT
php
Example 4
Example 5
function displayTable($arr) {
...........
}
$arr = [
['Name', 'Age'], ['John', '21'], ['Jane', '19'], ['Tom', '11‘]
];
echo displayTable($arr);
Output:
Example 5
Example 6
You have the following archive “example6.zip” on the website lab. The archive
contains the website from lab 3. First, lets change the name of the files from html
to php. After that, create a folder inc and create two files header.php and
footer.php. We want to use header.php and footer.php. Let’s figure how.
Example 6
On each page of the website (index, about, contact, gallery), replace the copied
code with the include function.
The copied html code must be deleted.
The header is replaced at the beginning of the site.
Example 6
On each page of the website (index, about, contact, gallery), replace the copied
code with the include function.
The copied html code must be deleted.
The footer is replaced at the end of the site.