advertisement
What are variable and variable Scopes?
Variable
A variable in a programming language is named memory location, which stores data that keeps charging during the execution of a program. Different types of data can be stored in a variable. An expression combines variables, constants, functions, and operators.
In computer programming, a variable is a named storage location that holds a value. This value can be of different types, such as integer, floating-point number, string, boolean, and so on. The purpose of a variable is to store data that can be used in the program's operations and calculations. Variables are defined by specifying a name and a data type. The name of a variable is a sequence of characters that identifies it in the program. The data type specifies the kind of data that can be stored in the variable. For example, an integer variable can only hold whole numbers, whereas a floating-point variable can hold fractional numbers.
Variables can be initialized with an initial value, which can be a literal value or the result of an expression. Once a variable is defined and initialized, its value can be changed during the program's execution by assigning a new value to it. Variables are essential to programming because they allow programs to store and manipulate data dynamically. They enable programs to perform calculations, make decisions, and interact with users by storing and retrieving data as needed. Without variables, programs would be limited to fixed data and predefined calculations, making them less flexible and less powerful.
How Variable Works
Variables work by storing data in computer memory, which can be accessed and manipulated by the program. When a variable is declared in a program, the computer allocates a block of memory with a unique address where the variable's value can be stored. The type of data that the variable can hold determines the amount of memory that is allocated for the variable.
To use a variable in a program, you need to perform two basic operations: declaration and assignment. Declaration involves specifying the variable's name and type, which tells the computer to allocate memory for the variable. The assignment involves setting the variable's value by storing data in the memory location that the variable represents.
For example, let's say you want to create a variable named "age" that stores a person's age in years. You would declare the variable as an integer type by writing: int age;
This tells the computer to allocate enough memory to store an integer value and label it as "age".
Next, you would assign a value to the variable by writing:
age = 25;
This stores the value 25 in the memory location that the variable "age" represents.
Once a variable is declared and assigned a value, it can be used in calculations, comparisons, and other operations within the program. The value of a variable can also be changed by assigning a new value to it at any point in the program.
Scopes of variables
The scope of a variable is the context within which it is defined. It is the lifetime of a variable from the time the variable is created until the time execution of the script ends. In PHP, different scopes that a variable can have are as follows:
1. Local variables:-
A variable initialized and used inside a function is called a local variable. The declaration of a local variable is similar to a normal variable. It is declared inside a function. The function is completed.
2. Global variable:-
A variable that retains its value throughout the lifetime of a web page is called a global variable. A global variable is declared with the help of the keyword global, within the function. As a result, it can be accessed from any part of the program. Syntax to declare a variable in a program is as follows:
global $var_name;
where,
$var_name - defines the global variable name.
3. Static variable:-
A static variable is similar to a local variable. The only difference is that the static variable retains its value even after the function terminates. When a function terminates, The local variables of that function lose their values. To retain the value of the variable throughout the execution of the program, use the static keyword with the variable name. Static variables are only accessible from within the function calls. static variables can be initialized during declaration and the static declaration is resolved during compile time. The static variable is commonly used in recursive functions. A recursive function calls itself recursively within a function.
The syntax to declare a static variable is as follows:
static $var_name = value;
where,
var_name - defines the variable name
variable - specific the value of the variable
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0