SlideShare una empresa de Scribd logo
1 de 37
PHP Basic SyntaxPHP Basic Syntax
Chapter 2
MOHAMAD RAHIMI MOHAMAD
ROSMAN
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 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:
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 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:
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 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
ExampleExample
MOHAMAD RAHIMI MOHAMAD
ROSMAN
<html>
<body>
<?php
$txt="Hello World";
echo $txt;
?>
</body>
</html>
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?
ExampleExample
MOHAMAD 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() 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
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
PHP OperatorsPHP Operators
Chapter 2
MOHAMAD RAHIMI MOHAMAD
ROSMAN
Arimethic OperatorArimethic Operator
MOHAMAD RAHIMI MOHAMAD
ROSMAN
Operator Description Example Result
+ Addition x=2
x+2
4
- Subtraction x=2
5-x
3
* Multiplication x=4
x*5
20
/ Division 15/5
5/2
3
2.5
% Modulus (division remainder) 5%2
10%8
10%2
1
2
0
++ Increment x=5
x++
x=6
-- Decrement x=5
x--
x=4
Assignment OperatorAssignment Operator
MOHAMAD 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 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
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
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
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 false
1. 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 = 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
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
~…..The end…..~~…..The end…..~
MOHAMAD RAHIMI MOHAMAD
ROSMAN

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
PHP
PHPPHP
PHP
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHP
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Files in php
Files in phpFiles in php
Files in php
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Javascript
JavascriptJavascript
Javascript
 
PHP - Introduction to PHP Forms
PHP - Introduction to PHP FormsPHP - Introduction to PHP Forms
PHP - Introduction to PHP Forms
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 

Destacado

Destacado (18)

Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
Php i basic chapter 3 (mardhiah kamaludin's conflicted copy 2013-04-23)
 
PHP Tutorials
PHP TutorialsPHP Tutorials
PHP Tutorials
 
PHP for HTML Gurus - J and Beyond 2012
PHP for HTML Gurus - J and Beyond 2012PHP for HTML Gurus - J and Beyond 2012
PHP for HTML Gurus - J and Beyond 2012
 
Drupal7 an introduction by ayushiinfotech
Drupal7 an introduction by ayushiinfotechDrupal7 an introduction by ayushiinfotech
Drupal7 an introduction by ayushiinfotech
 
PHP - Introduction to String Handling
PHP -  Introduction to  String Handling PHP -  Introduction to  String Handling
PHP - Introduction to String Handling
 
Surprise! It's PHP :) (unabridged)
Surprise! It's PHP :) (unabridged)Surprise! It's PHP :) (unabridged)
Surprise! It's PHP :) (unabridged)
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit university
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Memphis php html form processing with php
Memphis php   html form processing with phpMemphis php   html form processing with php
Memphis php html form processing with php
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
PHP
PHPPHP
PHP
 
Arrays &amp; functions in php
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in php
 
Php string function
Php string function Php string function
Php string function
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
Php string
Php stringPhp string
Php string
 
Cookie and session
Cookie and sessionCookie and session
Cookie and session
 

Similar a Chapter 02 php basic syntax

Chap 4 PHP.pdf
Chap 4 PHP.pdfChap 4 PHP.pdf
Chap 4 PHP.pdfHASENSEID
 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_masterjeeva indra
 
Chapter 07 php forms handling
Chapter 07   php forms handlingChapter 07   php forms handling
Chapter 07 php forms handlingDhani Ahmad
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)Arjun Shanka
 
PHP Basics Ebook
PHP Basics EbookPHP Basics Ebook
PHP Basics EbookSwanand Pol
 
Introduction to PHP.ppt
Introduction to PHP.pptIntroduction to PHP.ppt
Introduction to PHP.pptSanthiNivas
 
Php tutorial from_beginner_to_master
Php tutorial from_beginner_to_masterPhp tutorial from_beginner_to_master
Php tutorial from_beginner_to_masterPrinceGuru MS
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Muhamad Al Imran
 
Chapter 05 looping
Chapter 05   loopingChapter 05   looping
Chapter 05 loopingDhani Ahmad
 

Similar a Chapter 02 php basic syntax (20)

Chap 4 PHP.pdf
Chap 4 PHP.pdfChap 4 PHP.pdf
Chap 4 PHP.pdf
 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master
 
Chapter 07 php forms handling
Chapter 07   php forms handlingChapter 07   php forms handling
Chapter 07 php forms handling
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Php tutorialw3schools
Php tutorialw3schoolsPhp tutorialw3schools
Php tutorialw3schools
 
php 1
php 1php 1
php 1
 
PHP Basics Ebook
PHP Basics EbookPHP Basics Ebook
PHP Basics Ebook
 
phptutorial
phptutorialphptutorial
phptutorial
 
phptutorial
phptutorialphptutorial
phptutorial
 
PHP
 PHP PHP
PHP
 
Php
PhpPhp
Php
 
Introduction to PHP.ppt
Introduction to PHP.pptIntroduction to PHP.ppt
Introduction to PHP.ppt
 
Php tutorial from_beginner_to_master
Php tutorial from_beginner_to_masterPhp tutorial from_beginner_to_master
Php tutorial from_beginner_to_master
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Basic php
Basic phpBasic php
Basic php
 
Php basics
Php basicsPhp basics
Php basics
 
Chapter 05 looping
Chapter 05   loopingChapter 05   looping
Chapter 05 looping
 

Más de Dhani Ahmad

Strategic planning
Strategic planningStrategic planning
Strategic planningDhani Ahmad
 
Strategic information system planning
Strategic information system planningStrategic information system planning
Strategic information system planningDhani Ahmad
 
Opportunities, threats, industry competition, and competitor analysis
Opportunities, threats, industry competition, and competitor analysisOpportunities, threats, industry competition, and competitor analysis
Opportunities, threats, industry competition, and competitor analysisDhani Ahmad
 
Information system
Information systemInformation system
Information systemDhani Ahmad
 
Information resource management
Information resource managementInformation resource management
Information resource managementDhani Ahmad
 
Types of islamic institutions and records
Types of islamic institutions and recordsTypes of islamic institutions and records
Types of islamic institutions and recordsDhani Ahmad
 
Islamic information seeking behavior
Islamic information seeking behaviorIslamic information seeking behavior
Islamic information seeking behaviorDhani Ahmad
 
Islamic information management
Islamic information managementIslamic information management
Islamic information managementDhani Ahmad
 
Islamic information management sources in islam
Islamic information management sources in islamIslamic information management sources in islam
Islamic information management sources in islamDhani Ahmad
 
The need for security
The need for securityThe need for security
The need for securityDhani Ahmad
 
The information security audit
The information security auditThe information security audit
The information security auditDhani Ahmad
 
Security technologies
Security technologiesSecurity technologies
Security technologiesDhani Ahmad
 
Security and personnel
Security and personnelSecurity and personnel
Security and personnelDhani Ahmad
 
Risk management ii
Risk management iiRisk management ii
Risk management iiDhani Ahmad
 
Risk management i
Risk management iRisk management i
Risk management iDhani Ahmad
 
Privacy & security in heath care it
Privacy & security in heath care itPrivacy & security in heath care it
Privacy & security in heath care itDhani Ahmad
 
Physical security
Physical securityPhysical security
Physical securityDhani Ahmad
 
Legal, ethical & professional issues
Legal, ethical & professional issuesLegal, ethical & professional issues
Legal, ethical & professional issuesDhani Ahmad
 

Más de Dhani Ahmad (20)

Strategic planning
Strategic planningStrategic planning
Strategic planning
 
Strategic information system planning
Strategic information system planningStrategic information system planning
Strategic information system planning
 
Opportunities, threats, industry competition, and competitor analysis
Opportunities, threats, industry competition, and competitor analysisOpportunities, threats, industry competition, and competitor analysis
Opportunities, threats, industry competition, and competitor analysis
 
Information system
Information systemInformation system
Information system
 
Information resource management
Information resource managementInformation resource management
Information resource management
 
Types of islamic institutions and records
Types of islamic institutions and recordsTypes of islamic institutions and records
Types of islamic institutions and records
 
Islamic information seeking behavior
Islamic information seeking behaviorIslamic information seeking behavior
Islamic information seeking behavior
 
Islamic information management
Islamic information managementIslamic information management
Islamic information management
 
Islamic information management sources in islam
Islamic information management sources in islamIslamic information management sources in islam
Islamic information management sources in islam
 
The need for security
The need for securityThe need for security
The need for security
 
The information security audit
The information security auditThe information security audit
The information security audit
 
Security technologies
Security technologiesSecurity technologies
Security technologies
 
Security policy
Security policySecurity policy
Security policy
 
Security and personnel
Security and personnelSecurity and personnel
Security and personnel
 
Secure
SecureSecure
Secure
 
Risk management ii
Risk management iiRisk management ii
Risk management ii
 
Risk management i
Risk management iRisk management i
Risk management i
 
Privacy & security in heath care it
Privacy & security in heath care itPrivacy & security in heath care it
Privacy & security in heath care it
 
Physical security
Physical securityPhysical security
Physical security
 
Legal, ethical & professional issues
Legal, ethical & professional issuesLegal, ethical & professional issues
Legal, ethical & professional issues
 

Último

VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一ffjhghh
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiSuhani Kapoor
 

Último (20)

VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一定制英国白金汉大学毕业证(UCB毕业证书)																			成绩单原版一比一
定制英国白金汉大学毕业证(UCB毕业证书) 成绩单原版一比一
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
 

Chapter 02 php basic syntax

  • 1. PHP Basic SyntaxPHP Basic Syntax Chapter 2 MOHAMAD RAHIMI MOHAMAD ROSMAN
  • 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?
  • 21. ExampleExample MOHAMAD 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?
  • 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
  • 24. PHP OperatorsPHP Operators Chapter 2 MOHAMAD RAHIMI MOHAMAD ROSMAN
  • 25. Arimethic OperatorArimethic Operator MOHAMAD RAHIMI MOHAMAD ROSMAN Operator Description Example Result + Addition x=2 x+2 4 - Subtraction x=2 5-x 3 * Multiplication x=4 x*5 20 / Division 15/5 5/2 3 2.5 % Modulus (division remainder) 5%2 10%8 10%2 1 2 0 ++ Increment x=5 x++ x=6 -- Decrement x=5 x-- x=4
  • 26. Assignment OperatorAssignment Operator MOHAMAD 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
  • 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
  • 31. ExerciseExercise Indicate true or false 1. 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
  • 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
  • 35. 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
  • 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