INTECH 2201
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
Can collect form data
Can send and receive cookies
Can add, delete, modify data in database
Can restrict users to access some pages on your
website
Can encrypt data
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 open
index.php is the
default webpage that
will be displayed.
If you want to access
non-index filenames,
use this URL to access
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
three
different
pairs
of
opening and closing tags 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 start with <? and ends
with ?>
▪ Short style tags are only available when
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
▪ In the first example, both semicolon(;)
and a closing PHP tag are present.
This is valid
▪ In the second example, there is no
semicolon(;) after the last instruction
but a closing PHP tag is present.
20
PHP case sensitivity
▪ In
PHP
the
user
defined
functions,
classes, core language keywords (e.g. if,
else,
while,
echo
etc.)
are
caseinsensitive.
▪ Note!
▫ In terms of VARIABLES it is casesensitive
21
PHP case sensitivity cont.
▪ The
three
echo
statements
following example are equal.
in
the
Output of the code
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.
Single line comment
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
sign, followed by
variable.
▪ Example
starts with the $
the name of the
In
the
example,
we
declared three variables.
have
$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
There is no output for the variables $TXT and $tXt
28
PHP Variables naming conventions
▪ The first character must be a dollar sign
▪ A variable name must start with a letter
or the underscore character
▪ Cannot begin with numbers.
▪ Case sensitive
▪ A variable name can only contain alphanumeric characters and underscores (A-z,
0-9, and _ )
29
Naming conventions
$email
$fang-spot
$how_long
Valid or Invalid?
$10apples
$is Choice
alien desc
30
4
Conditional Statements
If…Else…Elseif
31
PHP Conditional Statements
▪ Conditional
statements
are
perform
different
actions
different conditions
▪ Conditional statements in PHP:
▫ if statement
▫ if…else statement
▫ if…elseif…else statement
▫ switch statement
used
based
to
on
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)
▪ The if...elseif...else statement executes
different
codes
for
more
than
two
conditions.
▪ Syntax
▪ Note: You can add many else if (condition)
36
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/phphome.php
50
End
Any questions?
Email me at
amir.ledesma@clsu2.edu.ph
51