Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Working with PHP Datatypes

By Steve Adcock
2005-05-14


Defining variables and constants

A programming language is useless without variables and constants. Variables and constants simply hold information, called a value. A variable's value can change, while a constant's value is, as you might have guessed, constant, or static. Let's first look at how you'd define a variable. Note, two slashes (//) define a PHP comment, 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) as an integer, or 12
$z = $i + 20; // Defines z as the value of i + 20, or 30
$team = "Broncos"; // Defines team as Broncos

?>
Note a few things. First, each variable is defined with a dollar sign ($) before the variable's name. In addition, like many lines of PHP code, a semicolon is used. Semicolons do not, however, need to be placed at the end of commented lines. Lastly, strings, or a combination of characters, are defined with quotation marks around the value, while integers are not.

Now, let's define some constants, which are essentially a variable that never changes. Constants are defined using the define() function.

<?


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 display what we have defined? Let's find out.

<?


// First, we will display our variables that we defined above, and listed below
$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 easier to understand, I just brought over the variables and constants that we had defined a little while ago. Notice we are again using echo, which will print out whatever is within the parenthesis. echo("$i <br>"); means we will print out the value of variable i along with a line space, for readability. Notice I put two line spaces after the last variable to give room for the constants, which lie below.

Displaying constants is slightly different. We do not use quotation marks around them, first of all. In addition, to include other text and HTML code within the same echo statement, we must use a period (.) to separate the two and use quotation marks only around the text or HTML code.

Tutorial Pages:
» Working with PHP Datatypes
» Finding your PHP info
» Defining variables and constants
» Let's play "What's that datatype?" !
» If this, If that


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1

Ask A Question
characters left.