SlideShare a Scribd company logo
PHPVariables
1
Presented by:Vinit
What is a
variable?
 A variable in PHP is the name of the
memory location that holds data. In
PHP, a variable is declared using the
$ sign followed by the variable name.
The main way to store information in
the middle of a PHP program is by
using a variable.
2
PHPVariable
Rules
• A variable must start with a dollar ($) sign,
followed by the variable name.
• It can only contain alpha-numeric character and
underscore (A-z, 0-9, _).
• A variable name must start with a letter or
underscore (_) character.
• A PHP variable name cannot contain spaces.
• One thing to be kept in mind that the variable
name cannot start with a number or special
symbols.
• PHP variables are case-sensitive, so $name
and $NAME both are treated as different
variable.
3
Types of
Variable
 Data types utilized by PHP to declare or
build variables:
• Null
• Doubles
• Integers
• Strings
• Booleans
• Resources
• Objects
• Arrays
4
Assignment
 A variable is said to be assigned a value in
an assignment statement. E.g.
$NumberHours = 45;
$WorkerFirstName = “Pete”;
 The assignment statement should not be
thought of as expressing mathematical
equality rather it a set of instructions to
evaluate the expression on the right hand
side and give the variable on the left hand
side the value that results. Thus
$Counter = $Counter + 1;
Makes sense as an assignment
statement even though it makes no
sense as mathematics.
5
PHP Variables
Scopes  PHPVariables Scope In PHP, variables can be
declared anywhere in the script.The scope of
a variable is the part of the script where the
variable can be referenced/used.
 PHP has three different variable scopes:
• Local
• Global
• Static
6
Local
Variable
 The variables that are declared within a function are called
local variables for that function. These local variables have
their scope only in that particular function in which they
are declared. This means that these variables cannot be
accessed outside the function, as they have local scope.
Example:
<?php
function local_var()
{
$num = 45; //local variable
echo "Local variable declared inside the function is: ". $
num;
}
local_var();
?>
7
Global
Variable
 The global variables are the variables that are
declared outside the function. These variables
can be accessed anywhere in the program. To
access the global variable within a function, use
the GLOBAL keyword before the variable.
However, these variables can be directly
accessed or used outside the function without
any keyword. Therefore there is no need to use
any keyword to access a global variable
outside the function.
Ex:<?php
$name = “VK"; //global variable
function global_var()
{
echo "Variable inside the function: ". $name;
echo "</br>";
}
global_var();
?>
8
Static
Variable
 A static variable is the attribute of PHP to erase
the variable once it finishes its execution and
the memory is liberated. However, in some
cases, we really want to store the variables
even after the fulfillment of function execution.
Ex:
<?php
function keep_track() {
STATIC $count = 0;
$count++;
print $count;
print "<br/>";
}
keep_track();
keep_track();
keep_track();
?>
9
Thank you
10
Ad

More Related Content

Similar to PHPVariables_075026.ppt (20)

Introduction to php
Introduction to phpIntroduction to php
Introduction to php
KIRAN KUMAR SILIVERI
 
Variables
VariablesVariables
Variables
Suraj Motee
 
PHP Training Part1
PHP Training Part1PHP Training Part1
PHP Training Part1
than sare
 
php basics
php basicsphp basics
php basics
Anmol Paul
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfHsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdf
AAFREEN SHAIKH
 
php basic part one
php basic part onephp basic part one
php basic part one
jeweltutin
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
pratik tambekar
 
Php notes
Php notesPhp notes
Php notes
Muthuganesh S
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
McSoftsis
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Muthuganesh S
 
phptutorial
phptutorialphptutorial
phptutorial
tutorialsruby
 
phptutorial
phptutorialphptutorial
phptutorial
tutorialsruby
 
PHP Basics Ebook
PHP Basics EbookPHP Basics Ebook
PHP Basics Ebook
Swanand Pol
 
Php1
Php1Php1
Php1
Shamik Tiwari
 
chapter Two Server-side Script lang.pptx
chapter  Two Server-side Script lang.pptxchapter  Two Server-side Script lang.pptx
chapter Two Server-side Script lang.pptx
alehegn9
 
Php essentials
Php essentialsPhp essentials
Php essentials
sagaroceanic11
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
adityathote3
 
Variable scope in php
Variable scope in phpVariable scope in php
Variable scope in php
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Introduction to php
Introduction  to  phpIntroduction  to  php
Introduction to php
leo paul
 
introduction to php and its uses in daily
introduction to php and its uses in dailyintroduction to php and its uses in daily
introduction to php and its uses in daily
vishal choudhary
 

Recently uploaded (20)

How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
34 Mobile Payment (Thomas Lerner (auth.).pdf
34 Mobile Payment (Thomas Lerner (auth.).pdf34 Mobile Payment (Thomas Lerner (auth.).pdf
34 Mobile Payment (Thomas Lerner (auth.).pdf
Nguyễn Minh
 
Presentation Mehdi Monitorama 2022 Cancer and Monitoring
Presentation Mehdi Monitorama 2022 Cancer and MonitoringPresentation Mehdi Monitorama 2022 Cancer and Monitoring
Presentation Mehdi Monitorama 2022 Cancer and Monitoring
mdaoudi
 
34 Turban Electronic Commerce 2018_ A Managerial and Social Networks Perspect...
34 Turban Electronic Commerce 2018_ A Managerial and Social Networks Perspect...34 Turban Electronic Commerce 2018_ A Managerial and Social Networks Perspect...
34 Turban Electronic Commerce 2018_ A Managerial and Social Networks Perspect...
Nguyễn Minh
 
Internet Coordination Policy 2 (ICP-2) Review
Internet Coordination Policy 2 (ICP-2) ReviewInternet Coordination Policy 2 (ICP-2) Review
Internet Coordination Policy 2 (ICP-2) Review
APNIC
 
APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...
APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...
APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...
APNIC
 
34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...
34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...
34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...
Nguyễn Minh
 
34 Advances in Mobile Commerce Technologies (2003).pdf
34 Advances in Mobile Commerce Technologies (2003).pdf34 Advances in Mobile Commerce Technologies (2003).pdf
34 Advances in Mobile Commerce Technologies (2003).pdf
Nguyễn Minh
 
ProjectArtificial Intelligence Good or Evil.pptx
ProjectArtificial Intelligence Good or Evil.pptxProjectArtificial Intelligence Good or Evil.pptx
ProjectArtificial Intelligence Good or Evil.pptx
OlenaKotovska
 
水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证
水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证
水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证
Taqyea
 
GiacomoVacca - WebRTC - troubleshooting media negotiation.pdf
GiacomoVacca - WebRTC - troubleshooting media negotiation.pdfGiacomoVacca - WebRTC - troubleshooting media negotiation.pdf
GiacomoVacca - WebRTC - troubleshooting media negotiation.pdf
Giacomo Vacca
 
Cloud-to-cloud Migration presentation.pptx
Cloud-to-cloud Migration presentation.pptxCloud-to-cloud Migration presentation.pptx
Cloud-to-cloud Migration presentation.pptx
marketing140789
 
34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...
34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...
34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...
Nguyễn Minh
 
Biochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptx
Biochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptxBiochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptx
Biochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptx
SergioBarreno2
 
34 E-commerce - business, technology and society (2022).pdf
34 E-commerce - business, technology and society (2022).pdf34 E-commerce - business, technology and society (2022).pdf
34 E-commerce - business, technology and society (2022).pdf
Nguyễn Minh
 
23 Introduction to E-Commerce ( PDFDrive ) (1).pdf
23 Introduction to E-Commerce ( PDFDrive ) (1).pdf23 Introduction to E-Commerce ( PDFDrive ) (1).pdf
23 Introduction to E-Commerce ( PDFDrive ) (1).pdf
Nguyễn Minh
 
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness GuideThe Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
russellpeter1995
 
Breaking Down the Latest Spectrum Internet Plans.pdf
Breaking Down the Latest Spectrum Internet Plans.pdfBreaking Down the Latest Spectrum Internet Plans.pdf
Breaking Down the Latest Spectrum Internet Plans.pdf
Internet Bundle Now
 
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
Taqyea
 
Fractures In Chronic Kidney Disease Patients - Copy (3).pptx
Fractures In Chronic Kidney Disease Patients - Copy (3).pptxFractures In Chronic Kidney Disease Patients - Copy (3).pptx
Fractures In Chronic Kidney Disease Patients - Copy (3).pptx
ChaitanJaunky1
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
34 Mobile Payment (Thomas Lerner (auth.).pdf
34 Mobile Payment (Thomas Lerner (auth.).pdf34 Mobile Payment (Thomas Lerner (auth.).pdf
34 Mobile Payment (Thomas Lerner (auth.).pdf
Nguyễn Minh
 
Presentation Mehdi Monitorama 2022 Cancer and Monitoring
Presentation Mehdi Monitorama 2022 Cancer and MonitoringPresentation Mehdi Monitorama 2022 Cancer and Monitoring
Presentation Mehdi Monitorama 2022 Cancer and Monitoring
mdaoudi
 
34 Turban Electronic Commerce 2018_ A Managerial and Social Networks Perspect...
34 Turban Electronic Commerce 2018_ A Managerial and Social Networks Perspect...34 Turban Electronic Commerce 2018_ A Managerial and Social Networks Perspect...
34 Turban Electronic Commerce 2018_ A Managerial and Social Networks Perspect...
Nguyễn Minh
 
Internet Coordination Policy 2 (ICP-2) Review
Internet Coordination Policy 2 (ICP-2) ReviewInternet Coordination Policy 2 (ICP-2) Review
Internet Coordination Policy 2 (ICP-2) Review
APNIC
 
APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...
APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...
APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...
APNIC
 
34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...
34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...
34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...
Nguyễn Minh
 
34 Advances in Mobile Commerce Technologies (2003).pdf
34 Advances in Mobile Commerce Technologies (2003).pdf34 Advances in Mobile Commerce Technologies (2003).pdf
34 Advances in Mobile Commerce Technologies (2003).pdf
Nguyễn Minh
 
ProjectArtificial Intelligence Good or Evil.pptx
ProjectArtificial Intelligence Good or Evil.pptxProjectArtificial Intelligence Good or Evil.pptx
ProjectArtificial Intelligence Good or Evil.pptx
OlenaKotovska
 
水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证
水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证
水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证
Taqyea
 
GiacomoVacca - WebRTC - troubleshooting media negotiation.pdf
GiacomoVacca - WebRTC - troubleshooting media negotiation.pdfGiacomoVacca - WebRTC - troubleshooting media negotiation.pdf
GiacomoVacca - WebRTC - troubleshooting media negotiation.pdf
Giacomo Vacca
 
Cloud-to-cloud Migration presentation.pptx
Cloud-to-cloud Migration presentation.pptxCloud-to-cloud Migration presentation.pptx
Cloud-to-cloud Migration presentation.pptx
marketing140789
 
34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...
34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...
34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...
Nguyễn Minh
 
Biochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptx
Biochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptxBiochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptx
Biochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptx
SergioBarreno2
 
34 E-commerce - business, technology and society (2022).pdf
34 E-commerce - business, technology and society (2022).pdf34 E-commerce - business, technology and society (2022).pdf
34 E-commerce - business, technology and society (2022).pdf
Nguyễn Minh
 
23 Introduction to E-Commerce ( PDFDrive ) (1).pdf
23 Introduction to E-Commerce ( PDFDrive ) (1).pdf23 Introduction to E-Commerce ( PDFDrive ) (1).pdf
23 Introduction to E-Commerce ( PDFDrive ) (1).pdf
Nguyễn Minh
 
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness GuideThe Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
russellpeter1995
 
Breaking Down the Latest Spectrum Internet Plans.pdf
Breaking Down the Latest Spectrum Internet Plans.pdfBreaking Down the Latest Spectrum Internet Plans.pdf
Breaking Down the Latest Spectrum Internet Plans.pdf
Internet Bundle Now
 
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
Taqyea
 
Fractures In Chronic Kidney Disease Patients - Copy (3).pptx
Fractures In Chronic Kidney Disease Patients - Copy (3).pptxFractures In Chronic Kidney Disease Patients - Copy (3).pptx
Fractures In Chronic Kidney Disease Patients - Copy (3).pptx
ChaitanJaunky1
 
Ad

PHPVariables_075026.ppt

  • 2. What is a variable?  A variable in PHP is the name of the memory location that holds data. In PHP, a variable is declared using the $ sign followed by the variable name. The main way to store information in the middle of a PHP program is by using a variable. 2
  • 3. PHPVariable Rules • A variable must start with a dollar ($) sign, followed by the variable name. • It can only contain alpha-numeric character and underscore (A-z, 0-9, _). • A variable name must start with a letter or underscore (_) character. • A PHP variable name cannot contain spaces. • One thing to be kept in mind that the variable name cannot start with a number or special symbols. • PHP variables are case-sensitive, so $name and $NAME both are treated as different variable. 3
  • 4. Types of Variable  Data types utilized by PHP to declare or build variables: • Null • Doubles • Integers • Strings • Booleans • Resources • Objects • Arrays 4
  • 5. Assignment  A variable is said to be assigned a value in an assignment statement. E.g. $NumberHours = 45; $WorkerFirstName = “Pete”;  The assignment statement should not be thought of as expressing mathematical equality rather it a set of instructions to evaluate the expression on the right hand side and give the variable on the left hand side the value that results. Thus $Counter = $Counter + 1; Makes sense as an assignment statement even though it makes no sense as mathematics. 5
  • 6. PHP Variables Scopes  PHPVariables Scope In PHP, variables can be declared anywhere in the script.The scope of a variable is the part of the script where the variable can be referenced/used.  PHP has three different variable scopes: • Local • Global • Static 6
  • 7. Local Variable  The variables that are declared within a function are called local variables for that function. These local variables have their scope only in that particular function in which they are declared. This means that these variables cannot be accessed outside the function, as they have local scope. Example: <?php function local_var() { $num = 45; //local variable echo "Local variable declared inside the function is: ". $ num; } local_var(); ?> 7
  • 8. Global Variable  The global variables are the variables that are declared outside the function. These variables can be accessed anywhere in the program. To access the global variable within a function, use the GLOBAL keyword before the variable. However, these variables can be directly accessed or used outside the function without any keyword. Therefore there is no need to use any keyword to access a global variable outside the function. Ex:<?php $name = “VK"; //global variable function global_var() { echo "Variable inside the function: ". $name; echo "</br>"; } global_var(); ?> 8
  • 9. Static Variable  A static variable is the attribute of PHP to erase the variable once it finishes its execution and the memory is liberated. However, in some cases, we really want to store the variables even after the fulfillment of function execution. Ex: <?php function keep_track() { STATIC $count = 0; $count++; print $count; print "<br/>"; } keep_track(); keep_track(); keep_track(); ?> 9
  翻译: