SlideShare une entreprise Scribd logo
1  sur  25
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.
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?

Contenu connexe

Tendances

Tendances (20)

Php variables
Php variablesPhp variables
Php variables
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
What Is Php
What Is PhpWhat Is Php
What Is Php
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
Php operators
Php operatorsPhp operators
Php operators
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
PHP
PHPPHP
PHP
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Operators php
Operators phpOperators php
Operators php
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
Php array
Php arrayPhp array
Php array
 
Files in php
Files in phpFiles in php
Files in php
 
Php
PhpPhp
Php
 
Sessions and cookies in php
Sessions and cookies in phpSessions and cookies in php
Sessions and cookies in php
 
Statements and Conditions in PHP
Statements and Conditions in PHPStatements and Conditions in PHP
Statements and Conditions in PHP
 

En vedette

Execute MySQL query using command prompt
Execute MySQL query using command promptExecute MySQL query using command prompt
Execute MySQL query using command prompt
Ikhwan Krisnadi
 
Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In Php
Harit Kothari
 
State management
State managementState management
State management
Iblesoft
 

En vedette (19)

Font
FontFont
Font
 
Constructor and encapsulation in php
Constructor and encapsulation in phpConstructor and encapsulation in php
Constructor and encapsulation in php
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
Htmltag.ppt
Htmltag.pptHtmltag.ppt
Htmltag.ppt
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Execute MySQL query using command prompt
Execute MySQL query using command promptExecute MySQL query using command prompt
Execute MySQL query using command prompt
 
What's new in PHP 7.1
What's new in PHP 7.1What's new in PHP 7.1
What's new in PHP 7.1
 
PHP Comprehensive Overview
PHP Comprehensive OverviewPHP Comprehensive Overview
PHP Comprehensive Overview
 
PHP
PHPPHP
PHP
 
Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In Php
 
State management
State managementState management
State management
 
Execute sql query or sql command sql server using command prompt
Execute sql query or sql command sql server using command promptExecute sql query or sql command sql server using command prompt
Execute sql query or sql command sql server using command prompt
 
Php forms
Php formsPhp forms
Php forms
 
Cookie and session
Cookie and sessionCookie and session
Cookie and session
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 

Similaire à Php variables (english)

P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
phelios
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)
Core Lee
 

Similaire à Php variables (english) (20)

unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Functions in PHP.pptx
Functions in PHP.pptxFunctions in PHP.pptx
Functions in PHP.pptx
 
OpenGurukul : Language : PHP
OpenGurukul : Language : PHPOpenGurukul : Language : PHP
OpenGurukul : Language : PHP
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
basics of php
 basics of php basics of php
basics of php
 
Variables
VariablesVariables
Variables
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
PHPVariables_075026.ppt
PHPVariables_075026.pptPHPVariables_075026.ppt
PHPVariables_075026.ppt
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
 
Variables in PHP
Variables in PHPVariables in PHP
Variables in PHP
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Java script
Java scriptJava script
Java script
 
Chap 4 PHP.pdf
Chap 4 PHP.pdfChap 4 PHP.pdf
Chap 4 PHP.pdf
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)
 
Expressions and Operators.pptx
Expressions and Operators.pptxExpressions and Operators.pptx
Expressions and Operators.pptx
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to java
 

Dernier

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 

Dernier (20)

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 

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.
  • 3.
  • 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