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

PDF
Chap 4 PHP.pdf
PDF
Php introduction
PDF
Introduction to php
PPTX
Polymorphism presentation in java
PPT
Php with MYSQL Database
PPTX
Form Handling using PHP
PDF
Node Architecture and Getting Started with Express
PPTX
All about decision making statements in php
Chap 4 PHP.pdf
Php introduction
Introduction to php
Polymorphism presentation in java
Php with MYSQL Database
Form Handling using PHP
Node Architecture and Getting Started with Express
All about decision making statements in php

What's hot (20)

PPT
01 Php Introduction
PPTX
Abstraction and Encapsulation
PDF
Polymorphism In Java
PPTX
Php with mysql ppt
PPTX
Polymorphism in java
PPTX
Preprocessor
PPT
PHP Workshop Notes
PPTX
Files in php
PPTX
php basics
PPT
Oops concepts in php
PPSX
JDBC: java DataBase connectivity
PPTX
Modos de direccionamiento!!
 
PPTX
Introduction to php
PPT
Inheritance and Polymorphism
PPTX
Looping statement
PPTX
Introduction to Node js
PPT
PHP - Introduction to PHP Date and Time Functions
PPT
Introduction to Javascript
PPT
Introduction To PHP
PPTX
Inheritance in java
01 Php Introduction
Abstraction and Encapsulation
Polymorphism In Java
Php with mysql ppt
Polymorphism in java
Preprocessor
PHP Workshop Notes
Files in php
php basics
Oops concepts in php
JDBC: java DataBase connectivity
Modos de direccionamiento!!
 
Introduction to php
Inheritance and Polymorphism
Looping statement
Introduction to Node js
PHP - Introduction to PHP Date and Time Functions
Introduction to Javascript
Introduction To PHP
Inheritance in java
Ad

Similar to PHP Variable variables Examples (20)

PDF
Variables in PHP
PPTX
3 Introduction to PHP Variables Moduless
ODP
Php variables (english)
PPT
PHPVariables_075026.ppt
PPTX
PHP Variables and scopes
PPTX
PHP BASICs Data Type ECHo and PRINT.pptx
PDF
Lecture2_IntroductionToPHP_Spring2023.pdf
PPTX
Variables
PDF
Introduction to PHP_Slides by Lesley_Bonyo.pdf
PPTX
Php by shivitomer
PPTX
PHP FUNCTIONS AND ARRAY.pptx
PPTX
Lecture4 php by okello erick
PPTX
Functions in PHP.pptx
PDF
Hsc IT 5. Server-Side Scripting (PHP).pdf
PDF
03phpbldgblock
 
PPTX
PHP Introduction and Training Material
PPT
PHP Scripting
PPTX
PHP Training Part1
PDF
Introduction to PHP
PDF
phptutorial
Variables in PHP
3 Introduction to PHP Variables Moduless
Php variables (english)
PHPVariables_075026.ppt
PHP Variables and scopes
PHP BASICs Data Type ECHo and PRINT.pptx
Lecture2_IntroductionToPHP_Spring2023.pdf
Variables
Introduction to PHP_Slides by Lesley_Bonyo.pdf
Php by shivitomer
PHP FUNCTIONS AND ARRAY.pptx
Lecture4 php by okello erick
Functions in PHP.pptx
Hsc IT 5. Server-Side Scripting (PHP).pdf
03phpbldgblock
 
PHP Introduction and Training Material
PHP Scripting
PHP Training Part1
Introduction to PHP
phptutorial
Ad

More from Abdul Rahman Sherzad (20)

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

Recently uploaded (20)

PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Nekopoi APK 2025 free lastest update
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Online Work Permit System for Fast Permit Processing
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
history of c programming in notes for students .pptx
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Transform Your Business with a Software ERP System
PDF
Digital Strategies for Manufacturing Companies
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Introduction to Artificial Intelligence
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
Wondershare Filmora 15 Crack With Activation Key [2025
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Nekopoi APK 2025 free lastest update
Operating system designcfffgfgggggggvggggggggg
Online Work Permit System for Fast Permit Processing
2025 Textile ERP Trends: SAP, Odoo & Oracle
Which alternative to Crystal Reports is best for small or large businesses.pdf
history of c programming in notes for students .pptx
How Creative Agencies Leverage Project Management Software.pdf
Odoo Companies in India – Driving Business Transformation.pdf
Transform Your Business with a Software ERP System
Digital Strategies for Manufacturing Companies
Upgrade and Innovation Strategies for SAP ERP Customers
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
Softaken Excel to vCard Converter Software.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Introduction to Artificial Intelligence
Odoo POS Development Services by CandidRoot Solutions
Internet Downloader Manager (IDM) Crack 6.42 Build 41
VVF-Customer-Presentation2025-Ver1.9.pptx

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