Php Variables

Php Variables

PHP VARIABLES FOR BEGINNERS

Every programming language has a concept called a variable. In this tutorial, we will learn everything concerning variables in PHP. How its syntax is been written, declared, PHP variable scope, and example will be cited for every step in the tutorial.

PREREQUISITE

• Server environment installed and basic knowledge of PHP syntax (i.e., to test code) e.g., XAMP, WAMP, LAMP, MAMP. If you don’t have this installed check this tutorial before you continue.

• Code editor.

• Basic understanding of using an operating system.

VARIABLES

Variables are used in programming to store data or value for future use, as simple as that. Variable has rules that must be followed in declaring and assigning it to a value or expression. They are as follows

• A variable in PHP must begin with a dollar sign ($) followed by the variable name. See the example below.

// Variable

$name = "Malcolm X";

• A variable in PHP can only contain alphanumeric characters and underscores. See the example below.

// Variable naming

$name1 = "Martin";

$name_2 = "Luther";

$name3three = "Junior";

• Variable assigning is been done using the assignment operator equal to sign (=). With the variable name on the left and the value on the right. See the example below.

// Variable assigning is only with equal to sign  

$name = "Floyd";

• PHP variables are case-sensitive. See the example below.

/* PHP variables are case sensitive, the two examples below are two different variables not because of the value they possess but the variable name */

$Cityname = "Ontario";

$cityname = "Ottawa"

• PHP variables do not need to be declared before assignment. See the example below.

// Assigning variable to value immediately without declaring they first

$marital status = "inbetween";

• Variable value in PHP is the last assigned expression to the variables. See the example below.

/* Last assigned value will be the variable value as below capital was changed from "New York" to "Washington D.C" */

$capital = "New York";

$capital = "Washington D.C";

PHP VARIABLE SCOPE

Variable scope refers to the part of the program a variable can be a reference to where the variable is declared or assigned to an expression.

PHP variable scope is divided into four, namely:

• Global variable.

• Local Variable.

• Static variable.

• Function parameter.

GLOBAL VARIABLES

These are variables that are declared in a program and can be referenced from anywhere in the program. However, it’s important to state clearly that such variables are global. This is achieved by placing a GLOBAL keyword in front of the variable like in the example below.

// Global variable

GLOBAL $location = "space";

Putting the GLOBAL keyword in front of an existing variable tells the program to make such variable considered global when referenced.

LOCAL VARIABLES

Variables declared within functions are called local variables. Such variables are considered local because they can only be referenced within the function it was declared. When refer to from outside of the function PHP will look everywhere in the program except for inside the functions for the variable with the same name and if none is found it will return an error that undeclared variable is been called but if any is found it will return a value which is different from the one declared inside the function. See the example below.

/* Local variable. Below is a function that add two number together and return a answer. The result variable to be returned is local because it can only be referenced within the function */

function addition($x, $y) {
    $result = $x + $y
    return $result
  }

FUNCTION PARAMETERS

Function parameters are values pass into a function to perform some operation and return a result. Function, in short, is a block of code that performs some operation taking in an argument as parameters. Parameters are pass into a function like the way variables are declared. Parameters are declared after the function name in-between the parenthesis. See the example below to understand more.

/* Function parameters are the variable passed between the parenthesis after the function name, in the case of the example below addition */

function addition($x, $y) {
    $result = $x + $y
    return $result
  }

STATIC VARIABLES

The value of variables declared as function parameters is been lost on the function exit (i.e. when the function is been called and the result has been returned by the function). The static variable however retains its value after the function exit. The static variable will still hold the value should the function be called again. Static variables are declared by simply placing a STATIC keyword in front of the variable name. See the example below to understand more.

// Static variable retains their value after the function exit.

function addition($x, $y) {
   ** STATIC** $result = $x + $y
    return $result
  }

BONUS

CONSTANT

Constant is a name or an identifier for a simple value. Constant is case-sensitive by defaults. By convention constant identifiers are uppercase. Constants are like variables but once defined their value cannot be changed. A constant name can start with a letter or an underscore followed by any number of underscores, alphabet, and number. To define a constant, you use the define() function and you retrieve it by simply specifying the name of the constant. Constant is applied in programming to store data that don’t change like configuration settings such as database name and password, URL etc.

CONSTANT EXAMPLES

// Constant examples

define("DRIVING_AGE": 18);

define("VOTING_AGE": 21);

VALID AND INVALID CONSTANT

// Valid constant

define("_DRIVING_AGE": 18);

define("VOTING_AGE2": 21);

// Invalid constant

// Constant cannot start with a double underscore as in below but can start with a single underscore
define("__VOTING_AGE": 18);

// Constant cannot start with a number as in below 
define("1VOTING_AGE": 21);

DIFFERENCES BETWEEN CONSTANT AND VARIABLE

• There is no need to write a dollar sign ($) before a constant name whereas it compulsory for variables to be declared.

• Variable is declared using assignment operator (i.e., equal to sign) while constant is defined with the define () function.

• Constant can be defined and accessed anywhere without regard to variable scoping rules.

• Once constant is defined it cannot be defined or undefined.

CONCLUSION

Welcome to the end of the tutorial on PHP Variable for Beginner. You have learned the basics of PHP variables. How variables are named, declare, access, and every rule to follow in using variables in PHP. Also, you learn about constants, how they are used, rules and properties that apply to them. If you have any questions, feel free to comment below or better send me a DM on Twitter. Thank you for your time and have a nice day.