Variables in PHP - What is Their Purpose?

PHP has functionality, officially called " Variable Variables ", where you can assign variable variables. Variable-variable takes the value of one variable as a name for a new variable! For instance:

$name='Joe'; $$name='Smith'; // could also be written as ${$name}='Smith' 

The first variable $ name contains the value "Joe", and the second is a variable named $ Joe with the value "Smith". Note that PHP variables are case sensitive!

I have never used this functionality and do not see it as a goal. Can someone explain to me where this functionality can be used as good practice?

+5
source share
4 answers

Sometimes we need software that is extremely flexible and that we can parameterize. Of course, you should prepare all this, but some of this only comes from user input, and we don’t have time to change the software just because the user needs a new login.

With variable variables and variable functions, you can solve problems that are much more difficult to solve with them.

Quick example:

Without variable variables:

 $comment = new stdClass(); // Create an object $comment->name = sanitize_value($array['name']); $comment->email = sanitize_values($array['email']); $comment->url = sanitize_values($array['url']); $comment->comment_text = sanitize_values($array['comment_text']); 

With variable variables

 $comment = new stdClass(); // Create a new object foreach( $array as $key=>$val ) { $comment->$key = sanitize_values($val); } 
+4
source

This has some uses when referencing variables inside classes, and there are some cases where they may actually be needed (for example, in __get() , __set() , __isset() and __unset() ). However, in most cases, they are not practical for global variables.

Remember, you should NEVER directly accept end-user input when it comes to variable variables. Instead, use the wrapper function to allow the use of only specific input.

In most cases, variable variables are not required, and it is recommended that you avoid them whenever possible.

0
source

You can use for something like

 $labels = array( 'first_name" => 'First Name", 'middle_name" => 'Middle Name", 'last_name" => 'Last Name", 'phone" => 'Phone"); foreach($labels as $field => $label) { echo '<div id='field'><label for='$field'>$label</label> <input id='$field' name='$field' type='text' value='".$$field."' /></div>"; } 

In my opinion, this is a bad old school ...

0
source

According to @Faiz's answer (which I accepted as a formal answer to my question), I created the following example.

If I had a Customer class:

 class Customer { public $firstname; public $lastname; public $country; public $gender; ... } 

and an HTML web form with INPUT / SELECT fields named 'firstname', 'lastname', 'country', 'gender' ...

 <form action="..." method="post"> <input type="text" name="firstname" value="" /> <input type="text" name="lastname" value="" /> <select name="country"> <option value="AL">Albania</option> <option value="BE">Belgium</option> <option value="HR">Croatia</option> ... </select> <input type="radio" name="gender" value="M" />Man <input type="radio" name="gender" value="F" />Woman ... <input type="submit" value="Submit" /> </form> 

usually in my script action I will map these form fields to class variables one at a time like:

 $Customer=new Customer(); $Customer->firstname=$_POST['firstname']; $Customer->lastname=$_POST['lastname']; $Customer->country=$_POST['country']; $Customer->gender=$_POST['gender']; ... $Customer->create(); 

But using variable variables, I can easily map all the values ​​of associative arrays (there may be many of them that are prone to errors) to class variables using the following foreach loop for the next line:

 $Customer=new Customer(); foreach($_POST as $key=>$value) $Customer->$key=$value; $Customer->create(); 

Note. For simplicity and clarity of the answer (logic), I omitted the sanation of the values ​​of $ _POST.

0
source

Source: https://habr.com/ru/post/1201507/


All Articles