SlideShare a Scribd company logo
PHP Variable Variables
Examples
Abdul Rahman Sherzad
Computer Science Lecturer
Herat University, Afghanistan
Definition
■ Variable variables take the value
of one variable, and treat that as
the name of another variable.
■ Variable variables are just
variables whose names can be
programmatically set and
accessed. Hence, they are also
called dynamic variable names.
2
Example 1
■ Employees share similar name. Hence, all
employees are given a unique identifier (ID, aka
code) by organizations. This unique code is
important for the organizations to distinguish their
employees.
■ Likewise, educational institutions assign a unique
code for their students.
■ This unique code is very important for the system,
but it alone is not very informative in reports.
■ Variable variables can be used in such situations to
cover both. Following code creates a variable called
$S20120, and outputs the string "Abdul Rahman
Sherzad".
3
Example 2
■ It is also possible to use value of a
variable/literal value as part of variable
variables names.
■ Of course, there are many other easy
ways to accomplish the functionality of
this program. But this is to explain
variable variables concept through
examples.
4
Example 2 - Array
■ This is another method to
accomplish Example 2 in the
previous slide using array.
5
Example 3
■ Variable variables can be used to
dynamically generate variables from an
associative array.
■ The PHP extract() built-in function also
takes an associative array and treats the
keys as variable names and the values
as variable values, (see next slide).
■ This example is to describe and
demonstrate the variable variables
applications, not reinventing the wheel.
6
Example 3 - Extract
■ The PHP extract() function takes an
associative array and treats the keys as
variable names and the values as
variable values.
■ The extract() has useful functionality
such as to prevent variables that already
exist.
■ Also it enables one to group the variables
by using prefixes too.
7
Example 4
■ Use variable variables to convert POST
variables to local variables, as illustrated
in the example.
■ This technique has several benefits.
– Lines of code are reduced,
– Central place to clean and sanitize
all external input,
– Easy change and modification of
variable names.
8
Example 5
■ PHP variable variables can simulate
Microsoft Excel’s indirect() function to
create dependent drop-down lists.
■ In this example, it is shown how to create
dependent drop-down lists, using
variable variables names.
■ Of course, there are better ways to
accomplish creating dependent drop-
down lists. In this presentation, the
purpose is to show the application and
example of variable variables in PHP.
9
Example 5 - complete
10
11

More Related Content

Similar to PHP Variable variables Examples (20)

PDF
C Programming - Refresher - Part II
Emertxe Information Technologies Pvt Ltd
 
PPTX
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
rishiskj
 
PPTX
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
Indu65
 
PDF
Chapter4__Method_Lim Lyheng_RULE (2).pdf
MornThea
 
PPTX
GUI Programming in JAVA (Using Netbeans) - A Review
Fernando Torres
 
PDF
Handout # 4 functions + scopes
NUST Stuff
 
PPT
Ap Power Point Chpt4
dplunkett
 
PPTX
07 java variables
Zeeshan-Shaikh
 
PPTX
PL_SQL - II.pptx
priyaprakash11
 
PPTX
JAVA programming language made easy.pptx
Sunila31
 
PDF
Annotations in Java with Example.pdf
SudhanshiBakre1
 
PPT
Object Oriented Analysis and Design with UML2 part2
Haitham Raik
 
PPT
Methods in C#
Prasanna Kumar SM
 
PPTX
PHP Basics
Muthuganesh S
 
PDF
Web Technology-Method .pdf
nandiaditi2010
 
PDF
Object Oriented Principles
Sujit Majety
 
PPTX
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
PPTX
Mule data weave_2
kunal vishe
 
PPTX
E learning excel vba programming lesson 3
Vijay Perepa
 
C Programming - Refresher - Part II
Emertxe Information Technologies Pvt Ltd
 
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
rishiskj
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
Indu65
 
Chapter4__Method_Lim Lyheng_RULE (2).pdf
MornThea
 
GUI Programming in JAVA (Using Netbeans) - A Review
Fernando Torres
 
Handout # 4 functions + scopes
NUST Stuff
 
Ap Power Point Chpt4
dplunkett
 
07 java variables
Zeeshan-Shaikh
 
PL_SQL - II.pptx
priyaprakash11
 
JAVA programming language made easy.pptx
Sunila31
 
Annotations in Java with Example.pdf
SudhanshiBakre1
 
Object Oriented Analysis and Design with UML2 part2
Haitham Raik
 
Methods in C#
Prasanna Kumar SM
 
PHP Basics
Muthuganesh S
 
Web Technology-Method .pdf
nandiaditi2010
 
Object Oriented Principles
Sujit Majety
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
Mule data weave_2
kunal vishe
 
E learning excel vba programming lesson 3
Vijay Perepa
 

More from Abdul Rahman Sherzad (20)

PDF
Data is the Fuel of Organizations: Opportunities and Challenges in Afghanistan
Abdul Rahman Sherzad
 
PDF
PHP Unicode Input Validation Snippets
Abdul Rahman Sherzad
 
PDF
Iterations and Recursions
Abdul Rahman Sherzad
 
PDF
Sorting Alpha Numeric Data in MySQL
Abdul Rahman Sherzad
 
PDF
Cross Join Example and Applications
Abdul Rahman Sherzad
 
PDF
Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...
Abdul Rahman Sherzad
 
PDF
Web Application Security and Awareness
Abdul Rahman Sherzad
 
PDF
Database Automation with MySQL Triggers and Event Schedulers
Abdul Rahman Sherzad
 
PDF
Mobile Score Notification System
Abdul Rahman Sherzad
 
PDF
Herat Innovation Lab 2015
Abdul Rahman Sherzad
 
PDF
Evaluation of Existing Web Structure of Afghan Universities
Abdul Rahman Sherzad
 
PDF
PHP Basic and Fundamental Questions and Answers with Detail Explanation
Abdul Rahman Sherzad
 
PDF
Java Applet and Graphics
Abdul Rahman Sherzad
 
PDF
Fundamentals of Database Systems Questions and Answers
Abdul Rahman Sherzad
 
PDF
Everything about Database JOINS and Relationships
Abdul Rahman Sherzad
 
PDF
Create Splash Screen with Java Step by Step
Abdul Rahman Sherzad
 
PDF
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Abdul Rahman Sherzad
 
PDF
Web Design and Development Life Cycle and Technologies
Abdul Rahman Sherzad
 
PDF
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Abdul Rahman Sherzad
 
PDF
Java Unicode with Live GUI Examples
Abdul Rahman Sherzad
 
Data is the Fuel of Organizations: Opportunities and Challenges in Afghanistan
Abdul Rahman Sherzad
 
PHP Unicode Input Validation Snippets
Abdul Rahman Sherzad
 
Iterations and Recursions
Abdul Rahman Sherzad
 
Sorting Alpha Numeric Data in MySQL
Abdul Rahman Sherzad
 
Cross Join Example and Applications
Abdul Rahman Sherzad
 
Applicability of Educational Data Mining in Afghanistan: Opportunities and Ch...
Abdul Rahman Sherzad
 
Web Application Security and Awareness
Abdul Rahman Sherzad
 
Database Automation with MySQL Triggers and Event Schedulers
Abdul Rahman Sherzad
 
Mobile Score Notification System
Abdul Rahman Sherzad
 
Herat Innovation Lab 2015
Abdul Rahman Sherzad
 
Evaluation of Existing Web Structure of Afghan Universities
Abdul Rahman Sherzad
 
PHP Basic and Fundamental Questions and Answers with Detail Explanation
Abdul Rahman Sherzad
 
Java Applet and Graphics
Abdul Rahman Sherzad
 
Fundamentals of Database Systems Questions and Answers
Abdul Rahman Sherzad
 
Everything about Database JOINS and Relationships
Abdul Rahman Sherzad
 
Create Splash Screen with Java Step by Step
Abdul Rahman Sherzad
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Abdul Rahman Sherzad
 
Web Design and Development Life Cycle and Technologies
Abdul Rahman Sherzad
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Abdul Rahman Sherzad
 
Java Unicode with Live GUI Examples
Abdul Rahman Sherzad
 
Ad

Recently uploaded (20)

PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Ad

PHP Variable variables Examples

  • 1. PHP Variable Variables Examples Abdul Rahman Sherzad Computer Science Lecturer Herat University, Afghanistan
  • 2. Definition ■ Variable variables take the value of one variable, and treat that as the name of another variable. ■ Variable variables are just variables whose names can be programmatically set and accessed. Hence, they are also called dynamic variable names. 2
  • 3. Example 1 ■ Employees share similar name. Hence, all employees are given a unique identifier (ID, aka code) by organizations. This unique code is important for the organizations to distinguish their employees. ■ Likewise, educational institutions assign a unique code for their students. ■ This unique code is very important for the system, but it alone is not very informative in reports. ■ Variable variables can be used in such situations to cover both. Following code creates a variable called $S20120, and outputs the string "Abdul Rahman Sherzad". 3
  • 4. Example 2 ■ It is also possible to use value of a variable/literal value as part of variable variables names. ■ Of course, there are many other easy ways to accomplish the functionality of this program. But this is to explain variable variables concept through examples. 4
  • 5. Example 2 - Array ■ This is another method to accomplish Example 2 in the previous slide using array. 5
  • 6. Example 3 ■ Variable variables can be used to dynamically generate variables from an associative array. ■ The PHP extract() built-in function also takes an associative array and treats the keys as variable names and the values as variable values, (see next slide). ■ This example is to describe and demonstrate the variable variables applications, not reinventing the wheel. 6
  • 7. Example 3 - Extract ■ The PHP extract() function takes an associative array and treats the keys as variable names and the values as variable values. ■ The extract() has useful functionality such as to prevent variables that already exist. ■ Also it enables one to group the variables by using prefixes too. 7
  • 8. Example 4 ■ Use variable variables to convert POST variables to local variables, as illustrated in the example. ■ This technique has several benefits. – Lines of code are reduced, – Central place to clean and sanitize all external input, – Easy change and modification of variable names. 8
  • 9. Example 5 ■ PHP variable variables can simulate Microsoft Excel’s indirect() function to create dependent drop-down lists. ■ In this example, it is shown how to create dependent drop-down lists, using variable variables names. ■ Of course, there are better ways to accomplish creating dependent drop- down lists. In this presentation, the purpose is to show the application and example of variable variables in PHP. 9
  • 10. Example 5 - complete 10
  • 11. 11