8/12/2019 Lecture - Introduction to PHP
1/38
Web Development: Dynamically
Generated Content
Introduction to PHP
8/12/2019 Lecture - Introduction to PHP
2/38
PHP Start and End Tags
You need to inform the PHP engine that you want toexecute PHP code
You can do this using special tags to mark start andend of PHP code
8/12/2019 Lecture - Introduction to PHP
3/38
PHP Start and End Tags
Tag Style Start Tag End Tag
Standard tags
Short tags
ASP tags
Script tags
Standard and the script tags cab beguaranteed to work on any configuration
8/12/2019 Lecture - Introduction to PHP
4/38
8/12/2019 Lecture - Introduction to PHP
5/38
The echoStatement and print() Function
The echostatement and the print() function areused to output data
Example
OR
8/12/2019 Lecture - Introduction to PHP
6/38
Combining HTML and PHP
A PHP script including HTML
8/12/2019 Lecture - Introduction to PHP
7/38
Adding Comments
Comments make it easier for other programmers to workwith your code
There two types of comments in PHP: Single-line comments which begin with // or #
//This is a single-line comment# This is also a single-line comment
Multi-line comments with /* and ending with */
/*This is a comment
and will be ignored by
PHP engine */
8/12/2019 Lecture - Introduction to PHP
8/38
PHP LANGUAGE STRUCTURE
8/12/2019 Lecture - Introduction to PHP
9/38
Variables
A variable consists of a name of your choosingpreceded by a $ sign
Variable names can include letters, numbers and theunderscore character but can not include spaces
A semi-colon is used to end a PHP statement$name_of_student;
$number123;
Variable names are case sensitive that means $sumand $Sum are treated as entirely different variablenames
8/12/2019 Lecture - Introduction to PHP
10/38
Variables
PHP is a loosely typed language This means that variable does not need to be
declared before adding a value to it.
PHP automatically converts the variable to the
correct data type, depending on its value
8/12/2019 Lecture - Introduction to PHP
11/38
8/12/2019 Lecture - Introduction to PHP
12/38
Globals and Superglobals
PHP has predefined variables called superglobals These variables are always present and their values
are available to all your scripts
8/12/2019 Lecture - Introduction to PHP
13/38
Examples of Superglobals
$_GETcontains any variables provided to a scriptthrough the GET method
$_POSTcontains any variables provided to ascript through the POST method
$_COOKIE contains any variables provided to ascript through a cookie
$_FILES contains any variables provided to a scriptthrough file uploads
8/12/2019 Lecture - Introduction to PHP
14/38
Examples of Superglobals
$_SERVER contains information such as headers,file paths, and script locations
$_ENV contains any variables provided to a script aspart of the server environment
$_REQUEST contains any variables provided to ascript through any user input mechanism
$_SESSION contains any variables that arecurrently registered in a session
8/12/2019 Lecture - Introduction to PHP
15/38
Working with Strings
A string is a collection of characters, numbers, lettersenclosed in quotes
For example Interactive Media
Dynamic Websites
To make a string assign it a variable name$name=Jimmy
To create a srting you can use single or double quotes
A string can be printed using echo() or print()echo $name
Or
echo HND Interactive Media
8/12/2019 Lecture - Introduction to PHP
16/38
Concatenating Strings
To append a string at the end of another you can usethe concatenation operator which is the period(.)
$first_name=Jack;
$last_name=Murray;$full_name= $first_name. $last_name;
8/12/2019 Lecture - Introduction to PHP
17/38
String Functions
PHP has several functions to manipulatetext(Strings)
You would usually pass a variable by putting it inparentheses
The functions can return a value in addition toperforming tasks
Functions do not start with a dollar sign and areimmediately followed by parentheses
8/12/2019 Lecture - Introduction to PHP
18/38
String Functions strlen()
The function strlen()returns the length of a string
8/12/2019 Lecture - Introduction to PHP
19/38
String Functions htmlspecialchars()
The function htmlspecialchars()takes a string andthen converts special characters like >, to HTMLcode
8/12/2019 Lecture - Introduction to PHP
20/38
String Functions ucfirst()
The function ucfirst()changes the first character touppercase
8/12/2019 Lecture - Introduction to PHP
21/38
String Functions ucwords()
The function ucwords()changes the first characterof each word to uppercase
8/12/2019 Lecture - Introduction to PHP
22/38
String Functions trim()
The function trim()removes blank space from thebeginning and end of string
8/12/2019 Lecture - Introduction to PHP
23/38
8/12/2019 Lecture - Introduction to PHP
24/38
String Functions strtoupper()
The function strtoupper()converts any lowercaseletters to uppercase
8/12/2019 Lecture - Introduction to PHP
25/38
PHP Data Types
Type Description
Boolean One of the values true or false
Integer A whole number
Float or Double A floating point number
String A collection of characters
Array An ordered set of valuesResource Reference to a third party resource e.g. database
NULL An uninitialized variable
8/12/2019 Lecture - Introduction to PHP
26/38
Operators and Expressions
Operators are symbols used to manipulate data A value that is operated on by operator is called
operand
A combination of operands and operators is called
expressions
8/12/2019 Lecture - Introduction to PHP
27/38
Assignment Operators
The assignment operator is =
$name=Dougie;
$sum=45;
8/12/2019 Lecture - Introduction to PHP
28/38
Working with Numbers
PHP has both integer and floating-point(decimal)number types
Number values are never enclosed within quotes
All numbers are presumed positive unless preceded
by a minus(-)
8/12/2019 Lecture - Introduction to PHP
29/38
Working with Numbers
Along with arithmetic operators you can use built-in
functions to manipulate numbers e.g. number_format() used to display numbers with specified
number of decimal places number_format()has the following syntax
number_format(variable, number of decimal places)
number_format($total, 3);
round() is used to decimal numbers to nearest whole number
round() has the following syntaxround(variable) e.g.
round($total);
8/12/2019 Lecture - Introduction to PHP
30/38
Arithmetic Operators
Operator Name
+ Addition
- Subtraction
/ Division
* Multiplication
% Modulus
8/12/2019 Lecture - Introduction to PHP
31/38
Combined Assignment Operators
Operator Example Equivalent to
+= $x+=5 $x=$x+5
-= $x-=5 $x=$x-5
/= $x/=5 $x=$x/5
*= $x*=5 $x=$x*5
%= $x%=5 $x=$x%5.= $x.= test $x=$x. test
8/12/2019 Lecture - Introduction to PHP
32/38
Automatically Incrementing and
Decrementing
A variable can be incremented by one as follows
$x=$x+1;
OR
$x+=1;
8/12/2019 Lecture - Introduction to PHP
33/38
Automatically Incrementing and
Decrementing
A variable can be decreased by 1 as follows:
$x=$x-1;
OR
$x-=1;
OR
$x--;
8/12/2019 Lecture - Introduction to PHP
34/38
Comparison Operators
Operator Name
== Equivalence
!= Non-equivalence
=== Identical
> Greater than
>= Greater than or equal to< Less than
8/12/2019 Lecture - Introduction to PHP
35/38
Constants
A constant is a value that does not change
A constant is defined using function define()
define (name_of_constant,41);
8/12/2019 Lecture - Introduction to PHP
36/38
8/12/2019 Lecture - Introduction to PHP
37/38
Single vs Double Quotes
In PHP values enclosed within single quotes will be
treated literally whereas those within double quotes willbe interpreted
Lets say you have
echo $test_var=Some Text;
echo var is equal to $test_var; will print:
var is equal to Some Text
Whereas echo var is equal to $var; will print
var is equal to $test_var
8/12/2019 Lecture - Introduction to PHP
38/38
Special Characters
The following characters have special meaning whenenclosed in double quotes
Code Meaning
\ Double quotes
\ Single quotes\\ Backslash
\n New line
\r Carriage return
\t Tab\$ Dollar sign