///Working with PHP Datatypes

Working with PHP Datatypes

Working with PHP Datatypes

For those who have not noticed, this entire site has been constructed from the incredibly powerful PHP server-side programming language. PHP is based heavily upon C++, so all you C++ programmers out there will pick up PHP very easily. What’s the difference then? While C++ was designed to be a computer programming language for .exe applications, PHP was designed specifically for the Internet and is native to Linux. This article’s intent is not to detail dull programming theory, but to introduce you to PHP datatypes, working with them and manipulating them, minus the wordy mumbo-jumbo. This article presents code…code you can use and learn from. So, let us begin.

First, some unavoidable dialog. PHP is parsed on the server and can be embedded into an HTML document, saved with the .php or .phtml extension. Because the code is parsed on the server, any web visitor who tries to view your source code will NOT see any of your PHP coding, since it has been interpreted and transferred to HTML by the web server. You must have PHP installed on your personal server or your remote server. You may download and install PHP from PHP.net. Now, let’s get started, and remember, you can copy and paste all these working examples and try them out yourself.

Finding your PHP info

You can easily find out some juicy stuff regarding your PHP installation and web server by using PHP’s phpinfo() function. PHP’s delimiters are <? and ?> and MUST surround all your PHP coding. The following will display in-depth information about your PHP installation. Try it with PHP installed.

<?


phpinfo()

?>

A cool little pre-defined PHP constant will let you view the type of operating system that your PHP installation is operating on. To use that, follow the following code:

<?


echo(PHP_OS);

?>

The echo syntax will simply print out whatever is within the parenthesis; in this case, the value of the variable PHP_OS.

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.

Let’s play “What’s that datatype?” !

PHP includes a real easy function to display the type of a variable, not the actual value, called gettype(). You can use gettype() like this:

<?


$i = 10;
$j = 10.3;
$k = "Broncos";

echo(gettype($i) . "<br>"); // Displays integer
echo(gettype($j) . "<br>"); // Displays double
echo(gettype($k) . "<br>"); // Displays string

?>

Notice our use of the period to separate the gettype function by the HTML code <br>. The convenience of functions is the fact any variable can be passed into the function, from within the parenthesis, to be processed. So, by simply changing the variable we call into gettype(), we can check the datatype of that specific variable.

You can also set the datatype of a variable after it has been created. The following example first defines a double, and I will use PHP’s settype() function to redefine it as an integer.

<?


$k = 12.1;

echo("k = " . gettype($k) . "<br>"); // Displays k = double
settype($k, "integer"); // Changes the datatype of variable k
echo("k now = " . gettype($k)); // Displays k now = integer

?>

As you can see, we are actually passing in two different types of information into the settype() function, the variable and the variable type we want to change it to. Keep in mind that if you change a string to an integer, the value of that variable will equal 0, instead of the string of characters it initially was defined to hold.

If this, If that

Let’s make some decisions here. PHP’s if statements can be used to check the validity of data, integrity or even the presence of data within a variable. Let’s use the gettype() function in our first example to check the datatype of a variable.

<?


$i = 10;

if(gettype($i) == "integer") // Checks if variable i is an integer
{
echo("Alright, the variable i is an integer"); // If integer, then display
} else {
echo("No, sorry, variable i is not an integer"); // If not integer, then display
}

?>

Since 10 is an integer, and our if statement checks to see if the variable is an integer, the “Alright, the variable i is an integer” string will be displayed. Try changing 10 to 10.1. Since 10.1 is a double instead of an integer, the second statement, “No, sorry, variable i is not an integer” will be displayed.

Two more functions, isset() and unset() are also useful PHP utilities for working with datatypes. isset() checks to see if a variable has been given a value. unset() will destroy the variable, even from memory.

<?


$i = 10;

if(isset($i)) // Checks if variable i has a value
{
echo("Variable i has a value"); // If defined, then display
} else {
echo("Sorry, variable i has no value"); // If not integer, then display
}

?>

Since variable i does have a value, “Variable i has a value” will be printed. If you delete the $i = 10; line, the second echo statement, “Sorry, variable i has no value” will execute. Let’s move on to unset, to remove the variable for memory.

<?


$i = 10;

unset($i); // Destroys variable i

if(isset($i)) // Checks if variable i has a value
{
echo("This should NOT display"); // If not destroyed, then display
}

?>

In the first part of the coding, we defined a variable, i. Then, we used the unset() function and passed $i into it as an argument, which therefore destroyed the variable. Just to make sure that we did, in fact, destroy the variable, I used the isset() function we just looked at above to check for a value. Since there should not be a value, no echo statement should be printed and you should be left with a blank page, if nothing else is within your document, of course.

We can also perform an action if, and only if, a particular variable is a specific datatype, by using the is_double(), or is_string(), or is_array(), or is_integer, etc functions relative to the respective datatype you are checking for. Let’s look at an example.

<?


$i = 10;

if (is_integer($i))
{
echo("This is an integer");

if (is_string($i))
{
echo("This is a string");

if (is_double($i))
{
echo("This is a double");
}

?>

Since 10 is an integer, the first if statement will be true and “This is an integer” will be displayed. Try adding quotes around the 10, then the string echo value would display, or try adding a .1 after the 10, then it’s a double.

Here you have a basic introduction into datatypes and how to manipulate them using the PHP server-side language. A complete mastery of PHP ultimately begins at datatypes, so congratulations on passing the first step. A continuing study of this language can render you an expert in no time. Good luck.

2010-05-26T11:19:59+00:00 May 14th, 2005|PHP|0 Comments

About the Author:

Steve have been the main guy behind WebSiteGravy.com since its induction more than 5 years ago. Steve is a recent college graduate and works as a systems engineer. He has over 9 years of Internet experience, including web design, web site maintenance and planning and web site programming, promotion and graphics creation.

Leave A Comment