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.
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:
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.