Questions tagged [variables]

THIS IS AMBIGUOUS; USE SPECIFIC-LANGUAGE TAGS WHENEVER APPLICABLE. A variable is a named data storage location in memory. Using variables, a computer program can store numbers, text, binary data, or a combination of any of these data types. They can be passed around in the program.

A variable is a named data storage location in memory. Using variables a computer program can store numbers, textual data, , or a combination of any of these data types. The data can be passed around in a program by copying it from variable to variable or by referencing variables, i.e. defining from which variable a receiving code part is to take the contained data.

Variables which are only accessible within a certain function are termed "local variables". A "global variable", or one with indefinite scope, may be referred to anywhere in the program.

In some , variables are constrained by a specific data type. Data types may vary across languages, but share many commonalities.

Primitive data types usually include:

  • character, char, string, varchar (text)
  • byte, short, int, tinyint, integer, long (whole numbers)
  • double, decimal, float (real numbers)
  • bit, boolean (true/false)
  • date, datetime (date and time values)
  • object (any value, including composite types)
  • binary, raw, varbinary (that store stream of system data in binary form)

Composite data types consist of usually more than one of the primitive types and/or even other composite types.

# an example composite type, in pseudo code
Person(
    'First name'  : string,
    'Surname'     : string,
    'Birthday'    : date,
    'CanProgram'  : boolean
)

Some languages contain extra primitives: Tuples (Python), Linked Lists (Lisp), Hash Tables (Lisp, Perl, Python, Lua, D).

Some programming languages allow variables that store functions, that can be stored in data structures, passed as parameters to other functions, or returned as a result from other functions.

Memory allocation

The specifics of variable allocation and the representation of their values vary widely, both among programming languages and among implementations of a given language. Many language implementations allocate space for local variables (whose extent lasts for a single function call) on the stack, and their memory is automatically reclaimed when the function returns. More generally, in name binding, the name of a variable is bound to the address of some particular block (contiguous sequence) of bytes in memory, and operations on the variable manipulate that block. Referencing is more common for variables whose values have large or unknown sizes when the code is compiled. Such variables reference the location of the value instead of storing the value itself, which is allocated from a pool of memory called the heap.

More information and reference material on Wikipedia.

53839 questions
231
votes
9 answers

PowerShell: Setting an environment variable for a single command only

On Linux, I can do: $ FOO=BAR ./myscript to call "myscript" with the environment variable FOO being set. Is something similar possible in PowerShell, i.e. without having to first set the variable, call the command, and then unset the variable…
miracle2k
  • 29,597
  • 21
  • 65
  • 64
228
votes
9 answers

Using braces with dynamic variable names in PHP

I'm trying to use dynamic variable names (I'm not sure what they're actually called) But pretty much like this: for($i=0; $i<=2; $i++) { $("file" . $i) = file($filelist[$i]); } var_dump($file0); The return is null which tells me it's not…
user1159454
  • 3,267
  • 3
  • 19
  • 25
227
votes
8 answers

Importing variables from another file?

How can I import variables from one file to another? example: file1 has the variables x1 and x2 how to pass them to file2? How can I import all of the variables from one to another?
Some kid
  • 2,417
  • 3
  • 14
  • 12
225
votes
8 answers

How can you dynamically create variables?

I want to create variables dynamically in Python. Does anyone have any creative means of doing this?
Noah R
  • 5,287
  • 21
  • 56
  • 75
225
votes
27 answers

Simpler way to create dictionary of separate variables?

I would like to be able to get the name of a variable as a string but I don't know if Python has that much introspection capabilities. Something like: >>> print(my_var.__name__) 'my_var' I want to do that because I have a bunch of variables I'd…
Bite code
  • 578,959
  • 113
  • 301
  • 329
224
votes
22 answers

How to swap two variables in JavaScript

I have this two variables: var a = 1, b = 2; My question is how to swap them? Only this variables, not any objects.
Lukas
  • 7,384
  • 20
  • 72
  • 127
221
votes
8 answers

Multiple left-hand assignment with JavaScript

var var1 = 1, var2 = 1, var3 = 1; This is equivalent to this: var var1 = var2 = var3 = 1; I'm fairly certain this is the order the variables are defined: var3, var2, var1, which would be equivalent to this: var var3 = 1, var2 = var3, var1…
David Calhoun
  • 8,315
  • 4
  • 30
  • 23
218
votes
16 answers

How do I count occurrence of unique values inside a list

So I'm trying to make this program that will ask the user for input and store the values in an array / list. Then when a blank line is entered it will tell the user how many of those values are unique. I'm building this for real life reasons and not…
Joel Aqu.
  • 2,289
  • 2
  • 13
  • 4
206
votes
9 answers

How do you create different variable names while in a loop?

For example purposes... for x in range(0,9): string'x' = "Hello" So I end up with string1, string2, string3... all equaling "Hello"
Takkun
  • 8,119
  • 13
  • 38
  • 41
204
votes
7 answers

What is the difference between ${var}, "$var", and "${var}" in the Bash shell?

What the title says: what does it mean to encapsulate a variable in {}, "", or "{}"? I haven't been able to find any explanations online about this - I haven't been able to refer to them except for using the symbols, which doesn't yield…
SheerSt
  • 3,229
  • 7
  • 25
  • 34
201
votes
7 answers

How do you know a variable type in java?

Let's say I declare a variable: String a = "test"; And I want to know what type it is, i.e., the output should be java.lang.String How do I do this?
Miguel Ribeiro
  • 8,057
  • 20
  • 51
  • 74
197
votes
19 answers

How can I combine two strings together in PHP?

I don't actually know how to describe what I wanted, but I'll show you: For example: $data1 = "the color is"; $data2 = "red"; What should I do (or process) so $result is the combination of $data1 and $data2? Desired result: $result = "the color is…
glennanyway
  • 2,013
  • 2
  • 13
  • 3
197
votes
6 answers

How to get the type of a variable in MATLAB

Does MATLAB have a function/operator that indicates the type of a variable (similar to the typeof operator in JavaScript)?
Dónal
  • 185,044
  • 174
  • 569
  • 824
195
votes
9 answers

How do I create a multiline Python string with inline variables?

I am looking for a clean way to use variables within a multiline Python string. Say I wanted to do the following: string1 = go string2 = now string3 = great """ I will $string1 there I will go $string2 $string3 """ I'm looking to see if there is…
evolution
  • 4,332
  • 5
  • 29
  • 34
194
votes
17 answers

Best way to test for a variable's existence in PHP; isset() is clearly broken

From the isset() docs: isset() will return FALSE if testing a variable that has been set to NULL. Basically, isset() doesn't check for whether the variable is set at all, but whether it's set to anything but NULL. Given that, what's the best way to…
chazomaticus
  • 15,476
  • 4
  • 30
  • 31