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

What's hot

Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
CPD INDIA
 
PHP - Introduction to PHP AJAX
PHP -  Introduction to PHP AJAXPHP -  Introduction to PHP AJAX
PHP - Introduction to PHP AJAX
Vibrant Technologies & Computers
 
Client Server models in JAVA
Client Server models in JAVAClient Server models in JAVA
Client Server models in JAVATech_MX
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
Priya Goyal
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
Manisha Keim
 
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
M.Zalmai Rahmani
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
ShahDhruv21
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript Programming
Haim Michael
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
Mohammed Arif
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
Php
PhpPhp
Html5 for mobiles
Html5 for mobilesHtml5 for mobiles
Html5 for mobiles
Christian Glahn
 
Format String Attack
Format String AttackFormat String Attack
Format String Attack
Mayur Mallya
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
Zeeshan Ahmed
 

What's hot (20)

Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
PHP - Introduction to PHP AJAX
PHP -  Introduction to PHP AJAXPHP -  Introduction to PHP AJAX
PHP - Introduction to PHP AJAX
 
Client Server models in JAVA
Client Server models in JAVAClient Server models in JAVA
Client Server models in JAVA
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript Programming
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
Php
PhpPhp
Php
 
Html5 for mobiles
Html5 for mobilesHtml5 for mobiles
Html5 for mobiles
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Format String Attack
Format String AttackFormat String Attack
Format String Attack
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
Php basics
Php basicsPhp basics
Php basics
 

Viewers also liked

Font
FontFont
Constructor and encapsulation in php
Constructor and encapsulation in phpConstructor and encapsulation in php
Constructor and encapsulation in php
SHIVANI SONI
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
baabtra.com - No. 1 supplier of quality freshers
 
Execute MySQL query using command prompt
Execute MySQL query using command promptExecute MySQL query using command prompt
Execute MySQL query using command promptIkhwan Krisnadi
 
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
Simon Jones
 
PHP Comprehensive Overview
PHP Comprehensive OverviewPHP Comprehensive Overview
PHP Comprehensive Overview
Mohamed Loey
 
PHP
PHPPHP
Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In PhpHarit Kothari
 
State management
State managementState management
State managementIblesoft
 
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
Ikhwan Krisnadi
 
Php forms
Php formsPhp forms
Php forms
Anne Lee
 
Cookie and session
Cookie and sessionCookie and session
Cookie and session
Aashish Ghale
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
Lena Petsenchuk
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
pratik tambekar
 

Viewers also liked (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 to Php variables (english)

unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
adityathote3
 
Php introduction
Php introductionPhp introduction
Php introduction
Pratik Patel
 
Functions in PHP.pptx
Functions in PHP.pptxFunctions in PHP.pptx
Functions in PHP.pptx
Japneet9
 
OpenGurukul : Language : PHP
OpenGurukul : Language : PHPOpenGurukul : Language : PHP
OpenGurukul : Language : PHP
Open Gurukul
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Muthuganesh S
 
basics of php
 basics of php basics of php
basics of php
PRINCE KUMAR
 
Variables
VariablesVariables
Variables
Suraj Motee
 
PHPVariables_075026.ppt
PHPVariables_075026.pptPHPVariables_075026.ppt
PHPVariables_075026.ppt
06Vinit
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
Khem Puthea
 
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 Kianphelios
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
McSoftsis
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
SVN Polytechnic Kalan Sultanpur UP
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
Vineet Kumar Saini
 
Java script
Java scriptJava script
Java script
Shyam Khant
 
Chap 4 PHP.pdf
Chap 4 PHP.pdfChap 4 PHP.pdf
Chap 4 PHP.pdf
HASENSEID
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
Yoeung Vibol
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)Core Lee
 
Expressions and Operators.pptx
Expressions and Operators.pptxExpressions and Operators.pptx
Expressions and Operators.pptx
Japneet9
 

Similar to 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
 

Recently uploaded

2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 

Recently uploaded (20)

2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 

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