variable-variables

Python variable variables without eval?

会有一股神秘感。 提交于 2019-12-02 01:05:38
Is there is a way to access variables using a variable string in python? For instance, I would like a neater way than using eval for the following: def toggleListButtons (self): buttons = ["flip", "remove", "removeAll", "delete", "deleteAll", "loadDirectory"] for button in buttons: eval("self." + button + "Button.setEnabled(!self." + button + "Button.isEnabled())") What you're looking for is the getattr() built-in function. There is also hasattr() and setattr() . button = getattr(self, 'flipButton') button.setEnabled(True) 来源: https://stackoverflow.com/questions/12111358/python-variable

PHP $_GET/$_POST via variable variables

做~自己de王妃 提交于 2019-12-01 06:37:34
I'm attempting to dynamically access both the $_GET and $_POST arrays, among others, using variable variables. The reason I'm trying to do this is so that I can perform similar actions on multiple arrays without needing to declare specific iterations for each. I'm reasonably sure this is possible, as PHP's documentation says it is able to use variable variables to dynamically access an array, however I'm unable to get it to work. A simple demonstration is when I'm attempting to verify that a certain property has been set. if(isset(${$this->_array}[$property])) { return ${$this->_array}[

PHP $_GET/$_POST via variable variables

こ雲淡風輕ζ 提交于 2019-12-01 05:01:40
问题 I'm attempting to dynamically access both the $_GET and $_POST arrays, among others, using variable variables. The reason I'm trying to do this is so that I can perform similar actions on multiple arrays without needing to declare specific iterations for each. I'm reasonably sure this is possible, as PHP's documentation says it is able to use variable variables to dynamically access an array, however I'm unable to get it to work. A simple demonstration is when I'm attempting to verify that a

When to use a variable variable in PHP?

核能气质少年 提交于 2019-11-30 15:02:45
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 ? 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 could remove the script name using the following: $uri_string = str_replace($_SERVER['SCRIPT_NAME'], '', $

variable-variables in PHP

时光毁灭记忆、已成空白 提交于 2019-11-29 14:12:42
I know you can do: $hash('foo') and $$foo and also $bar[$foo] , what are each of these things called? $hash('foo') is a variable function. $hash may contain a string with the function name, or an anonymous function. $hash = 'md5'; // This means echo md5('foo'); // Output: acbd18db4cc2f85cedef654fccc4a4d8 echo $hash('foo'); $$foo is a variable variable. $foo may contain a string with the variable name. $foo = 'bar'; $bar = 'baz'; // This means echo $bar; // Output: baz echo $$foo; $bar[$foo] is a variable array key. $foo may contain anything that can be used as an array key, like a numeric

Variable variables: when useful? [duplicate]

十年热恋 提交于 2019-11-29 08:08:19
Possible Duplicate: What's an actual use of variable variables? OK, this question may look a little bit point-whoreish, but I'd really like to know: when are variable variables useful? I haved programmed in PHP for several years, but I've never used them. For me it looks rather funny than useful. What are some real life examples of variable variables? Update: I'm really sorry about your votes. The linked 'duplicate' may be really a dupe except one thing: the examples listed there show me why not to use variable variables. Your Common Sense Never. This feature should be banned from the language

Using Variable for Property Name of Object - Javascript [duplicate]

雨燕双飞 提交于 2019-11-28 09:41:36
问题 This question already has an answer here: How to use the value of a variable in creating an object in JavaScript [duplicate] 1 answer saw a few answers related to this, but none answer this version of the subject in question. Consider the following: (linkto: jsfiddle) $(function(){ arrKeys = []; objArr = []; nameArr = ['name1','name2','name3','name4']; descArr = ['desc1','desc2','desc3','desc4']; allValues = {name: nameArr, desc: descArr}; arrKeys[0] = 'name'; arrKeys[1] = 'desc'; first =

Trying to access $_SERVER(or any global) variable from string name [duplicate]

馋奶兔 提交于 2019-11-28 09:31:04
问题 This question already has answers here : Superglobals can't be accessed via variable variables in a function? (3 answers) Closed 14 days ago . Today I met such a terrible situation. It seems this bug is related to PHP . I'm trying to access to $_SERVER or another super global variables but from string name. This version of implementation is working. var_dump(${"_SERVER"}); // working But when trying to do this with variable then receiving notice that variable not found. $var_name = "_SERVER";

How do I dynamically create the variable name in a PHP loop?

蓝咒 提交于 2019-11-28 09:19:52
Ok so i have this php foreach loop <?php foreach ($step_count->rows as $step) { ?> and $step will be the step numbers 1, 2, 3, 4, 5 up to the total steps within the loop i a need to set the value of the images within the loop to standard_image_1 or whatever step there is...so for example <input value="<?php echo {$standard_image_"$step['number']"}; ?>" /> so basically i need the variable $standard_image_1 and so on depending on the steps but i dont know the correct syntax to do this Look at the docs for "variable variables" - http://php.net/manual/en/language.variables.variable.php <?php echo

Construct a PHP variable name based on other variable values and static text

╄→尐↘猪︶ㄣ 提交于 2019-11-28 08:47:37
问题 I want to tell my function which variable to call based on the day of the week. The day of the week is stored in $s_day, and the variables I want to call changes based on which day it is. e.g. I've stored a string 'Welcome to the week' in $d_monday_text1. Rather than build a set of 7 conditional statements (e.g. if date=monday echo $foo, else if date=tuesday echo $bar...), can I change the name of the variable called in the function by concatenating the name of the variable? $s_day = date("l"