Professional Documents
Culture Documents
Introduction To PHP
Introduction To PHP
LECTURE
1
Introduction to PHP
Setting a PHP Environment
2
What is PHP?
▪ Acronym for PHP:Hypertext Preprocessor.
▪ A server-side programming language – it runs on a web
server.
▪ A widely-used open source general-purpose scripting
language that is especially suited for web development and
can be embedded into HTML.
3
What PHP can do?
Can generate dynamic page content
Can create, open, read, write, delete, and close files on the server
4
How PHP scripts run on the server?
5
How PHP scripts run on the server?(cont.)
6
How to code in PHP?
▪ Download XAMPP or
WAMP (your call) installer.
▪ Run XAMPP or WAMP
control panel and start
Apache and MySQL.
7
How to code in PHP?(cont.)
▪ Open htdocs folder inside the
XAMPP or WAMP folder.
▪ Create a folder that will
contain your website.
▪ Save your PHP files and
other components.
8
How to code in PHP?(cont.)
▪ For the text editor you may opt to use notepad++, sublime, or
an IDE Visual Studio.
9
How to code in PHP?(cont.)
Visual Studio
Sublime
10
Viewing
▪ Note! Make sure that the Apache and MySQL is running on
XAMPP or WAMP
▪ Type the following URL in the address bar of your web
browser:
▫ localhost/folder_name
▫ 127.0.0.1/folder_name
11
Tips
localhost/folder_name localhost/folder_name/file.php
Without specifying the file to If you want to access non-index
open index.php is the default filenames, use this URL to access
webpage that will be displayed. that php file you want to be
displayed.
12
2
Basics of PHP
PHP Syntax and Tags
13
PHP opening and closing tags syntax
▪ There are four different pairs of opening and closing tags
which can be used in PHP. Here is the list of tags.
▫ Default syntax
▫ Short open tags
▫ Omit the PHP closing tag at the end of the file.
14
Default Syntax
▪ The default syntax starts with <?php and ends with ?>
▪ Example
15
Short open tags
▪ The short tags starts with <? And ends with ?>
▪ Short style tags are only available when they are enabled in
php.ini configuration on file servers.
▪ Example
16
Omit the PHP closing tag
▪ It is recommended that a closing PHP tag shall be omitted in a
file containing only PHP code.
▪ So that occurrences of accidental whitespace or new lines
being added after the PHP closing tag.
▪ Example
17
PHP echo and print statements
▪ echo – can output one or more Strings
▪ print – can only output one String
▪ Echo without parentheses can take multiple parameters, which
get concatenated
▫ echo “and a “, 1, 2, 3; //without ()
▫ echo (“and a 123”); //with ()
▪ print can only take one parameter
▫ print “and a 123”; //without ()
▫ print (and a 123); //with () 18
PHP Statement separation
▪ In PHP, statements are terminated by a semicolon (;) like C or
Perl.
▪ The closing tag of a block of PHP code automatically implies
a semicolon, there is no need to have a semicolon terminating
the last line of a PHP block.
▪ Rules for statement separation
▫ Semicolon
▫ AND/OR
▫ A closing PHP tag
19
PHP Statement separation cont.
This is valid
20
PHP case sensitivity
▪ In PHP the user defined functions, classes, core language
keywords (e.g. if, else, while, echo etc.) are
case-insensitive.
▪ Note!
▫ In terms of VARIABLES it is case-sensitive
21
PHP case sensitivity cont.
▪ The three echo statements in the following example are equal.
22
PHP comments
▪ A comment in PHP code is a line that is not executed as a part
of the program. Its only purpose is to be read by someone who
is looking at the code.
▪ Comments can be used to:
▫ Let others understand your code
▫ Remind yourself of what you did
▪ PHP supports several ways of commenting
23
PHP comments cont.
Multiple-line
comment
Integrating
comment in the
code
24
3
Variables
PHP Variables
25
PHP Variables
▪ A variable is a container that you can store data in, and every
variable has a unique name.
▪ PHP variable names must begin with a dollar $ sign, and
cannot contain spaces.
▪ Variables in PHP don’t need data type.
▪ Create once a value is assigned.
26
Creating (Declaring) PHP Variables
▪ In PHP, a variable starts with the $ sign, followed by the name
of the variable.
▪ Example
In the example we have declared 3 variables.
$txt contains a String
$x contains an integer
$y contains a floating point
Tip! When you assign a text value to a
variable, put quotes (“ “) or (‘ ‘) around the
value.
27
PHP Variables are case-sensitive
29
Naming conventions
$email $how_long
30
4
Conditional Statements
If…Else…Elseif
31
PHP Conditional Statements
▪ Conditional statements are used to perform different actions
based on different conditions
▪ Conditional statements in PHP:
▫ if statement
▫ if…else statement
▫ if…elseif…else statement
▫ switch statement
32
PHP Conditional Statements (if)
▪ The if statement executes some code if one condition is true.
▪ Syntax
▪ Example
33
PHP Conditional Statements (if…else)
▪ The if...else statement executes some code if a condition is
true and another code if that condition is false.
▪ Syntax
34
PHP Conditional Statements (if…else)
▪ Example
35
PHP Conditional Statements (if…elseif…else)
▪ Example
37
PHP Conditional Statements (switch)
▪ The switch statement is used to perform different actions
based on different conditions.
▪ Syntax
38
PHP Conditional Statements (switch)
▪ Example
39
5
Loops
For, While, Do While
Foreach, Break/Continue
40
PHP Loops
▪ Loops are used to execute the same block of code again and
again, as long as a certain condition is true.
▪ Loops in PHP:
▫ for
▫ while
▫ do…while
▫ foreach
41
PHP Loops (for loop)
▪ Loops through a block of code a specified number of times.
▪ Syntax
▪ Parameters
▫ init counter: Initialize the loop counter value
▫ test counter: Evaluated for each loop iteration. If it evaluates to TRUE,
the loop continues. If it evaluates to FALSE, the loop ends.
▫ increment counter: Increases the loop counter value
42
PHP Loops (for loop)
▪ Example
▫ Count from 0 to 100 by tens:
43
PHP Loops (while loop)
▪ Loops through a block of code as long as the specified
condition is true.
▪ Syntax
44
PHP Loops (while loop)
▪ Example
▫ Print the value of x from 1 to 5 and hello world 5 times
45
PHP Loops (do…while loop)
▪ Loops through a block of code once, and then repeats the loop
as long as the specified condition is true.
▪ This loop will always execute the block of code
once, it will then check the condition, and repeat the loop
while the specified condition is true.
▪ Syntax
46
PHP Loops (do…while loop)
▪ Example
47
PHP Loops (foreach loop)
▪ Loops through a block of code for each element in an array.
▪ For every loop iteration, the value of the current array element
is assigned to $value and the array pointer is moved by one,
until it reaches the last array element.
▪ Syntax
48
PHP Loops (foreach loop)
▪ Example
▫ Display the values of the given array
49
References
▪ W3Schools (2022). PHP Tutorial.
https://www.w3schools.com/php/default.asp
▪ w3resource (2020, February 26). PHP Tutorials for beginners.
https://www.w3resource.com/php/php-home.php
50
End
Any questions?
Email me at amir.ledesma@clsu2.edu.ph
51