variable-variables

PHP and variable variables ($$) syntax

一曲冷凌霜 提交于 2020-01-25 18:26:34
问题 Before upgrading to PHP 7, I had this code and it returned true var_dump(isset($$_SESSION['payment']) ); var_dump(is_object($$_SESSION['payment'])); var_dump($_SESSION['payment']); // string 'moneyorder' After upgrading to PHP 7, I rewrote the same code inside a class, but now it returns false var_dump(isset(${$_SESSION['payment']})); var_dump(is_object(${$_SESSION['payment']})); var_dump($_SESSION['payment']); // string 'moneyorder' Do you have an idea why ? Thank you 回答1: Note the PHP

javascript equivalent of php $$ dollar dollar

百般思念 提交于 2020-01-14 03:12:08
问题 I have declared a local variable named cont in a function named validate. I am calling a function process from inside validate. I am sending the string 'cont' as argument to validate function. In the process function using the string 'cont' i want to access the javascript local variable's value like window['cont']. But i get undefined. What i am trying to do is trying to access variables like $GLOBALS in php or $$. Here is an example of what i did. <script> function process(str) { alert

variable variables bad practice to use?

血红的双手。 提交于 2020-01-05 10:14:42
问题 I have just read the post is it bad practice to use variable variables in php in the following fashion? explaining why they are bad to use in with classes however, i have to create dynamic variables to be sorted for example: $array = array( array("Line 1","Line 2","Line 3"), array("Line 1","Line 2","Line 3"), array("Line 1","Line 2","Line 3"), ) $i = 1; foreach($array as $item){ $string = "Item".$i; $$string = $item[0]."some code".$item[1]."some code".$item[2]."some code"; } i know that there

PHP: setting session variables through variable variables

╄→гoц情女王★ 提交于 2020-01-04 06:04:14
问题 I would like to set a session variable with something akin to: $key = '_SESSION[element]'; $$key = 'value'; This does indeed set $_SESSION['element'] equal to value , but it also seems to clear the rest of my $_SESSION variable, resulting in the $_SESSION array only containing the new key/value pair. How can I write into the session using variable variables without nuking it? Edit: if this can't be done, so be it, we'll probably have to restructure and do things the "right" way. I just wanted

is it bad practice to use variable variables in php in the following fashion?

喜你入骨 提交于 2020-01-02 05:43:06
问题 For example, a simple mvc type system: /api/class/method rewritten into php variables using .htaccess/nginx.conf then doing something like: <?php // Set up class + method variables $className = some_class_filter($_GET['class']); $method = some_method_filter($_GET['method']); // Check if class exists and execute if(file_exists(BASE . "/controllers/" . $className . ".class.php")) { require BASE . "/controllers/" . $className . ".class.php"; $$className = new $className(); // Execute the method

Variable variables handling order: changes in PHP 7

天大地大妈咪最大 提交于 2020-01-01 19:07:29
问题 With the new PHP 7.0.0 out now, I'm a bit worried about the changes in evaluation order of the so-called 'variable variables'. On this page, under 'Changes to variable handling', a table is displayed with examples of expressions with their handling order in PHP 5 and PHP 7. The four expressions listed are: $$foo['bar']['baz'] $foo->$bar['baz'] $foo->$bar['baz']() Foo::$bar['baz']() Given the following string and array: $qux = 'quux'; $foo = array('bar' => array('baz' => 'qux')); the first

Variable variables in JavaScript

妖精的绣舞 提交于 2019-12-31 04:24:30
问题 according to my knowledge, this feature already exists in PHP. lets look at the following php code: $color = 'red'; $$color = 'dark'; description of the feature: Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically.A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, red , can be used as the name of a variable.At this point two variables have been

When to use a variable variable in PHP?

柔情痞子 提交于 2019-12-30 04:57:05
问题 I've been developing in PHP for a while now, and I still have not had a task where I've had to use variable variables. Can anyone give me examples where using them is a good idea ? Or were they included in the language just for fun ? 回答1: One situation where I've had to use them is URI processing, although this technique might be dated, and I admittedly haven't used it in a long time. Let's say we want to pull the URI from the script in the format domain.tld/controller/action/parameter/s . We

PHP variable variables in .NET

余生颓废 提交于 2019-12-24 03:39:05
问题 Does .NET natively support anything similar to PHP's variable variables? If not, how 1 could such a feature be most easily implemented? 1 If you think variable variables are always bad, feel free to state your case but the main question is: how can they be implemented? 回答1: This is a feature deeply embedded in dynamic languages. C# has its roots as a static, object-oriented language, and up to C# 3.0 this means no luck accomplishing what you want in any proper way. However, C# 4.0/.NET 4.0

How can I print $title1 $title2 $title3… using a for loop in PHP

若如初见. 提交于 2019-12-24 00:56:29
问题 I want to print these variables using a for loop: <?php $title1 = "TEXT1"; $title2 = "TEXT2"; $title3 = "TEXT3"; $title4 = "TEXT4"; $title5 = "TEXT5"; for ($i = 1; $i <= 10; $i++) { echo "$title".$i; // I want this: TEXT1 TEXT2 TEXT3 TEXT4 TEXT5 } ?> 回答1: To do exactly what you want, create a new variable containing the name of the variable you want to use, and then use it as a variable variable, like this: $varname = "title$i"; echo $$varname; However, the more correct way to do this is to