SlideShare a Scribd company logo
PHP Basic
Khem Puthea
putheakhemdeveloper@gmail.com
Part I: Understanding PHP Basics
Using Variables and Operators
Prepared by KhmerCourse
Storing Data in Variables
៚ Some simple rules for naming variables
៙ Be preceded with a dollar symbol $
៙ And begin with a letter or underscore _
៙ Optionally followed by more letters, numbers, or underscore
៙ Be not permitted
ៜ Punctuation: commas ,, quotation marks ?, or periods .
ៜ Spaces
៙ e.g.
ៜ $id, $_name and $query3: valid
ៜ $96, $day. and email: invalid
៚ Variable names are case-sensitive.
៙ e.g. $name and $Name refer to different variables.
3
Assigning Values to Variables
៚ $var = val;
៙ e.g. assigningValues2Variables.php
<?php $language = "PHP"; ?>
<h1>Welcome <?php echo $language; ?></h1>
៚ Dynamic variable's name
៙ e.g. dynamicVariableName.php
<?php
$clone = "real";
// create a
value of
${$clone} =
echo $real;
?>
new variable dynamically
$clone
"REAL";
// output: REAL
at run time from the
Is it possible for a variable's name itself to be a variable?
៚ echo(): print the value of a variable
4
Destroying Variables
៚ e.g. destroyingVariables.php
<?php
$apple = "Apple";
echo $apple; // output: Apple
// unset()
unset($apple);
echo $apple; // error: Undefined variable
$banana = "Banana";
echo $banana; //
// NULL value
$banana = NULL;
echo $banana; //
?>
output: Banana
output: (nothing)
5
Inspecting Variable Contents
៚ e.g. inspectingVariableContents.php
<?php
$apple = "Apple"; $yr = 2011;
// var_dump()
var_dump($apple);
var_dump($yr); //
// output: string(5)
output: int(2011)
"Apple"
// print_r()
print_r($apple); // output: Apple
print_r($yr);
?>
// output: 2011
6
Understanding PHP’s Data Types
៚ Data type is the values assigned to a variable.
៚ Booleans
៙ 1 (true) or 0 (false)
៚ 2 numeric
៙ Floating-point values (a.k.a floats or doubles) are decimal or fractional
numbers,
៙ While integers are round numbers.
៚ Non-numeric: String
៙ Be enclosed in single quotes (') or double quotes (")
៚ NULL (a special data type in PHP4)
៙ Represent empty variables; a variable of type NULL is a variable without
any data.
A NULL value is not equivalent to an empty string "".
7
Understanding PHP’s Data Types (cont.)
៚ e.g. hexadecimal_octal_scientificNotation.php
<?php
$dec
echo
= 8; // decimal
$dec; // output: 8
$oct
echo
= 010; // octal
$oct; // output: 8
$hex
echo
= 0x5dc;
$hex; //
// hexadecimal
output: 1500
// scientific notation
$sn1
$sn2
echo
?>
= 6.9e+2;
= 6.9e-2;
$sn1." ".$sn2; // output: 690 0.069
8
Setting and Checking Variable Data Types
៚ e.g. setting_CheckingVariableDataTypes.php
<?php
$apple = "Apple";
echo gettype($apple); // output: string
$yr = 2011;
echo gettype($yr); // output: integer
$valid = true;
echo gettype($valid); // output : boolean
echo gettype($banana); // output: NULL
variable)
(error: Undefined
$empty = NULL;
echo gettype($empty); // output: NULL
?>
9
Setting and Checking Variable Data Types (cont.)
៚ e.g. casting.php
<?php
$f_speed =
$i_speed =
// output:
36.9; // floating-point
(integer)
36.9
$f_speed; // cast to integer
echo $f_speed;
// output: 36
echo
?>
$i_speed;;
10
Data Type Checking Functions
Function Purpose
is_bool Test if holding a Boolean value
is_numeric Test if holding a numeric value
is_int Test if holding an integer value
is_float Test if holding a float value
is_string Test if holding a string value
is_null Test if holding a NULL value
is_array Test if being an array
is_object Test if being an object
Using Constants
៚ define(CONST, val);
៚ Constant names follows the same rules as variable names but not the $
៚ e.g. usingConstants.php
<?php
define("APPLE", "Apple");
define("YR", 2011);
prefix.
// output: Apple 2011
echo
?>
APPLE." ".YR;
Constants name are usually entirely UPPERCASED.
When should we use a variable, and when should we use a constant?
11
Manipulating Variables with Operators
៚ Operators are symbols that tell the PHP processor to perform certain actions.
៚ PHP supports more than 50 such operators, ranging from operators for
arithmetical operations to operators for logical comparison and bitwise
calculations.
12
Performing Arithmetic Operations
៚ e.g. arithmeticOperations.php
<?php
echo 3 + 2; // output: 5
echo 3 - 2; // output: 1
echo 3 * 2; // output: 6
echo 3 / 2; // output: 1.5
echo
?>
3 % 2; // output: 1
Is there any limit on how large a PHP integer value can be?
13
Arithmetic Operators
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
% Modulus
Concatenating Strings
៚ e.g. concatenatingStrings.php
<?php
$apple = "Apple";
$banana = "Banana";
// use (.) to join strings into 1
$fruits = $apple." and ".$banana;
// output: I love apple and
".$fruits.".";
banana..
echo
?>
"I love
14
Comparing Variables
៚ e.g. comparingVariables.php
<?php
$num = 6; $num2 = 3; $str = "6";
// output:
echo ($num
// output:
echo ($num
0
<
1
>
(false)
$num2);
(true)
$num2);
// output:
echo ($num
0
<
(false)
$str);
// output:
echo ($num
// output:
echo ($num
?>
1 (true)
== $str);
0 (false)
=== $str);
15
Comparison Operators
Operator Description
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
=== Equal to and of the same type
Performing Logical Tests
៚ e.g. performingLogicalTests.php
<?php
// output:
echo (true
// output:
echo (true
// output:
1 (true)
&& true);
0 (false)
&& false);
0 (false)
echo (false && false);
// output: 1 (true)
echo (false || true);
// output: 0 (false)
echo (!true);
?>
16
Logical Operators
Operator Description
&& AND
|| OR
! NOT
Other Useful Operators
៚ e.g. otherUsefulOperators.php
<?php
$count = 7; $age = 60; $greet = "We";
Increased by 1: ++
Decreased by 1: --
$count -= 2;
// output: 5
echo $count;
e.g. $count++; // $count = $count + 1;
$age /= 5;
// output: 12
echo $age;
$greet .= "lcome!";
// output: Welcome!
echo $greet;
?>
17
Assignment Operators
Operator Description
+= Add, assign
-= Subtract, assign
*= Multiply, assign
/= Divide, assign
%= Modulus, assign
.= Concatenate, assign
Understanding Operator Precedence
៚ Operators at the same level have equal precedence:
៙ ++
៙ !
៙ *
៙ +
៙ <
៙ ==
៙ &&
៙ ||
៙ =
--
/
-
<=
!=
%
.
> >=
=== !==
+= -= *= /= .= %= &= |= ^=
៚ Parentheses (
៚ e.g.
៙ 3 + 2 *
៙ (3 + 2)
): force PHP to evaluate it first
5; // 3 + 10 = 13
* 5; // 5 * 5 = 25
18
Handling Form Input
៚ e.g. chooseCar.html
<form name="fCar" method="POST" action="getCar.php">
<select name="selType">
<option value="Porsche">Porsche</option>
<option value="Ford">Ford</option>
</select>
Color:
<input
<input
</form>
type="text" name="txtColor" />
type="submit" value="get Car" />
action="getCar.php"
Reference a PHP script
method="POST"
Submission via POST
GET: method="GET"
19
Handling Form Input (cont.)
៚ e.g. getCar.php
<?php
// get values via $_POST | $_GET
$type = $_POST["selType"];
$color = $_POST["txtColor"];
echo $color." ".$type;
?>
$_POST[fieldName];
$_POST: a special container variable (array) is used to get a value of a field
of a form sent by using the POST method (or $_GET for the GET method).
fieldName: the field whose value will be get/assigned to a variable.
20
The End
21
The End

More Related Content

What's hot (20)

standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
•sreejith •sree
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
Mohammed Sikander
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
Nilesh Dalvi
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Nikhil Pandit
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
Sharad Dubey
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
Vishesh Jha
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
Greg Sohl
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
ASHOK KUMAR REDDY
 
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
Php operators
Php operatorsPhp operators
Php operators
Aashiq Kuchey
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
File handling in c
File handling in cFile handling in c
File handling in c
aakanksha s
 
String functions in C
String functions in CString functions in C
String functions in C
baabtra.com - No. 1 supplier of quality freshers
 
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
Vineet Kumar Saini
 
Python tuple
Python   tuplePython   tuple
Python tuple
Mohammed Sikander
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
Danial Mirza
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 

Viewers also liked (20)

Sign up github
Sign up githubSign up github
Sign up github
Khem Puthea
 
Introduction to css part1
Introduction to css part1Introduction to css part1
Introduction to css part1
Khem Puthea
 
Membuat partisi di os windows
Membuat partisi di os windowsMembuat partisi di os windows
Membuat partisi di os windows
Nugroho Setiawan
 
Facebook Tackle Box: 10 Apps & Tools Every Brand Should Use
Facebook Tackle Box: 10 Apps & Tools Every Brand Should UseFacebook Tackle Box: 10 Apps & Tools Every Brand Should Use
Facebook Tackle Box: 10 Apps & Tools Every Brand Should Use
Firebelly Marketing
 
Q2 2013 ASSA ABLOY investors presentation 19 july
Q2 2013 ASSA ABLOY investors presentation 19 julyQ2 2013 ASSA ABLOY investors presentation 19 july
Q2 2013 ASSA ABLOY investors presentation 19 july
ASSA ABLOY
 
C53200
C53200C53200
C53200
Subhraneel Dey
 
srgoc
srgocsrgoc
srgoc
Gaurav Singh
 
Numeración romana
Numeración romanaNumeración romana
Numeración romana
Samuel Rodríguez
 
Time Management within IT Project Management
Time Management within IT Project ManagementTime Management within IT Project Management
Time Management within IT Project Management
rielaantonio
 
Granada425
Granada425Granada425
Granada425
sjacaruso
 
Caring for Sharring
 Caring for Sharring  Caring for Sharring
Caring for Sharring
faleulaaoelua
 
OICX Retail Customer Experience
OICX Retail Customer ExperienceOICX Retail Customer Experience
OICX Retail Customer Experience
Damian Kernahan
 
Monomictic lakes francisco muñoz maestre
Monomictic lakes francisco muñoz maestreMonomictic lakes francisco muñoz maestre
Monomictic lakes francisco muñoz maestre
Francisco Maestre
 
Abc c program
Abc c programAbc c program
Abc c program
Dayakar Siddula
 
Hybrid-Active-Optical-Cable-White-Paper
Hybrid-Active-Optical-Cable-White-PaperHybrid-Active-Optical-Cable-White-Paper
Hybrid-Active-Optical-Cable-White-Paper
Nguyen Nguyen
 
Ngaputaw ppt
Ngaputaw pptNgaputaw ppt
Ngaputaw ppt
Thurein Naywinaung
 
Statement to Guardian
Statement to GuardianStatement to Guardian
Statement to Guardian
Aristides Hatzis
 
Aula 1 a obra de kant como síntese do nascente pensamento burguês
Aula 1   a obra de kant como síntese do nascente pensamento burguêsAula 1   a obra de kant como síntese do nascente pensamento burguês
Aula 1 a obra de kant como síntese do nascente pensamento burguês
Leandro Alano
 
Sun & VMware Desktop Training
Sun & VMware Desktop TrainingSun & VMware Desktop Training
Sun & VMware Desktop Training
Matthias Mueller-Prove
 
Introduction to css part1
Introduction to css part1Introduction to css part1
Introduction to css part1
Khem Puthea
 
Membuat partisi di os windows
Membuat partisi di os windowsMembuat partisi di os windows
Membuat partisi di os windows
Nugroho Setiawan
 
Facebook Tackle Box: 10 Apps & Tools Every Brand Should Use
Facebook Tackle Box: 10 Apps & Tools Every Brand Should UseFacebook Tackle Box: 10 Apps & Tools Every Brand Should Use
Facebook Tackle Box: 10 Apps & Tools Every Brand Should Use
Firebelly Marketing
 
Q2 2013 ASSA ABLOY investors presentation 19 july
Q2 2013 ASSA ABLOY investors presentation 19 julyQ2 2013 ASSA ABLOY investors presentation 19 july
Q2 2013 ASSA ABLOY investors presentation 19 july
ASSA ABLOY
 
Time Management within IT Project Management
Time Management within IT Project ManagementTime Management within IT Project Management
Time Management within IT Project Management
rielaantonio
 
OICX Retail Customer Experience
OICX Retail Customer ExperienceOICX Retail Customer Experience
OICX Retail Customer Experience
Damian Kernahan
 
Monomictic lakes francisco muñoz maestre
Monomictic lakes francisco muñoz maestreMonomictic lakes francisco muñoz maestre
Monomictic lakes francisco muñoz maestre
Francisco Maestre
 
Hybrid-Active-Optical-Cable-White-Paper
Hybrid-Active-Optical-Cable-White-PaperHybrid-Active-Optical-Cable-White-Paper
Hybrid-Active-Optical-Cable-White-Paper
Nguyen Nguyen
 
Aula 1 a obra de kant como síntese do nascente pensamento burguês
Aula 1   a obra de kant como síntese do nascente pensamento burguêsAula 1   a obra de kant como síntese do nascente pensamento burguês
Aula 1 a obra de kant como síntese do nascente pensamento burguês
Leandro Alano
 
Ad

Similar to Php using variables-operators (20)

PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP nPHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
ArtiRaju1
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Saraswathi Murugan
 
Php
PhpPhp
Php
Rajkiran Mummadi
 
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfIT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
pkaviya
 
Unit IV.pptx Server side scripting PHP IT3401
Unit IV.pptx Server side scripting PHP IT3401Unit IV.pptx Server side scripting PHP IT3401
Unit IV.pptx Server side scripting PHP IT3401
lakshitakumar291
 
Vision academy classes bcs_bca_bba_sybba_php
Vision academy  classes bcs_bca_bba_sybba_phpVision academy  classes bcs_bca_bba_sybba_php
Vision academy classes bcs_bca_bba_sybba_php
sachin892777
 
Chapter 4 server side Php Haypertext P.pptx
Chapter 4 server side Php Haypertext P.pptxChapter 4 server side Php Haypertext P.pptx
Chapter 4 server side Php Haypertext P.pptx
KelemAlebachew
 
Web Technology_10.ppt
Web Technology_10.pptWeb Technology_10.ppt
Web Technology_10.ppt
Aftabali702240
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Muthuganesh S
 
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP nPHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
ArtiRaju1
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
McSoftsis
 
Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26
SynapseindiaComplaints
 
Php introduction
Php introductionPhp introduction
Php introduction
Pratik Patel
 
Php modul-1
Php modul-1Php modul-1
Php modul-1
Kristophorus Hadiono
 
Intro to php
Intro to phpIntro to php
Intro to php
NithyaNithyav
 
Intoroduction to Adnvanced Internet Programming Chapter two.pptx
Intoroduction to Adnvanced Internet Programming Chapter two.pptxIntoroduction to Adnvanced Internet Programming Chapter two.pptx
Intoroduction to Adnvanced Internet Programming Chapter two.pptx
JerusalemFetene
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
Denis Ristic
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
sami2244
 
PHP-Part1
PHP-Part1PHP-Part1
PHP-Part1
Ahmed Saihood
 
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP nPHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
ArtiRaju1
 
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfIT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
pkaviya
 
Unit IV.pptx Server side scripting PHP IT3401
Unit IV.pptx Server side scripting PHP IT3401Unit IV.pptx Server side scripting PHP IT3401
Unit IV.pptx Server side scripting PHP IT3401
lakshitakumar291
 
Vision academy classes bcs_bca_bba_sybba_php
Vision academy  classes bcs_bca_bba_sybba_phpVision academy  classes bcs_bca_bba_sybba_php
Vision academy classes bcs_bca_bba_sybba_php
sachin892777
 
Chapter 4 server side Php Haypertext P.pptx
Chapter 4 server side Php Haypertext P.pptxChapter 4 server side Php Haypertext P.pptx
Chapter 4 server side Php Haypertext P.pptx
KelemAlebachew
 
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP nPHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
ArtiRaju1
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
McSoftsis
 
Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26Synapse india complain sharing info about php chaptr 26
Synapse india complain sharing info about php chaptr 26
SynapseindiaComplaints
 
Intoroduction to Adnvanced Internet Programming Chapter two.pptx
Intoroduction to Adnvanced Internet Programming Chapter two.pptxIntoroduction to Adnvanced Internet Programming Chapter two.pptx
Intoroduction to Adnvanced Internet Programming Chapter two.pptx
JerusalemFetene
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
Denis Ristic
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
sami2244
 
Ad

Recently uploaded (20)

How to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time OffHow to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time Off
Celine George
 
How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18
Celine George
 
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly WorkshopsLDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDM & Mia eStudios
 
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptxQUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
Sourav Kr Podder
 
Types of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo SlidesTypes of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo Slides
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Pragya Champion's Chalice 2025 Set , General Quiz
Pragya Champion's Chalice 2025 Set , General QuizPragya Champion's Chalice 2025 Set , General Quiz
Pragya Champion's Chalice 2025 Set , General Quiz
Pragya - UEM Kolkata Quiz Club
 
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
wygalkelceqg
 
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdfForestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
ChalaKelbessa
 
Uterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managmentUterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managment
Ritu480198
 
State institute of educational technology
State institute of educational technologyState institute of educational technology
State institute of educational technology
vp5806484
 
Critical Thinking and Bias with Jibi Moses
Critical Thinking and Bias with Jibi MosesCritical Thinking and Bias with Jibi Moses
Critical Thinking and Bias with Jibi Moses
Excellence Foundation for South Sudan
 
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdfপ্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
Pragya - UEM Kolkata Quiz Club
 
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
 
LDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College VolumeLDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College Volume
LDM & Mia eStudios
 
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT PatnaSwachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Swachata Quiz - Prelims - 01.10.24 - Quiz Club IIT Patna
Quiz Club, Indian Institute of Technology, Patna
 
Introduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdfIntroduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdf
CME4Life
 
Odoo 18 Point of Sale PWA - Odoo Slides
Odoo 18 Point of Sale PWA  - Odoo  SlidesOdoo 18 Point of Sale PWA  - Odoo  Slides
Odoo 18 Point of Sale PWA - Odoo Slides
Celine George
 
0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx
0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx
0b - THE ROMANTIC ERA: FEELINGS AND IDENTITY.pptx
Julián Jesús Pérez Fernández
 
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
 
How to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time OffHow to Create Time Off Request in Odoo 18 Time Off
How to Create Time Off Request in Odoo 18 Time Off
Celine George
 
How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18How to Setup Renewal of Subscription in Odoo 18
How to Setup Renewal of Subscription in Odoo 18
Celine George
 
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly WorkshopsLDMMIA Free Reiki Yoga S7 Weekly Workshops
LDMMIA Free Reiki Yoga S7 Weekly Workshops
LDM & Mia eStudios
 
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptxQUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
Sourav Kr Podder
 
Types of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo SlidesTypes of Actions in Odoo 18 - Odoo Slides
Types of Actions in Odoo 18 - Odoo Slides
Celine George
 
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
Active Surveillance For Localized Prostate Cancer A New Paradigm For Clinical...
wygalkelceqg
 
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdfForestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
ChalaKelbessa
 
Uterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managmentUterine Prolapse, causes type and classification,its managment
Uterine Prolapse, causes type and classification,its managment
Ritu480198
 
State institute of educational technology
State institute of educational technologyState institute of educational technology
State institute of educational technology
vp5806484
 
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdfপ্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
প্রত্যুৎপন্নমতিত্ব - Prottutponnomotittwa 2025.pdf
Pragya - UEM Kolkata Quiz Club
 
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
 
LDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College VolumeLDMMIA About me 2025 Edition 3 College Volume
LDMMIA About me 2025 Edition 3 College Volume
LDM & Mia eStudios
 
Introduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdfIntroduction to Online CME for Nurse Practitioners.pdf
Introduction to Online CME for Nurse Practitioners.pdf
CME4Life
 
Odoo 18 Point of Sale PWA - Odoo Slides
Odoo 18 Point of Sale PWA  - Odoo  SlidesOdoo 18 Point of Sale PWA  - Odoo  Slides
Odoo 18 Point of Sale PWA - Odoo Slides
Celine George
 
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
 

Php using variables-operators

  • 2. Part I: Understanding PHP Basics Using Variables and Operators Prepared by KhmerCourse
  • 3. Storing Data in Variables ៚ Some simple rules for naming variables ៙ Be preceded with a dollar symbol $ ៙ And begin with a letter or underscore _ ៙ Optionally followed by more letters, numbers, or underscore ៙ Be not permitted ៜ Punctuation: commas ,, quotation marks ?, or periods . ៜ Spaces ៙ e.g. ៜ $id, $_name and $query3: valid ៜ $96, $day. and email: invalid ៚ Variable names are case-sensitive. ៙ e.g. $name and $Name refer to different variables. 3
  • 4. Assigning Values to Variables ៚ $var = val; ៙ e.g. assigningValues2Variables.php <?php $language = "PHP"; ?> <h1>Welcome <?php echo $language; ?></h1> ៚ Dynamic variable's name ៙ e.g. dynamicVariableName.php <?php $clone = "real"; // create a value of ${$clone} = echo $real; ?> new variable dynamically $clone "REAL"; // output: REAL at run time from the Is it possible for a variable's name itself to be a variable? ៚ echo(): print the value of a variable 4
  • 5. Destroying Variables ៚ e.g. destroyingVariables.php <?php $apple = "Apple"; echo $apple; // output: Apple // unset() unset($apple); echo $apple; // error: Undefined variable $banana = "Banana"; echo $banana; // // NULL value $banana = NULL; echo $banana; // ?> output: Banana output: (nothing) 5
  • 6. Inspecting Variable Contents ៚ e.g. inspectingVariableContents.php <?php $apple = "Apple"; $yr = 2011; // var_dump() var_dump($apple); var_dump($yr); // // output: string(5) output: int(2011) "Apple" // print_r() print_r($apple); // output: Apple print_r($yr); ?> // output: 2011 6
  • 7. Understanding PHP’s Data Types ៚ Data type is the values assigned to a variable. ៚ Booleans ៙ 1 (true) or 0 (false) ៚ 2 numeric ៙ Floating-point values (a.k.a floats or doubles) are decimal or fractional numbers, ៙ While integers are round numbers. ៚ Non-numeric: String ៙ Be enclosed in single quotes (') or double quotes (") ៚ NULL (a special data type in PHP4) ៙ Represent empty variables; a variable of type NULL is a variable without any data. A NULL value is not equivalent to an empty string "". 7
  • 8. Understanding PHP’s Data Types (cont.) ៚ e.g. hexadecimal_octal_scientificNotation.php <?php $dec echo = 8; // decimal $dec; // output: 8 $oct echo = 010; // octal $oct; // output: 8 $hex echo = 0x5dc; $hex; // // hexadecimal output: 1500 // scientific notation $sn1 $sn2 echo ?> = 6.9e+2; = 6.9e-2; $sn1." ".$sn2; // output: 690 0.069 8
  • 9. Setting and Checking Variable Data Types ៚ e.g. setting_CheckingVariableDataTypes.php <?php $apple = "Apple"; echo gettype($apple); // output: string $yr = 2011; echo gettype($yr); // output: integer $valid = true; echo gettype($valid); // output : boolean echo gettype($banana); // output: NULL variable) (error: Undefined $empty = NULL; echo gettype($empty); // output: NULL ?> 9
  • 10. Setting and Checking Variable Data Types (cont.) ៚ e.g. casting.php <?php $f_speed = $i_speed = // output: 36.9; // floating-point (integer) 36.9 $f_speed; // cast to integer echo $f_speed; // output: 36 echo ?> $i_speed;; 10 Data Type Checking Functions Function Purpose is_bool Test if holding a Boolean value is_numeric Test if holding a numeric value is_int Test if holding an integer value is_float Test if holding a float value is_string Test if holding a string value is_null Test if holding a NULL value is_array Test if being an array is_object Test if being an object
  • 11. Using Constants ៚ define(CONST, val); ៚ Constant names follows the same rules as variable names but not the $ ៚ e.g. usingConstants.php <?php define("APPLE", "Apple"); define("YR", 2011); prefix. // output: Apple 2011 echo ?> APPLE." ".YR; Constants name are usually entirely UPPERCASED. When should we use a variable, and when should we use a constant? 11
  • 12. Manipulating Variables with Operators ៚ Operators are symbols that tell the PHP processor to perform certain actions. ៚ PHP supports more than 50 such operators, ranging from operators for arithmetical operations to operators for logical comparison and bitwise calculations. 12
  • 13. Performing Arithmetic Operations ៚ e.g. arithmeticOperations.php <?php echo 3 + 2; // output: 5 echo 3 - 2; // output: 1 echo 3 * 2; // output: 6 echo 3 / 2; // output: 1.5 echo ?> 3 % 2; // output: 1 Is there any limit on how large a PHP integer value can be? 13 Arithmetic Operators Operator Description + Add - Subtract * Multiply / Divide % Modulus
  • 14. Concatenating Strings ៚ e.g. concatenatingStrings.php <?php $apple = "Apple"; $banana = "Banana"; // use (.) to join strings into 1 $fruits = $apple." and ".$banana; // output: I love apple and ".$fruits."."; banana.. echo ?> "I love 14
  • 15. Comparing Variables ៚ e.g. comparingVariables.php <?php $num = 6; $num2 = 3; $str = "6"; // output: echo ($num // output: echo ($num 0 < 1 > (false) $num2); (true) $num2); // output: echo ($num 0 < (false) $str); // output: echo ($num // output: echo ($num ?> 1 (true) == $str); 0 (false) === $str); 15 Comparison Operators Operator Description == Equal to != Not equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to === Equal to and of the same type
  • 16. Performing Logical Tests ៚ e.g. performingLogicalTests.php <?php // output: echo (true // output: echo (true // output: 1 (true) && true); 0 (false) && false); 0 (false) echo (false && false); // output: 1 (true) echo (false || true); // output: 0 (false) echo (!true); ?> 16 Logical Operators Operator Description && AND || OR ! NOT
  • 17. Other Useful Operators ៚ e.g. otherUsefulOperators.php <?php $count = 7; $age = 60; $greet = "We"; Increased by 1: ++ Decreased by 1: -- $count -= 2; // output: 5 echo $count; e.g. $count++; // $count = $count + 1; $age /= 5; // output: 12 echo $age; $greet .= "lcome!"; // output: Welcome! echo $greet; ?> 17 Assignment Operators Operator Description += Add, assign -= Subtract, assign *= Multiply, assign /= Divide, assign %= Modulus, assign .= Concatenate, assign
  • 18. Understanding Operator Precedence ៚ Operators at the same level have equal precedence: ៙ ++ ៙ ! ៙ * ៙ + ៙ < ៙ == ៙ && ៙ || ៙ = -- / - <= != % . > >= === !== += -= *= /= .= %= &= |= ^= ៚ Parentheses ( ៚ e.g. ៙ 3 + 2 * ៙ (3 + 2) ): force PHP to evaluate it first 5; // 3 + 10 = 13 * 5; // 5 * 5 = 25 18
  • 19. Handling Form Input ៚ e.g. chooseCar.html <form name="fCar" method="POST" action="getCar.php"> <select name="selType"> <option value="Porsche">Porsche</option> <option value="Ford">Ford</option> </select> Color: <input <input </form> type="text" name="txtColor" /> type="submit" value="get Car" /> action="getCar.php" Reference a PHP script method="POST" Submission via POST GET: method="GET" 19
  • 20. Handling Form Input (cont.) ៚ e.g. getCar.php <?php // get values via $_POST | $_GET $type = $_POST["selType"]; $color = $_POST["txtColor"]; echo $color." ".$type; ?> $_POST[fieldName]; $_POST: a special container variable (array) is used to get a value of a field of a form sent by using the POST method (or $_GET for the GET method). fieldName: the field whose value will be get/assigned to a variable. 20