Tutorials for PHP Variables

What is Variable?

In any programming language, a variable is a computer memory name that represents knowledge. One of the numerous computer program components is the variables. For declare a variable in php, the $ sign with its signature, like $myVariable, can be used.

How to declare PHP variable?

  • All variables in PHP start with a $(doller) sign followed by the name of the variable.
  • A valid Variable name starts with a letter (A-Z, a-z) or underscore(_), followed by any number of letters, numbers, or underscores.
  • If a variable name is more than one word, it can be separated with underscore(for example $employee_code instead of $employeecode).
  • ‘$’ is a special variable that can not be assigned.

What are the Types of Variable?

Local Variables:- A local variable is one that only exists inside a certain scope. They only exist in the function in which they were created. They’re frequently referred to as automatic variables since they’re produced automatically when the function starts running and disappear immediately when it’s completed. The term auto can be used to create these variables explicitly, but it isn’t required because auto is the default.

Global Variables:- A global variable is one that is defined outside of all functions and is accessible to all of them. These variables are unaffected by scopes and are always accessible, implying that a global variable persists until the program is terminated. It’s possible to make a global variable in one file and use it in another. The variable must be defined in both files to do this, but the term extern must come before the “second” declaration.

Static Variables:- A global or local variable can be a static variable. Both are generated by using the term static before the variable definition. A local static variable is one that can keep its value from one function call to the next and will persist until the program is finished. When creating a local static variable, it should be given an initial value. If it isn’t, the value will be set to 0 by default. A global static variable may only be accessed within the file in which it was created. File scope is a term used to describe a variable’s scope.

Variable Initialization?

PHP can store all type of data in variables. Before using a variable in PHP, assign a value to it so PHP will create that variable and store data in it. That means we have to assign data to a variable before attempting to read a variable’s value.

Example:-

$roll = 256;
$price = 25.50;
$name = “Geeky Shows”;
Note - If a variable is created without a value, it is automatically assigned a value of NULL.
Tagged : / / / / / / / / /