2. IntroductionIntroduction
When you load a PHP page into a browser,
it looks no different from an ordinary
webpage. But before it reaches your
browser, quite a lot goes on behind the
scenes to generate the page’s dynamic
content.
In most cases, this frenetic activity takes
only a few microseconds, so you rarely
notice any delay.
At first glance, PHP code can look quite
intimidating, but once you understand the
basics, you’ll discover that the structure is
remarkably simple.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
3. If you have worked with any other computer
language, such as JavaScript, ActionScript, or
ASP, you’ll find they have a lot in common.
Every PHP page must have the following:
◦ The correct filename extension, usually .php
◦ Opening and closing PHP tags surrounding each block of
PHP code
◦ A typical PHP page will use some or all of the following
elements:
Variables to act as placeholders for unknown or changing
values
Arrays to hold multiple values
Conditional statements to make decisions
Loops to perform repetitive tasks
Functions to perform preset tasks
MOHAMAD RAHIMI MOHAMAD
ROSMAN
4. Basic PHP SyntaxBasic PHP Syntax
MOHAMAD RAHIMI MOHAMAD
ROSMAN
<?php
?>
<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Below, is an example of a simple PHP script which sends the text
"Hello World" to the browser:
5. Comments in PHPComments in PHP
PHP treats everything between the opening
and closing PHP tags as statements to be
executed, unless you tell it not to do so by
marking a section of code as a comment.
The following three reasons explain why you
may want to do this:
◦ To insert a reminder of what the script does
◦ To insert a placeholder for code to be added later
◦ To disable a section of code temporarily
MOHAMAD RAHIMI MOHAMAD
ROSMAN
6. Comments in PHPComments in PHP
MOHAMAD RAHIMI MOHAMAD
ROSMAN
Single-line comments
• The most common method of adding a single-line comment is to precede it with
two forward slashes, like this:
Multiline comments
If you want a comment to stretch over several lines, you can use the same style of
comments as in Cascading Style Sheets (CSS). Anything between /* and */ is
treated as a comment, no matter how many lines are used, like this:
7. Telling the server to
process PHP
PHP is a server-side language. This means that the web server
processes your PHP code and sends only the results—usually as
XHTML—to the browser. Because all the action is on the server,
you need to tell it that your pages contain PHP code.
This involves two simple steps, namely:
◦ Give every page a PHP filename extension—the default is .php. Do not
use anything other than .php unless you are told to specifically by your
hosting company.
◦ Enclose all PHP code within PHP tags.
The opening tag is <?php and the closing tag is ?>.
It’s a good idea to put the opening and closing tags on separate
lines for the sake of clarity.
<?php
// some PHP code
?>
You may come across <? as an alternative short version of the
opening tag.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
8. Embedding PHP in a web page
PHP is an embedded language. This means that you
can insert blocks of PHP code inside ordinary web
pages. When somebody visits your site and requests
a PHP page, the server sends it to the PHP engine,
which reads the page from top to bottom looking for
PHP tags.
XHTML passes through untouched, but whenever the
PHP engine encounters a <?php tag, it starts
processing your code and continues until it reaches
the closing ?> tag.
If the PHP code produces any output, it’s inserted at
that point. Then any remaining XHTML passes
through until another <?php tag is encountered.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
9. ExampleExample
Above figure shows a block of PHP code embedded in an
ordinary web page
MOHAMAD RAHIMI MOHAMAD
ROSMAN
10. VariablesVariables
What is variables?
◦ Variables are used for storing values, such as
numbers, strings or function results, so that
they can be used many times in a script.
All variables in PHP start with a $ (dollar)
sign symbol.
Variables may contain:
◦ Strings
◦ Numbers
◦ arrays
MOHAMAD RAHIMI MOHAMAD
ROSMAN
11. Naming VariablesNaming Variables
A variable name must start with a letter or an underscore
"_"
A variable name can only contain alpha-numeric characters
and underscores (a-Z, 0-9, and _ )
A variable name should not contain spaces. If a variable
name should be more than one word, it should be
separated with underscore ($my_string), or with
capitalization ($myString)
Variables always begin with a dollar sign ($).
The first character after the dollar sign cannot be a
number.
No spaces or punctuation are allowed, except for the
underscore (_).
Variable names are case-sensitive: $startYear and
$startyear are not the same.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
12. • Dim Balance
• $_balance
• &_balance_due
• $_balance_due
• &_balance
• $3_string
• $35string
• $Balance5
• &_5Balance
• $_Balance5
• $_POST
• $_GET
MOHAMAD RAHIMI MOHAMAD ROSMAN
Which of the following variables is correct? Indicate T (true)
or F (false)
13. Assigning values to variables
Variables get their values from a variety
of sources, including the following:
◦ User input through online forms
◦ A database
◦ An external source, such as a news feed or XML
file
◦ The result of a calculation
◦ Direct inclusion in the PHP code
Wherever the value comes from, it’s
always assigned in the same way with an
equal sign (=), like this:
◦ $variable = value;
MOHAMAD RAHIMI MOHAMAD
ROSMAN
14. Assignment OperatorAssignment Operator
The variable goes on the left of the equal
sign, and the value goes on the right.
Because it assigns a value, the equal sign
is called the assignment operator.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
15. Ending commands with a
semicolon
PHP is written as a series of commands or
statements. Each statement normally
tells the PHP engine to perform a
particular action, and it must always be
followed by a semicolon, like this:
MOHAMAD RAHIMI MOHAMAD
ROSMAN
16. Missing SemicolonMissing Semicolon
You can omit the semicolon if there’s only
one statement in the code block.
However, don’t do it. Get into the habit of
always using a semicolon at the end of
every PHP statement.
PHP is not like JavaScript or ActionScript.
It won’t automatically assume there
should be a semicolon at the end of a line
if you omit it.
Caution: A missing semicolon will
bring your page to a grinding halt.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
17. Understanding when to use
quotes
Numbers: No quotes
◦ Eg: $StartYear=2007;
Text: Requires quotes
◦ Eg: $Stu_Name=“Bill, Gray”;
MOHAMAD RAHIMI MOHAMAD
ROSMAN
18. Strings in PHPStrings in PHP
A string variable is used to store and
manipulate a piece of text
The Concatenation Operator
There is only one string operator in PHP
The concatenation operator (.) is used to
put two string values together.
To concatenate two variables together,
use the dot (.) operator
MOHAMAD RAHIMI MOHAMAD
ROSMAN
20. ExampleExample
MOHAMAD RAHIMI MOHAMAD
ROSMAN
<html><body>
<?php
$txt1="Hello World";
$txt2="1234";
$txt3="4321";
echo $txt1 . " " . $txt3 . " " . $txt2 ;
?>
</body></html>
To concatenate two or more variables together, use the dot (.) operator:To concatenate two or more variables together, use the dot (.) operator:
What is the output of the above script?What is the output of the above script?
22. Strlen() functionsStrlen() functions
Using the strlen() function
The strlen() function is used to find the
length of a string
<?php
echo strlen("Hello world!");
?>
The output of the code above will be 12
MOHAMAD RAHIMI MOHAMAD
ROSMAN
H e l l o w o r l d !
1 2 3 4 5 6 7 8 9 10 11 12
23. strpos() functionstrpos() function
Using the strpos() function
The strpos() function is used to search for a string or
character within a string.
If a match is found in the string, this function will return
the position of the first match.
If no match is found, it will return FALSE.
<?php
echo strpos("Hello world!","world");
?>
*.start with 0
MOHAMAD RAHIMI MOHAMAD
ROSMAN
H e l l o w o r l d !
0 1 2 3 4 5 6 7 8 9 10 11
27. Comparison OperatorComparison Operator
MOHAMAD RAHIMI MOHAMAD
ROSMAN
Operator Description Example
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true
28. Logical OperatorsLogical Operators
MOHAMAD RAHIMI MOHAMAD
ROSMAN
Operator Description Example
&& and x=6
y=3
(x < 10 && y >
1) returns true
|| or x=6
y=3
(x==5 ||
y==5) returns
false
! not x=6
y=3
!(x==y) returns
true
29. SubstringSubstring
Finding the specific string in a words
substr(a,b,c);
◦ a: statement
◦ b: starting point
◦ c: Number of character
Example
◦ Substr(“Fakulti IS”,8,2)
◦ What’s the output for it?
* . The index start with 0.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
F a k u l t i I S
0 1 2 3 4 5 6 7 8 9
30. ExerciseExercise
Provide answer for the following question.
1.X=6, Y=10 Results
◦ x++ 7
◦ x+=y 16
◦ y-- 9
◦ y%x 4
◦ ((x--)+y)/ 5 3
◦ (x*2)/12 1
MOHAMAD RAHIMI MOHAMAD
ROSMAN
32. Nurfatihah, Naimah, Aminah & Sairul has
participated in the Mathematical
competition. Out of 100 marks, Nurfatihah
score 97, Naimah score 78, Aminah score
85, while Sairul score 82. Your task is:
◦ Create 4 variable to store each candidate score
◦ Display the mark for the following
Nurfatihah + Naimah – Aminah + Sairul
Naimah – Sairul
Sairul + Aminah
◦ Display average marks for all candidate
MOHAMAD RAHIMI MOHAMAD
ROSMAN
33. ExerciseExercise
1. x=“Nur” , y=“Fatihah”
Write a PHP code using the above variable
and answer the following questions.
Length of x
Length of y
Length of x+y
Combine the two variables together (xy) and
display it and then find the positon of i
MOHAMAD RAHIMI MOHAMAD
ROSMAN
34. ExerciseExercise
1. x=“Nazrul,Mohamad”
1. First name = mohamad
2. Last name = nazrul
3. Change the order of the name, using the PHP
code. Display first name first, followed by last
name.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
36. ExerciseExercise
Create a script that request initial loan,
interest, payback period, and display
the data in the listbox alongside expected
monthly payment.
MOHAMAD RAHIMI MOHAMAD
ROSMAN