Approaches to Web Development for Bioinformatics

Previous  Contents  Next
References

Types

PHP supports eight primitive types: Boolean, integer, float, string, array, object, resource, and NULL.  Boolean, integer, float, and string are scalar or simple types.  Array and object are compound types.  Resource and NULL are special types.  Since PHP is a scripting (i.e. loosely typed) language the interpreter infers the types rather than the programmer declaring the type.

The possible values of a Boolean variable are TRUE and FALSE.  To aid in programming shortcuts a number of other types are converted to Boolean values.  These include the integer 0 (FALSE), the empty string (FALSE), the special type NULL (FALSE), and others. 

String literals can be declared with single quotes, double quotes, or the heredoc syntax.  Backslashes are used to escape quotes, as in other languages.  Variables are expanded for strings in double quotes as in Perl.  The heredoc syntax uses <<< and is useful for multi-line strings.  You can get a character in a string using square brackets, as in an array.  For example $str[0] is the first character in the variable $str.  The function strlen returns the length of a string.  Strings are concatenated with '.' (rather than with '+' in other languages).

By default, a character in PHP is the same as a byte implying that PHP has no native support for Unicode.  However, if you have the Multibyte String extenstion (mbstring) installed, you can set the internal encoding to something else, like UTF-8, using the following code.


PHP

mb_internal_encoding('UTF-8');

The mbstring extension also allows this to be set in the PHP initialization file php.ini.

In PHP arrays are ordered maps.  An array can be created with the array function.


PHP

<?php
$gene = array("name" => "HD", "start_pos" => 146);
echo $gene["name"];
echo "<br>";
echo $gene["start_pos"];
?>

This will output the text


Program output

HD
146

You can iterate over an array using either a for or a foreach statement. Here is a simple for loop.


PHP

<?php
$hd = array("name" => "HD", "start_pos" => 146);
$scna = array("name" => "SCN3A", "start_pos" => 472);
$geneList = array($hd, $scna);
$count = count($geneList);
for ($i = 0; $i < $count; $i++) {
echo $geneList[$i]["name"];
echo " ";
echo $geneList[$i]["start_pos"];
echo "<br>";
}
?>

This produces the following output:


Program output

HD 146
SCN3A 472

To remove an element from an array use the unset() function. Arrays can grow and shrink in size in PHP.

A resource is a special type that holds a reference to an external resource, such as a file, a network connection, or a database connection. I show an example of a file type below in the section Working with Files and Text.


Previous  Contents  Next
References

Contributed Comments and NotesAdd a comment.

There are no user comments.

Google

Please send ideas and opinions by email at alexamies@gmail.com.

© 2006-2007 Alex Amies