PHP Variable Variables (original) (raw)
Summary: in this tutorial, you’ll learn about PHP variable variables and how to apply them effectively.
Introduction to the PHP variable variables #
Typically, you have a variable with a predefined name. For example, the following defines a variable with the name $title that holds a string.
`<?php
$title = 'PHP variable variables';`Code language: PHP (php)
In PHP, the name of a variable can be derived from the value of another variable. For example:
`<?php
$my_var= 'title'; my_var = 'PHP variable variables';
echo $title;`Code language: PHP (php)
Output:
PHP variable variables
Code language: PHP (php)
How it works.
- First, define a variable
$my_var
that holds the string'title'
. - Second, define a variable variable that holds the string
'PHP variable variables'
. Note that we use double$
signs instead of one. By doing this, we technically create another variable with the name$title
. - Third, display the value of the
$title
variable.
Suppose that you have the following folder and files:
├── inc | └── home.php └── index.php
Code language: PHP (php)
In the index.php
, you define the following view()
function that loads the code from a file specified by the $file
parameter:
`<?php
function view(string $file): void { require DIR . $file; }`Code language: PHP (php)
To pass the data to the script specified by the $file
, you can add a second parameter to the view()
function like this:
`<?php
function view(string file,arrayfile, array file,arraydata): void { require DIR . $file; }`Code language: PHP (php)
From the $file
script, you can access the elements of the $data
array.
The following example uses the view()
function to load the code from the home.php
script and pass an array of two elements to it:
`<?php
function view(string file,arrayfile, array file,arraydata): void { require DIR . '/' . $file; }
view( 'inc/home.php', [ 'title' => 'Home', 'heading' => 'Welcome to my homepage' ] );`Code language: PHP (php)
In the home.php
, you can access the $data
array like this:
`
<?php echo $data['title'] ?>/title> </head> <body> <h1><?php echo $data['heading'] ?></h1> </body> </html>`Code language: PHP (php) <p>When you launch the <code>index.php</code> file, it loads the code from the <code>home.php</code> file and displays the title and heading. However, it would be great if you can access the elements of the <code>$data</code> array in the <code>home.php</code> like this:</p> <p>`<!DOCTYPE html></p> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?php echo $title ?>/title> </head> <body> <h1><?php echo $heading ?></h1> </body> </html>`Code language: PHP (php) <p>To do that, you need to transform the array elements into variables using the variable variables. The new <code>view()</code> function will look like this:</p> <p>`function view(string <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>i</mi><mi>l</mi><mi>e</mi><mo separator="true">,</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><mi>y</mi></mrow><annotation encoding="application/x-tex">file, array </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span></span></span></span>data): void { foreach ($data as <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi><mi>e</mi><mi>y</mi><mo>=</mo><mo>></mo></mrow><annotation encoding="application/x-tex">key => </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="mord mathnormal" style="margin-right:0.03588em;">ey</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=></span></span></span></span>value) { <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>key = $value; }</p> <pre><code class="notranslate">require __DIR__ . '/' . $file;</code></pre><p>}`Code language: PHP (php)</p> <p>In the <code>view()</code> function, we iterate over the elements of the <code>$data</code> array and create variables whose names are the keys of the <code>$data</code> array and values are the values of the <code>$data</code> array:</p> <p><code>foreach ($data as <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>k</mi><mi>e</mi><mi>y</mi><mo>=</mo><mo>></mo></mrow><annotation encoding="application/x-tex">key => </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="mord mathnormal" style="margin-right:0.03588em;">ey</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=></span></span></span></span>value) { <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow></mrow><annotation encoding="application/x-tex"></annotation></semantics></math></span><span class="katex-html" aria-hidden="true"></span></span>key = $value; }</code>Code language: PHP (php)</p> <p>The variable variables are now available in the script specified by the <code>$file</code> parameter. For example, if you call the <code>view()</code> function to load the code from the <code>home.php</code> file as follows:</p> <p><code>view( 'inc/home.php', [ 'title' => 'Home', 'heading' => 'Welcome to my homepage' ] );</code>Code language: PHP (php)</p> <p>In the <code>home.php</code> file, you can access the <code>$title</code> and <code>$heading</code> variables:</p> <p>`<!DOCTYPE html></p> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?php echo $title ?>/title> </head> <body> <h1><?php echo $heading ?></h1> </body> </html>`Code language: PHP (php) <h2 id="summary-"><a class="anchor" aria-hidden="true" tabindex="-1" href="#summary-"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Summary <a href="#summary" title="Anchor for Summary">#</a></h2><ul> <li>PHP variable variables are variables whose names are set dynamically.</li> </ul> <p>Did you find this tutorial useful?</p>