Upload
dhani-ahmad
View
71
Download
4
Embed Size (px)
Citation preview
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
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
Basic PHP SyntaxBasic PHP SyntaxMOHAMAD RAHIMI MOHAMAD ROSMAN
<?php
?>
<html><body><?phpecho "Hello World";?></body></html>
Below, is an example of a simple PHP script which sends the text "Hello World" to the browser:
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
Comments in PHPComments in PHPMOHAMAD 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 commentsIf 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:
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
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
ExampleExample
Above figure shows a block of PHP code embedded in an ordinary web page
MOHAMAD RAHIMI MOHAMAD ROSMAN
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
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
• 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)
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
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
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
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
Understanding when to use quotes
Numbers: No quotes◦ Eg: $StartYear=2007;
Text: Requires quotes◦ Eg: $Stu_Name=“Bill, Gray”;
MOHAMAD RAHIMI MOHAMAD ROSMAN
Strings in PHPStrings in PHP
A string variable is used to store and manipulate a piece of text
The Concatenation OperatorThere is only one string operator in PHPThe concatenation operator (.) is used to
put two string values together.To concatenate two variables together,
use the dot (.) operator
MOHAMAD RAHIMI MOHAMAD ROSMAN
ExampleExampleMOHAMAD RAHIMI MOHAMAD ROSMAN
<html><body>
<?php
$txt="Hello World";
echo $txt;
?>
</body></html>
ExampleExampleMOHAMAD 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?
ExampleExampleMOHAMAD RAHIMI MOHAMAD ROSMAN
<html><body>
<?php
$txt1=“1234";$txt2=“5678";$txt3=“9012";echo $txt2 . $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?
Strlen() functionsStrlen() functions
Using the strlen() functionThe 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
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
Arimethic OperatorArimethic OperatorMOHAMAD RAHIMI MOHAMAD ROSMAN
Operator Description Example Result
+ Addition x=2x+2
4
- Subtraction x=25-x
3
* Multiplication x=4x*5
20
/ Division 15/55/2
32.5
% Modulus (division remainder) 5%210%810%2
120
++ Increment x=5x++
x=6
-- Decrement x=5x--
x=4
Assignment OperatorAssignment OperatorMOHAMAD RAHIMI MOHAMAD ROSMAN
Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y
Comparison OperatorComparison OperatorMOHAMAD 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
Logical OperatorsLogical OperatorsMOHAMAD RAHIMI MOHAMAD ROSMAN
Operator Description Example
&& and x=6y=3 (x < 10 && y > 1) returns true
|| or x=6y=3 (x==5 || y==5) returns false
! not x=6y=3 !(x==y) returns true
SubstringSubstring
Finding the specific string in a wordssubstr(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
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
ExerciseExercise
Indicate true or false1. x=5, y=8, z= “Hello” Results
◦ x==y False◦ x!=y True◦ x==5&&y==9 False◦ (x+3)>=y&& (y-3)>=x True◦ z!=“hello” True◦ $my_string!=$My_String True◦ z==“Hello”||z==“hello” True
MOHAMAD RAHIMI MOHAMAD ROSMAN
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
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
ExerciseExercise
1. x=“Nazrul,Mohamad”1. First name = mohamad2. Last name = nazrul3. Change the order of the name, using the PHP
code. Display first name first, followed by last name.
MOHAMAD RAHIMI MOHAMAD ROSMAN
ExerciseExercise
1. x=“Siti,Nurman,Fatimah”1. Variable x represents three (3) students of
IS110.2. Write a program to retrieve the name of the
last students, which is “Fatimah”.
MOHAMAD RAHIMI MOHAMAD ROSMAN
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