SlideShare a Scribd company logo
PHP Variables 
Write by: Mahmood masih tehrani 
Www.masihtehrani.ir 
tehrani@dabacenter.ir
Declaring PHP variables 
● All variables in PHP start with a $ (dollar) sign followed by the name of 
the variable. 
● 
● A valid variable name starts with a letter (A-Z, a-z) or underscore (_), 
followed by any number of letters, numbers, or underscores. 
● 
● If a variable name is more than one word, it can be separated with 
underscore (for example $employee_code instead of 
$employeecode). 
● 
● '$' is a special variable that can not be assigned.
Php variables (english)
Valid and invalid PHP variables 
● <?php 
● $abc = 'Welcome'; //valid 
● $Abc = 'W3resource.com'; //valid 
● $9xyz = 'Hello world'; //invalid; starts with a number 
● $_xyz = 'Hello world'; //valid; starts with an underscore 
● $_9xyz = 'Hello world'; //valid 
● $aĂ€a = 'Hello world'; //valid; 'Ă€' is (Extended) ASCII 228. 
● ?>
PHP variable name is case-sensitive 
● <?php 
● $abc = 'Welcome'; 
● echo "Value of abc : $abc"; 
● echo "Value of ABC : $ABC"; 
● ?>
● <?php 
● $height = 3.5; 
● $width = 4; 
● $area=$height*$width; 
● echo "Area of the rectangle is : $area"; 
● ?>
PHP variables : Assigning by 
Reference 
● <?php 
● $foo='bob'; 
● $bar=&$foo; 
● $bar="my $bar"; 
● echo $bar; 
● echo '<br />'; 
● echo $foo; 
● ?>
Output 
● my bob 
● my bob
PHP variable variables 
● <?php 
● $v='var1'; 
● echo $v; // prints var1 
● $$v = 'var2'; 
● echo $$v; // prints var2 
● echo $var1; // prints var2 
● ?>
PHP variable variables 
● You know how to declare variables in PHP. But what if you 
want the name your variable is a variable itself? In PHP, 
you have Variable Variables, so you may assign a variable 
to another variable. 
● In the following example at line no. 2, we declared a 
variable called $v which stores the value 'var1' and in line 
no. 4, "var1" is used as the name of a variable by using 
two dollar signs. i.e. $$v. 
● Therefore there are two variables now. $v which stores the 
value "var1" where as $$v which stores the value var2. At 
this point $$v and $var1 are equal, both store the value 
"var2".
PHP Variables Scope 
● In PHP, variables can be declared anywhere in the script. 
We declare the variables for a particular scope. There are 
two types of scope,
Example 
● <?php 
● //global scope 
● $x = 10; 
● function var_scope() 
● { 
● //local scope 
● $y=20; 
● echo "The value of x is : $x "."<br />"; 
● echo "The value of y is : $y"."<br />"; 
● } 
● var_scope(); 
● echo "The value of x is : $x"."<br />"; 
● echo "The value of y is : $y "; 
● ?>
● In the above script there are two variables 
$x and $y and a function var_scope(). $x 
is a global variable since it is declared 
outside the function and $y is a local 
variable as it is created inside the function 
var_scope(). At the end of the script 
var_scope() function is called, followed by 
two echo statements. Lets see the output 
of the script
● The value of x is : 
● The value of y is : 20 
● The value of x is : 10 
● The value of y is :
● There are two echo statements inside var_scope() function. It prints 
the value of $y as it is the locally declared and can not prints the value 
of $x since it is created outside the function. 
● 
● The next statement of the script prints the value of $x since it is global 
variable i.e. not created inside any function. 
● 
● The last echo statement can not prints the value of $y since it is local 
variable and it is created inside the function var_scope() function.
The global keyword 
● We have already learned variables 
declared outside a function are global. 
They can be accessed any where in the 
program except within a function. 
● To use these variables inside a function 
the variables must be declared global in 
that function. To do this we use the global 
keyword before the variables.
● <?php 
● $x=2; 
● $y=4; 
● $z=5; 
● $xyz=0; 
● function multiple() 
● { 
● global $x, $y, $z, $xyz; 
● $xyz=$x*$y*$z; 
● } 
● multiple(); 
● echo $xyz; 
● ?>
● In the above example $x, $y, $z, $xyz 
have initialized with 2, 4, 5, 0. Inside 
the multiple() function we declared $x, 
$y, $z, $xyz as global. Therefore all 
reference of each variable will refer to 
global version. Now call multiple() 
anywhere in the script and the 
variable $xyz will print 40 as it is 
already referred as global.
PHP static variables 
● Normally when a function terminates, all 
of its variables loose its values. 
Sometimes we want to hold these 
values for further job. Generally those 
variables which holds the values are 
called static variables inside a function. 
To do this we must write the keyword 
"static" in front of those variables. 
Consider the following example without 
static variable.
● <?php 
● function test_variable() 
● { 
● $x=1; 
● echo $x; 
● $x++; 
● } 
● test_variable(); 
● echo "<br>"; 
● test_variable(); 
● echo "<br>"; 
● test_variable(); 
● ?>
● In the above script the function 
test_count() is useless as the last 
statement $x = $x +1 can not increase 
the value of $x since every time it is 
called $x sets to 1 and print 1.
● 1 
● 1 
● 1
● <?php 
● function test_count() 
● { 
● static $x=1; 
● echo $x; 
● $x++; 
● } 
● test_count(); 
● echo "<br>"; 
● test_count(); 
● echo "<br>"; 
● test_count(); 
● ?>
● 1 
● 2 
● 3
The End 
●Questian?

More Related Content

PPT
PHP variables
PDF
Chap 4 PHP.pdf
PPTX
Form Handling using PHP
PPT
Php with MYSQL Database
PPT
PHP - Introduction to PHP Forms
PPTX
Lesson 2 php data types
PPT
Introduction to JavaScript
PPTX
Operators php
PHP variables
Chap 4 PHP.pdf
Form Handling using PHP
Php with MYSQL Database
PHP - Introduction to PHP Forms
Lesson 2 php data types
Introduction to JavaScript
Operators php

What's hot (20)

PDF
Php introduction
PDF
Introduction to php
PPTX
PHP FUNCTIONS
PPSX
Php and MySQL
PDF
Java I/o streams
PDF
4.2 PHP Function
PPTX
Introduction to php
PDF
jQuery for beginners
PPTX
PPT
Introduction to Javascript
PPTX
Event In JavaScript
PPT
Php Lecture Notes
PPTX
Php operators
PPTX
Introduction to ASP.NET
PPTX
Data types in php
PDF
Php Tutorials for Beginners
PPTX
JAVA-PPT'S.pptx
PPT
Introduction to PHP.ppt
PPSX
Javascript variables and datatypes
PPTX
Operators in java
Php introduction
Introduction to php
PHP FUNCTIONS
Php and MySQL
Java I/o streams
4.2 PHP Function
Introduction to php
jQuery for beginners
Introduction to Javascript
Event In JavaScript
Php Lecture Notes
Php operators
Introduction to ASP.NET
Data types in php
Php Tutorials for Beginners
JAVA-PPT'S.pptx
Introduction to PHP.ppt
Javascript variables and datatypes
Operators in java
Ad

Viewers also liked (20)

PPTX
Constructor and encapsulation in php
PPTX
OOPS Characteristics (With Examples in PHP)
PPT
Control Structures In Php 2
ODP
Htmltag.ppt
PDF
Arrays in PHP
PPS
Execute MySQL query using command prompt
PDF
What's new in PHP 7.1
PPSX
PHP Comprehensive Overview
PPT
PHP
ODP
Form Processing In Php
PPTX
State management
PPSX
Execute sql query or sql command sql server using command prompt
PPT
Php forms
PPTX
Cookie and session
PPTX
Constructors & destructors
PPT
Cookies and sessions
PPT
MySql slides (ppt)
PPTX
Constructor ppt
PPTX
Php Tutorial
Constructor and encapsulation in php
OOPS Characteristics (With Examples in PHP)
Control Structures In Php 2
Htmltag.ppt
Arrays in PHP
Execute MySQL query using command prompt
What's new in PHP 7.1
PHP Comprehensive Overview
PHP
Form Processing In Php
State management
Execute sql query or sql command sql server using command prompt
Php forms
Cookie and session
Constructors & destructors
Cookies and sessions
MySql slides (ppt)
Constructor ppt
Php Tutorial
Ad

Similar to Php variables (english) (20)

PPT
PHPVariables_075026.ppt
PPTX
Variable scope in php
PDF
Variables in PHP
PPTX
PHP Variables and scopes
PPTX
3 Introduction to PHP Variables Moduless
PPTX
Functions in PHP.pptx
PPTX
PHP BASICs Data Type ECHo and PRINT.pptx
PDF
Introduction to PHP_Slides by Lesley_Bonyo.pdf
PPTX
Variables
PPTX
Lesson 1 php syntax
PDF
Hsc IT 5. Server-Side Scripting (PHP).pdf
PPT
PHP Fuctions.ppt,IT CONTAINS PHP INCLUDE FUNCTION
PDF
php AND MYSQL _ppt.pdf
PPTX
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
PDF
Lecture2_IntroductionToPHP_Spring2023.pdf
ODP
Php Learning show
PPTX
PHP2An introduction to Gnome.pptx.j.pptx
PPTX
Introduction To PHP000000000000000000000000000000.pptx
PPTX
Lecture4 php by okello erick
PDF
03phpbldgblock
 
PHPVariables_075026.ppt
Variable scope in php
Variables in PHP
PHP Variables and scopes
3 Introduction to PHP Variables Moduless
Functions in PHP.pptx
PHP BASICs Data Type ECHo and PRINT.pptx
Introduction to PHP_Slides by Lesley_Bonyo.pdf
Variables
Lesson 1 php syntax
Hsc IT 5. Server-Side Scripting (PHP).pdf
PHP Fuctions.ppt,IT CONTAINS PHP INCLUDE FUNCTION
php AND MYSQL _ppt.pdf
PHPneweeeeeeeeeeeeeeeeeeeeeeeeeeeeee.pptx
Lecture2_IntroductionToPHP_Spring2023.pdf
Php Learning show
PHP2An introduction to Gnome.pptx.j.pptx
Introduction To PHP000000000000000000000000000000.pptx
Lecture4 php by okello erick
03phpbldgblock
 

Recently uploaded (20)

PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPT
Introduction Database Management System for Course Database
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
top salesforce developer skills in 2025.pdf
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Digital Strategies for Manufacturing Companies
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
ai tools demonstartion for schools and inter college
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Introduction Database Management System for Course Database
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Design an Analysis of Algorithms II-SECS-1021-03
Adobe Illustrator 28.6 Crack My Vision of Vector Design
2025 Textile ERP Trends: SAP, Odoo & Oracle
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
top salesforce developer skills in 2025.pdf
ManageIQ - Sprint 268 Review - Slide Deck
Odoo POS Development Services by CandidRoot Solutions
Digital Strategies for Manufacturing Companies
VVF-Customer-Presentation2025-Ver1.9.pptx
ai tools demonstartion for schools and inter college
Which alternative to Crystal Reports is best for small or large businesses.pdf
CHAPTER 2 - PM Management and IT Context
Softaken Excel to vCard Converter Software.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PTS Company Brochure 2025 (1).pdf.......
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 

Php variables (english)

  • 1. PHP Variables Write by: Mahmood masih tehrani Www.masihtehrani.ir tehrani@dabacenter.ir
  • 2. Declaring PHP variables ● All variables in PHP start with a $ (dollar) sign followed by the name of the variable. ● ● A valid variable name starts with a letter (A-Z, a-z) or underscore (_), followed by any number of letters, numbers, or underscores. ● ● If a variable name is more than one word, it can be separated with underscore (for example $employee_code instead of $employeecode). ● ● '$' is a special variable that can not be assigned.
  • 4. Valid and invalid PHP variables ● <?php ● $abc = 'Welcome'; //valid ● $Abc = 'W3resource.com'; //valid ● $9xyz = 'Hello world'; //invalid; starts with a number ● $_xyz = 'Hello world'; //valid; starts with an underscore ● $_9xyz = 'Hello world'; //valid ● $aĂ€a = 'Hello world'; //valid; 'Ă€' is (Extended) ASCII 228. ● ?>
  • 5. PHP variable name is case-sensitive ● <?php ● $abc = 'Welcome'; ● echo "Value of abc : $abc"; ● echo "Value of ABC : $ABC"; ● ?>
  • 6. ● <?php ● $height = 3.5; ● $width = 4; ● $area=$height*$width; ● echo "Area of the rectangle is : $area"; ● ?>
  • 7. PHP variables : Assigning by Reference ● <?php ● $foo='bob'; ● $bar=&$foo; ● $bar="my $bar"; ● echo $bar; ● echo '<br />'; ● echo $foo; ● ?>
  • 8. Output ● my bob ● my bob
  • 9. PHP variable variables ● <?php ● $v='var1'; ● echo $v; // prints var1 ● $$v = 'var2'; ● echo $$v; // prints var2 ● echo $var1; // prints var2 ● ?>
  • 10. PHP variable variables ● You know how to declare variables in PHP. But what if you want the name your variable is a variable itself? In PHP, you have Variable Variables, so you may assign a variable to another variable. ● In the following example at line no. 2, we declared a variable called $v which stores the value 'var1' and in line no. 4, "var1" is used as the name of a variable by using two dollar signs. i.e. $$v. ● Therefore there are two variables now. $v which stores the value "var1" where as $$v which stores the value var2. At this point $$v and $var1 are equal, both store the value "var2".
  • 11. PHP Variables Scope ● In PHP, variables can be declared anywhere in the script. We declare the variables for a particular scope. There are two types of scope,
  • 12. Example ● <?php ● //global scope ● $x = 10; ● function var_scope() ● { ● //local scope ● $y=20; ● echo "The value of x is : $x "."<br />"; ● echo "The value of y is : $y"."<br />"; ● } ● var_scope(); ● echo "The value of x is : $x"."<br />"; ● echo "The value of y is : $y "; ● ?>
  • 13. ● In the above script there are two variables $x and $y and a function var_scope(). $x is a global variable since it is declared outside the function and $y is a local variable as it is created inside the function var_scope(). At the end of the script var_scope() function is called, followed by two echo statements. Lets see the output of the script
  • 14. ● The value of x is : ● The value of y is : 20 ● The value of x is : 10 ● The value of y is :
  • 15. ● There are two echo statements inside var_scope() function. It prints the value of $y as it is the locally declared and can not prints the value of $x since it is created outside the function. ● ● The next statement of the script prints the value of $x since it is global variable i.e. not created inside any function. ● ● The last echo statement can not prints the value of $y since it is local variable and it is created inside the function var_scope() function.
  • 16. The global keyword ● We have already learned variables declared outside a function are global. They can be accessed any where in the program except within a function. ● To use these variables inside a function the variables must be declared global in that function. To do this we use the global keyword before the variables.
  • 17. ● <?php ● $x=2; ● $y=4; ● $z=5; ● $xyz=0; ● function multiple() ● { ● global $x, $y, $z, $xyz; ● $xyz=$x*$y*$z; ● } ● multiple(); ● echo $xyz; ● ?>
  • 18. ● In the above example $x, $y, $z, $xyz have initialized with 2, 4, 5, 0. Inside the multiple() function we declared $x, $y, $z, $xyz as global. Therefore all reference of each variable will refer to global version. Now call multiple() anywhere in the script and the variable $xyz will print 40 as it is already referred as global.
  • 19. PHP static variables ● Normally when a function terminates, all of its variables loose its values. Sometimes we want to hold these values for further job. Generally those variables which holds the values are called static variables inside a function. To do this we must write the keyword "static" in front of those variables. Consider the following example without static variable.
  • 20. ● <?php ● function test_variable() ● { ● $x=1; ● echo $x; ● $x++; ● } ● test_variable(); ● echo "<br>"; ● test_variable(); ● echo "<br>"; ● test_variable(); ● ?>
  • 21. ● In the above script the function test_count() is useless as the last statement $x = $x +1 can not increase the value of $x since every time it is called $x sets to 1 and print 1.
  • 22. ● 1 ● 1 ● 1
  • 23. ● <?php ● function test_count() ● { ● static $x=1; ● echo $x; ● $x++; ● } ● test_count(); ● echo "<br>"; ● test_count(); ● echo "<br>"; ● test_count(); ● ?>
  • 24. ● 1 ● 2 ● 3