SlideShare uma empresa 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?

Mais conteúdo relacionado

Mais procurados

JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic FunctionsWebStackAcademy
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentationritika1
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP StringsAhmed Swilam
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An IntroductionManvendra Singh
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and FunctionsJussi Pohjolainen
 
Java Data Types and Variables
Java Data Types and VariablesJava Data Types and Variables
Java Data Types and Variablessasi saseenthiran
 
Html5 and-css3-overview
Html5 and-css3-overviewHtml5 and-css3-overview
Html5 and-css3-overviewJacob Nelson
 
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
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and SessionsNisa Soomro
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingHaim Michael
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntaxDhani Ahmad
 

Mais procurados (20)

JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and Functions
 
Java Data Types and Variables
Java Data Types and VariablesJava Data Types and Variables
Java Data Types and Variables
 
java token
java tokenjava token
java token
 
Operators php
Operators phpOperators php
Operators php
 
Html5 and-css3-overview
Html5 and-css3-overviewHtml5 and-css3-overview
Html5 and-css3-overview
 
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
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Php basics
Php basicsPhp basics
Php basics
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript Programming
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Modern JS with ES6
Modern JS with ES6Modern JS with ES6
Modern JS with ES6
 

Destaque (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)
 
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
 
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
 

Semelhante a Php variables (english)

Semelhante 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
 
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 Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
 
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
 

Último

Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
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
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
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...panagenda
 
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
 
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
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
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
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
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 🔝✔️✔️Delhi Call girls
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
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
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
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 🔝✔️✔️Delhi Call girls
 
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-...Steffen Staab
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 

Último (20)

Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
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
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
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...
 
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
 
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
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
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
 
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
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
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 🔝✔️✔️
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
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 ...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
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 🔝✔️✔️
 
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-...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 

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