Working With PHP Data Types - Defining variables and constants
(Page 2 of 5 )
Finding your PHP infoYou can easily find out some detailed parameter values and options regarding your PHP installation using PHP's phpinfo() function. PHP's code delimiters are <? and ?> and must surround all of your PHP code. The following code will display in-depth information about your PHP installation:
<?
phpinfo();
?>A cool little pre-defined PHP constant will let you view the type of operating system that your PHP installation is running on. To view it in your browser, use the following code:
<?
echo(PHP_OS);
?>The echo syntax simply displays whatever value is within the parenthesis; in this case, the value of the constant variable, PHP_OS will be displayed.
Defining variables and constantsA programming language is useless without variables and constants. Variables and constants are the building blocks of any programming language and contain memory addresses that represent where a specific value is stored. A variable's value can change, while a constant's value is, as you might have guessed, fixed.
Let's look at how you would define a variable. Note that two slashes (//) define a comment in PHP, and will not be displayed in your browser.
<?
$i = 10; // Defines i as integer 10
$k = 12.1; // Defines k as a double, 12.1 (integer type)
$k = (int) $k; // Redefines the value of k (12.1) to an integer, or 12
$z = $i + 20; // Defines z as the value of i + 20, or 30
$team = "Broncos"; // Defines team as Broncos
?>Notice a few things about the PHP code shown above. First, each variable is defined with a dollar sign ($) prepended to the variable's name. In addition, a semicolon is used to separate each variables declaration. Lastly, strings are defined within quotation marks, while integers are not.
Let's create some constant variables. Constants are created using the define function. The signature of the define function looks like this:
define(string constantName, mixed value);<?
define("COLOR", "green"); // Defines COLOR as green
define("JOB", "supervisor"); // Defines JOB as supervisor
define("SALARY", 100000); // Defines SALARY as 100000
?>Notice again that strings are written with quotation marks around the value, while integers are not. Defining variables and constants is one thing, but how can we actually display the values of the constant variables that what we have defined?
<?
// First, we will display our variables
$i = 10;
$k = 12.1;
$k = (int) $k;
$z = $i + 20;
$team = "Broncos";
echo("$i <br>"); // Displays 10
echo("$k <br>"); // Displays 12
echo("$z <br>"); // Displays 30
echo("$team <br> <br>"); // Displays Broncos
// Now, let's display the constants
define("COLOR", "green"); // Defines COLOR as green
define("JOB", "supervisor"); // Defines JOB as supervisor
define("SALARY", 100000); // Defines SALARY as 100000
echo(COLOR . "<br>"); // Displays green
echo(JOB . "<br>"); // Displays supervisor
echo(SALARY . "<br>"); // Displays 100000
?>To make this code example easier to understand, I have reused the variables and constants that we had defined a little earlier. Displaying constants is slightly different to displaying standard variables. Most notably, when we refer to a constant value, we don't prepend it with a dollar sign.
Next: Manipulating a variables data type >>
More PHP Articles
More By Steve Adcock