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

More Related Content

PDF
Introduction to PHP_Slides by Lesley_Bonyo.pdf
PPTX
PHP Variables and scopes
PPTX
PHP2An introduction to Gnome.pptx.j.pptx
PPTX
3 Introduction to PHP Variables Moduless
PPTX
PHP BASICs Data Type ECHo and PRINT.pptx
PDF
Chap 4 PHP.pdf
PDF
Materi Dasar PHP
PDF
web Based Application Devlopment using PHP
Introduction to PHP_Slides by Lesley_Bonyo.pdf
PHP Variables and scopes
PHP2An introduction to Gnome.pptx.j.pptx
3 Introduction to PHP Variables Moduless
PHP BASICs Data Type ECHo and PRINT.pptx
Chap 4 PHP.pdf
Materi Dasar PHP
web Based Application Devlopment using PHP

Similar to PHPVariables_075026.ppt (20)

PPTX
Basic of PHP
PPTX
Introduction to php contains basic....pptx
PDF
Introduction to php
PPTX
Variables
PPTX
PHP Training Part1
PPTX
php basics
PDF
Hsc IT 5. Server-Side Scripting (PHP).pdf
PPTX
php basic part one
PPTX
Php Tutorial
PDF
Php notes
PPTX
Learn PHP Basics
PPTX
PHP Basics
PDF
phptutorial
PDF
phptutorial
PDF
PHP Basics Ebook
PPTX
PPTX
chapter Two Server-side Script lang.pptx
PPT
Php essentials
PPTX
unit 1.pptx
PPTX
Variable scope in php
Basic of PHP
Introduction to php contains basic....pptx
Introduction to php
Variables
PHP Training Part1
php basics
Hsc IT 5. Server-Side Scripting (PHP).pdf
php basic part one
Php Tutorial
Php notes
Learn PHP Basics
PHP Basics
phptutorial
phptutorial
PHP Basics Ebook
chapter Two Server-side Script lang.pptx
Php essentials
unit 1.pptx
Variable scope in php
Ad

Recently uploaded (20)

PPTX
international classification of diseases ICD-10 review PPT.pptx
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PDF
Paper PDF World Game (s) Great Redesign.pdf
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPTX
E -tech empowerment technologies PowerPoint
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
DOCX
Unit-3 cyber security network security of internet system
PPTX
Introduction to Information and Communication Technology
PPTX
artificial intelligence overview of it and more
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PDF
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PDF
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PDF
The Internet -By the Numbers, Sri Lanka Edition
Β 
PDF
Testing WebRTC applications at scale.pdf
international classification of diseases ICD-10 review PPT.pptx
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Paper PDF World Game (s) Great Redesign.pdf
An introduction to the IFRS (ISSB) Stndards.pdf
presentation_pfe-universite-molay-seltan.pptx
E -tech empowerment technologies PowerPoint
522797556-Unit-2-Temperature-measurement-1-1.pptx
introduction about ICD -10 & ICD-11 ppt.pptx
SASE Traffic Flow - ZTNA Connector-1.pdf
Unit-3 cyber security network security of internet system
Introduction to Information and Communication Technology
artificial intelligence overview of it and more
Cloud-Scale Log Monitoring _ Datadog.pdf
How to Ensure Data Integrity During Shopify Migration_ Best Practices for Sec...
PptxGenJS_Demo_Chart_20250317130215833.pptx
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
Slides PDF The World Game (s) Eco Economic Epochs.pdf
Slides PPTX World Game (s) Eco Economic Epochs.pptx
The Internet -By the Numbers, Sri Lanka Edition
Β 
Testing WebRTC applications at scale.pdf
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