note 127568 added to language.variables.variable
| From: | sebastopolys at gmail dot com | Date: | Sat, 03 Sep 2022 03:54:34 +0000 |
| Subject: | note 127568 added to language.variables.variable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219509@lists.php.net to get a copy of this message | ||
In addition, it is possible to use associative array to secure name of variables available to be
used within a function (or class / not tested).
This way the variable variable feature is useful to validate variables; define, output and manage
only within the function that receives as parameter
an associative array :
array('index'=>'value','index'=>'value');
index = reference to variable to be used within function
value = name of the variable to be used within function
<?php
$vars = ['id'=>'user_id','email'=>'user_email'];
validateVarsFunction($vars);
function validateVarsFunction($vars){
//$vars['id']=34; <- does not work
// define allowed variables
$user_id=21;
$user_email='email@mail.com';
echo $vars['id']; // prints name of variable: user_id
echo ${$vars['id']}; // prints 21
echo 'Email: '.${$vars['email']}; // print email@mail.com
// we don't have the name of the variables before declaring them inside the function
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.173 (proxied: 181.4.163.106)
----
Manual Page -- https://php.net/manual/en/language.variables.variable.php
Edit -- https://main.php.net/note/edit/127568
Del: integrated -- https://main.php.net/note/delete/127568/integrated
Del: useless -- https://main.php.net/note/delete/127568/useless
Del: bad code -- https://main.php.net/note/delete/127568/bad+code
Del: spam -- https://main.php.net/note/delete/127568/spam
Del: non-english -- https://main.php.net/note/delete/127568/non-english
Del: in docs -- https://main.php.net/note/delete/127568/in+docs
Del: other reasons-- https://main.php.net/note/delete/127568
Reject -- https://main.php.net/note/reject/127568
Search -- https://main.php.net/manage/user-notes.php