PHP variables

Asked

Viewed 146 times

-1

So I was studying a code here, and I came across a variable variable.

I have heard a lot about this concept but so far I still do not understand what they are for, when we should use etc..

So my questions are:

  • What are variables of variables and what they serve?

  • We must use variable variables?, if yes in what situations?

  • Variable variables, have some effect on php application performance ?

If possible with examples.

  • 3
  • 3
  • I had already looked before publishing but I think it does not cover all my questions

  • 1

    I believe all doubts are resolved in the link @Articuno posted. Anyway, I preferred to vote to close as broad and not as duplicate because I understand that the question escapes a little from the proposal of the site (many doubts in a post only). Here is the suggested reading of the [tour]. [Ask], and then [Edit] the question to leave it only with the specific question, if there really is something unsatisfied in the other posts. Remember that when you have questions, you can ask about one part at a time, and post new questions when the previous one is resolved.

  • 1

    Possible duplicate of What is a scalar variable?

1 answer

3


Good believe you mean variable variable and not variable variable

  • What are variables of variables and what they serve?

A variable name that can be configured and used dynamically. A variable takes the value of a variable and treats it as the name of a variable.

  • Should we use variable variables? , if yes in what situations?

They are most useful when accessing values within an array property, when the property name is made of multiple parts, or when the property name contains characters that are not valid (e.g., from json_decode() or Simplexml ).

I think the best example to understand its use is this:

<?php 
$varname = "foo"; 
$foo = "bar"; 

print $$varname;  // Prints "bar" 
print "$$varname";  // Prints "$foo" 
print "${$varname}"; // Prints "bar" 
?>

And for me at least, the greatest utility it has is when vc has compound variable names, but that have part of the same name referring to something, so vc puts only a list of the differentiation of these variables in a vector and with a foreach list all, as shown in the example below:

<?php
// Given these variables ...
$nameTypes    = array("first", "last", "company");
$name_first   = "John";
$name_last    = "Doe";
$name_company = "PHP.net";

// Then this loop is ...
foreach($nameTypes as $type)
  print ${"name_$type"} . "\n";

// ... equivalent to this print statement.
print "$name_first\n$name_last\n$name_company\n";
?>
  • Variable variables, have some effect on php application performance ?

Look if there is improvement in the performance I do not know, but I believe that not, what I know is that it has optimization of code as demonstrated in the previous example.

All examples and exceptions you can find in the official documentation: http://php.net/manual/en/language.variables.variable.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.