SlideShare ist ein Scribd-Unternehmen logo
1 von 11
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Java Chapter 04 - Writing Classes: part 1
Java Chapter 04 - Writing Classes: part 1Java Chapter 04 - Writing Classes: part 1
Java Chapter 04 - Writing Classes: part 1
DanWooster1
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
Ravi_Kant_Sahu
 
La5 Basicelement
La5 BasicelementLa5 Basicelement
La5 Basicelement
Cma Mohd
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbms
SANTOSH RATH
 

Was ist angesagt? (19)

Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
 
Xamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismXamarin: Inheritance and Polymorphism
Xamarin: Inheritance and Polymorphism
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
 
Java Chapter 04 - Writing Classes: part 1
Java Chapter 04 - Writing Classes: part 1Java Chapter 04 - Writing Classes: part 1
Java Chapter 04 - Writing Classes: part 1
 
Normalisation
NormalisationNormalisation
Normalisation
 
Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
La5 Basicelement
La5 BasicelementLa5 Basicelement
La5 Basicelement
 
Accessing non static members from the main
Accessing non static members from the mainAccessing non static members from the main
Accessing non static members from the main
 
Member Function in C++
Member Function in C++ Member Function in C++
Member Function in C++
 
Java Interface
Java InterfaceJava Interface
Java Interface
 
C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
Vb decision making statements
Vb decision making statementsVb decision making statements
Vb decision making statements
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbms
 
Bai giang-uml-21jan14
Bai giang-uml-21jan14Bai giang-uml-21jan14
Bai giang-uml-21jan14
 
Oracle: Control Structures
Oracle: Control StructuresOracle: Control Structures
Oracle: Control Structures
 
Definition of automation,finite automata,transition system
Definition of automation,finite automata,transition systemDefinition of automation,finite automata,transition system
Definition of automation,finite automata,transition system
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
 

Ähnlich wie PHP Variable variables Examples

INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
Indu65
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
dplunkett
 

Ähnlich wie PHP Variable variables Examples (20)

CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
 
Chapter4__Method_Lim Lyheng_RULE (2).pdf
Chapter4__Method_Lim Lyheng_RULE (2).pdfChapter4__Method_Lim Lyheng_RULE (2).pdf
Chapter4__Method_Lim Lyheng_RULE (2).pdf
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A Review
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
 
07 java variables
07   java variables07   java variables
07 java variables
 
PL_SQL - II.pptx
PL_SQL - II.pptxPL_SQL - II.pptx
PL_SQL - II.pptx
 
Annotations in Java with Example.pdf
Annotations in Java with Example.pdfAnnotations in Java with Example.pdf
Annotations in Java with Example.pdf
 
Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Php
PhpPhp
Php
 
Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practices
 
Object Oriented Principles
Object Oriented PrinciplesObject Oriented Principles
Object Oriented Principles
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
Mule data weave_2
Mule data weave_2Mule data weave_2
Mule data weave_2
 
E learning excel vba programming lesson 3
E learning excel vba programming  lesson 3E learning excel vba programming  lesson 3
E learning excel vba programming lesson 3
 
Operators
OperatorsOperators
Operators
 

Mehr von Abdul Rahman Sherzad

Web Application Security and Awareness
Web Application Security and AwarenessWeb Application Security and Awareness
Web Application Security and Awareness
Abdul Rahman Sherzad
 
Database Automation with MySQL Triggers and Event Schedulers
Database Automation with MySQL Triggers and Event SchedulersDatabase Automation with MySQL Triggers and Event Schedulers
Database Automation with MySQL Triggers and Event Schedulers
Abdul Rahman Sherzad
 

Mehr von Abdul Rahman Sherzad (20)

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

Kürzlich hochgeladen

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Kürzlich hochgeladen (20)

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
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
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
 
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
 
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
 
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
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
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...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
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
 
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
 
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
 
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
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
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 ...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 

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