SlideShare una empresa de Scribd logo
1 de 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?

Más contenido relacionado

La actualidad más candente

Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04Spy Seat
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)WebStackAcademy
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHPShweta A
 
javascript objects
javascript objectsjavascript objects
javascript objectsVijay Kalyan
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#Dr.Neeraj Kumar Pandey
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - ObjectsWebStackAcademy
 
Data types in php
Data types in phpData types in php
Data types in phpilakkiya
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...Simplilearn
 
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 requireTheCreativedev Blog
 

La actualidad más candente (20)

Statements and Conditions in PHP
Statements and Conditions in PHPStatements and Conditions in PHP
Statements and Conditions in PHP
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
PHP - Introduction to PHP Forms
PHP - Introduction to PHP FormsPHP - Introduction to PHP Forms
PHP - Introduction to PHP Forms
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHP
 
jQuery
jQueryjQuery
jQuery
 
Php string function
Php string function Php string function
Php string function
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
Php operators
Php operatorsPhp operators
Php operators
 
Data Types In PHP
Data Types In PHPData Types In PHP
Data Types In PHP
 
Data types in php
Data types in phpData types in php
Data types in php
 
Javascript
JavascriptJavascript
Javascript
 
Php introduction
Php introductionPhp introduction
Php introduction
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
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
 

Destacado (20)

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)
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
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
 

Similar a Php variables (english)

Similar a 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
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
 
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
 

Último

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.comFatema Valibhai
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
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 ApplicationsAlberto González Trastoy
 
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...ICS
 
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 CCTVshikhaohhpro
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
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 ...OnePlan Solutions
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
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...MyIntelliSource, Inc.
 
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.pdfWave PLM
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
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 Modelsaagamshah0812
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
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.pdfkalichargn70th171
 
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 GoalsJhone kinadey
 

Último (20)

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
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
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
 
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...
 
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
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
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 ...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
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...
 
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
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
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
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
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
 
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
 

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