Module Week (1-5) Webapps
Module Week (1-5) Webapps
(WEBAPPS)
TOPICS: WEEK 1 – 2:
• Introduction of PHP
• PHP Install
• PHP Syntax
• PHP Comments
• PHP Variables
• PHP Echo/Print
WEEK 3:
• PHP Data Types
• PHP Strings
• PHP Numbers
• PHP Casting
• PHP Math
WEEK 4:
• PHP Constants
• PHP Magic Constants
• PHP Operators
• PHP If…Else…Elseif
• PHP Switch
WEEK 5:
• PHP Loops
• PHP Arrays
WEB APPLICATIONS DEVELOPMENT
WEEK 1 – 2:
INTRODUCTION TO PHP
What is PHP?
• PHP is an acronym for "PHP: Hypertext Preprocessor"
• PHP is a widely-used, open source scripting language
• PHP scripts are executed on the server
• PHP is free to download and use
PHP SYNTAX
• A PHP script can be placed anywhere in the document.
• A PHP script starts with “<?php” and ends with “?>”:
1|Page
WEB APPLICATIONS DEVELOPMENT
Example:
A simple .php file with both HTML code and PHP code:
PHP VARIABLES
• Variables are "containers" for storing information.
• In PHP, a variable starts with the “$” sign, followed by the name of the variable
• A variable can have a short name (like $x and $y) or a more descriptive name
($age, $carname, $total_volume).
2|Page
WEB APPLICATIONS DEVELOPMENT
Global - a variable declared outside a function has a GLOBAL SCOPE and can
only be accessed outside a function:
Local - a variable declared within a function has a LOCAL SCOPE and can
only be accessed within that function:
3|Page
WEB APPLICATIONS DEVELOPMENT
4|Page
WEB APPLICATIONS DEVELOPMENT
Display Text:
Display Variables:
5|Page
WEB APPLICATIONS DEVELOPMENT
WEEK 3:
PHP STRINGS
• A string is a sequence of characters
• Strings in PHP are surrounded by either double quotation marks, or single
quotation marks.
6|Page
WEB APPLICATIONS DEVELOPMENT
Word Count
• The PHP str_word_count() function counts the number of words in a string.
Modify Strings
• Uppercase and Lowercase
• The PHP str_replace() function replaces some characters with some other
characters in a string.
String Concatenation
• To concatenate, or combine, two strings you can use the “.” operator:
7|Page
WEB APPLICATIONS DEVELOPMENT
PHP CASTING
• Sometimes you need to change a variable from one data type into another,
and sometimes you want a variable to have a specific data type. This can be
done with casting.
• Casting in PHP is done with these statements:
➢ (string) - Converts to data type String
➢ (int) - Converts to data type Integer
➢ (float) - Converts to data type Float
➢ (bool) - Converts to data type Boolean
➢ (array) - Converts to data type Array
PHP MATH
• PHP has a set of math functions that allows you to perform mathematical tasks
on numbers.
• The pi() function returns the value of PI:
• The min() and max() functions can be used to find the lowest or highest value in
a list of arguments:
8|Page
WEB APPLICATIONS DEVELOPMENT
➢ To get more control over the random number, you can add the
optional min and max parameters to specify the lowest integer and the
highest integer to be returned.
9|Page
WEB APPLICATIONS DEVELOPMENT
WEEK 4
PHP CONSTANTS
• Constants are like variables, except that once they are defined, they cannot
be changed or undefined.
• A constant is an identifier (name) for a simple value. The value cannot be
changed during the script.
• A valid constant name starts with a letter or underscore (no $ sign before the
constant name).
• Note: Unlike variables, constants are automatically global across the entire
script.
• To create a constant, use the define() function.
Parameters:
➢ name: Specifies the name of the constant
➢ value: Specifies the value of the constant
➢ case-insensitive: Specifies whether the constant name should be
case-insensitive. Default is false.
Examples:
10 | P a g e
WEB APPLICATIONS DEVELOPMENT
PHP OPERATORS
• Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
➢ Arithmetic operators
➢ Assignment operators
➢ Comparison operators
➢ Increment/Decrement operators
➢ Logical operators
➢ String operators
➢ Array operators
➢ Conditional assignment operators
11 | P a g e
WEB APPLICATIONS DEVELOPMENT
• The PHP arithmetic operators are used with numeric values to perform common
arithmetical operations, such as addition, subtraction, multiplication etc.
• The basic assignment operator in PHP is "=". It means that the left operand gets
set to the value of the assignment expression on the right.
• The PHP comparison operators are used to compare two values (number or
string):
12 | P a g e
WEB APPLICATIONS DEVELOPMENT
13 | P a g e
WEB APPLICATIONS DEVELOPMENT
• The PHP conditional assignment operators are used to set a value depending
on conditions:
PHP IF…ELSE…ELSEIF
In PHP we have the following conditional statements:
• if statement - executes some code if one condition is true
14 | P a g e
WEB APPLICATIONS DEVELOPMENT
• if...else statement - executes some code if a condition is true and another code if
that condition is false
• if...elseif...else statement - executes different codes for more than two conditions
PHP SWITCH
• Use the switch statement to select one of many blocks of code to be executed.
This is how it works:
➢ The expression is evaluated once
➢ The value of the expression is compared with the values of each case
➢ If there is a match, the associated block of code is executed
➢ The break keyword breaks out of the switch block
➢ The default code block is executed if there is no match
Example:
15 | P a g e
WEB APPLICATIONS DEVELOPMENT
WEEK 5
PHP LOOPS
In PHP, we have the following loop types:
• while - loops through a block of code as long as the specified condition is true
Example:
Note: remember to increment $i, or else the loop will continue forever.
• do...while - loops through a block of code once, and then repeats the loop as long
as the specified condition is true
➢ The do...while loop will always execute the block of code at least once, it
will then check the condition, and repeat the loop while the specified
condition is true.
Examples:
Answer: 12345
Answer: 8
16 | P a g e
WEB APPLICATIONS DEVELOPMENT
Note: In a do...while loop the condition is tested AFTER executing the statements
within the loop. This means that the do...while loop will execute its statements at
least once, even if the condition is false. See example above.
Example:
Example Explained:
1. The first expression, $x = 0;, is evaluated once and sets a counter to 0.
2. The second expression, $x <= 10;, is evaluated before each iteration, and
the code block is only executed if this expression evaluates to true. In this
example the expression is true as long as $x is less than, or equal to, 10.
3. The third expression, $x++;, is evaluated after each iteration, and in this
example, the expression increases the value of $x by one at each iteration.
Example:
17 | P a g e
WEB APPLICATIONS DEVELOPMENT
• The array above is an indexed array, where the first item has the key 0, the second
has the key 1, and so on.
• Associative arrays are different, associative arrays use named keys that you
assign to them, and when looping through associative arrays, you might want to
keep the key as well as the value.
• This can be done by specifying both the key and value in the foreach definition,
like this:
Example:
PHP ARRAYS
What is an Array?
• An array is a special variable that can hold many values under a single name,
and you can access the values by referring to an index number or name.
Useful link:
• https://www.w3schools.com/php/default.asp
18 | P a g e