Constant Variables PHP Essential Training

Constant Variables PHP Essential Training

PHP

Constant Variables PHP Essential Training

The PHP is using the variables to store the data for temporary untill we save the data into our database. The variables are rely on the data types like integer, double, float etc. In this Constant Variables PHP Essential Training we're gonna teach you how to save a constant value inside the variable. In our previous examples we see the variable values are changed depends on the condition.
But the constants are necessary thing like it used in other programming languages. They are used to maintain a value as same as until the current page is on live. If we move to another page, and that page also has the same variable name we used for the constant then only it changes the values to dynamic.
Lets see how the functions of the Constant Variables PHP Essential Training can do for us.
// Normal Variable declaration
$max_width = 980;
// constant variable declaration
define("MAX_WIDTH", 980);
echo "Constant Value <br />";
echo "Max_Width: " . MAX_WIDTH;
Constant Variables PHP Essential Training
The above is shows how the constant value is stored and view in PHP. We don't need to use the $ symbol like the other variables we used in previous lessons. The define() is a keyword to indicate to the PHP compiler this is an constant and don't change its values.
Lets see what will happen if the constant value is change dynamically.
// Increment the value by 1
MAX_WIDTH += 1;
// Display the Output
echo MAX_WIDTH;
Constant Variables PHP Essential Training

Constant Variables PHP Essential Training

The above try for increment a value with the constant variable is throws an error. Lets do this for an ordinary variable and see what will happen.
// Increment the value by 1
$max_width += 1;
echo "Max_Width: " . $max_width;
Constant Variables PHP Essential Training
The above is just increase the value of the $max_width by 1. The constant values are never gonna change until the present page is live.
The constant variable values are used for handle the credential default values and some mathematical operations to perform the calculation functions.
Download the source code for Constant Variables PHP Essential Training from here.

Leave a Reply