0% found this document useful (0 votes)
2 views24 pages

Lab 11 - en

The document provides an introduction to PHP, covering its file structure, capabilities, and syntax. It explains the differences between server-side and client-side scripting, as well as key concepts such as variables, functions, strings, and arrays in PHP. Additionally, it includes examples of PHP functions and practical applications for web programming.

Uploaded by

dondusca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views24 pages

Lab 11 - en

The document provides an introduction to PHP, covering its file structure, capabilities, and syntax. It explains the differences between server-side and client-side scripting, as well as key concepts such as variables, functions, strings, and arrays in PHP. Additionally, it includes examples of PHP functions and practical applications for web programming.

Uploaded by

dondusca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Introduction to Web

Programming
Mitrea Dan Alexandru
[email protected]
PHP
is an acronym for "PHP: Hypertext Preprocessor"

What is a PHP File ?


• PHP files can contain text, HTML, CSS, JavaScript, and PHP code
• PHP code are executed on the server, and the result is returned to the browser as plain HTML
• PHP files have extension ".php"

What Can PHP Do ?


• PHP can generate dynamic page content
• PHP can create, open, read, write, delete, and close files on the server
• PHP can collect form data
• PHP can send and receive cookies
• PHP can add, delete, modify data in your database
• PHP can be used to control user-access
• PHP can encrypt data
Server side vs. client side
• JavaScript = client-side scripting
– script is executed by the browser
– changes the existing html page

• PHP = server-side scripting


– script is executed by the web server
– used to generate the html page that is sent to the
browser
PHP Syntax
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>

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

Most common used String functions

strlen — Get string length


str_word_count — Return information about words used in a string
strrev — Reverse a string
str_replace — Replace all occurrences of the search string with the replacement string
strtoupper — Make a string uppercase
strtolower — Make a string lowercase
substr — Return part of a string
ucfirst — Make a string's first character uppercase
ucwords — Uppercase the first character of each word in a string

(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.

Most common used Array functions (Study array.php)


array_pop — Pop the element off the end of array
array_shift — Shift an element off the beginning of array
array_push — Push one or more elements onto the end of array
array_unshift — Prepend one or more elements to the beginning of an array
array_rand — Pick one or more random entries out of an array
count — Count all elements in an array, or something in an object
shuffle — Shuffle an array
sort — Sort an array
rsort — Sort an array in reverse order
implode — Join array elements with a string (string function)
explode — Split a string by string, returns an array of strings (string function)
include/require
• include – includes a file and evaluates it
– the file included inherits the variable
scope of the line where the include occurs
– can include files from the web or from
the local filesystem
• require – behaves as include. If the file is not found the
execution of the script is halted
• include_once / require_once – behave just like include
but don’t allow a specific file to be included more than
once
Example 1

Calculate the sum of the digits of a given number.

function sumDigits($n) {
...........
}

echo sumDigits(123);
echo sumDigits(12301);
echo sumDigits(1010);

Output:
6
7
2
Example 1
Example 2

Delete an element from an array.

function deleteElement($arr, $element) {


...........
}

$arrTest = [5, 7, 1, 4, 8];

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

Create a function that reverses the uppercase letters in lowercase letter.

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

Display a table. The content of the table is stored into an array.

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

Edit header.php from the 'inc' folder.


This section is repeated on every page, copy the html code from any of the site's
pages.
Example 6

Edit footer.php from the 'inc' folder.


This section is repeated on every page, copy the html code from any of the site's
pages.
We displayed the year, notice how we changed it in one place, and it appears on
all pages 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 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.

You might also like