PHP Data Types

PHP provides four primitive data types: integers, floating point numbers, strings, and booleans. In addition, there are two compound data types: arrays and objects.

 Integers

Integers are whole numbers. The range of integers in PHP is equivalent to the range of the long data type in C. On 32-bit platforms, integer values range from -2,147,483,648 to +2,147,483,647. PHP automatically converts larger values to floating point numbers if you happen to overflow the range. An integer can be expressed in decimal (base-10), hexadecimal (base-16), or octal (base-8). For example:
$decimal=16;
$hex=0x10;
$octal=020;

 Floating Point Numbers

Floating point numbers represent decimal values. The range of floating point numbers in PHP is equivalent to the range of the double type in C. On most platforms, a double can be between 1.7E-308 to 1.7E+308. A double may be expressed either as a regular number with a decimal point or in scientific notation. For example:
$var=0.017;
$var=17.0E-3
PHP also has two sets of functions that let you manipulate numbers with arbitrary precision. These two sets are known as the BC and the GMP functions. See http://www.php.net/bc and http://www.php.net/gmp for more information.

 Strings

A string is a sequence of characters. A string can be delimited by single quotes or double quotes:
'PHP is cool'
"Hello, World!"
Double-quoted strings are subject to variable substitution and escape sequence handling, while single quotes are not. For example:
$a="World";
echo "Hello\t$a\n";
This displays "Hello" followed by a tab and then "World" followed by a newline. In other words, variable substitution is performed on the variable $a and the escape sequences are converted to their corresponding characters. Contrast that with:
echo 'Hello\t$a\n';
In this case, the output is exactly "Hello\t$a\n". There is no variable substitution or handling of escape sequences.
Another way to assign a string is to use what is known as the heredoc syntax. The advantage with this approach is that you do not need to escape quotes. It looks like this:
$foo = <<<EOD
  This is a "multiline" string
  assigned using the 'heredoc' syntax.
EOD;
The following table shows the escape sequences understood by PHP inside double-quoted strings.
Escape sequence
Meaning
\n
Linefeed (LF or 0x0A (10) in ASCII)
\r
Carriage return (CR or 0x0D (13) in ASCII)
\t
Horizontal tab (HT or 0x09 (9) in ASCII)
\\
Backslash
\$
Dollar sign
\"
Double quote
\123
Octal notation representation of a character
\x12
Hexadecimal notation representation of a character

 Booleans

The boolean type only has two states: true and false. For example:
$flag = true;
Boolean values are most commonly used when the == or === operators perform a comparison and return the result.

 Arrays

An array is a compound data type that can contain multiple data values, indexed either numerically or with strings. For example, an array of strings can be written like this:
$var[0]="Hello";
$var[1]="World";
Note that when you assign array elements like this, you do not have to use consecutive numbers to index the elements.
Related Posts:
  • MySQL Database Using PHP <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  <title>Wines</title>  </head> <body> <pre> <?php  // (1) Open the… Read More
  • Declaring a Class To design your program or code library in an object-oriented fashion, you'll need to define your own classes, using the class keyword. A class definition includes the class name and the properties and methods of the clas… Read More
  • Configuring the Connection Mysql <?phptry{$pdo = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbuser','mypassword');}catch (PDOException $e){$output = 'Unable to connect to the database server.';include 'output.html.php';exit();} $pdo->setAttribute(… Read More
  • mysqli Fetch Methods If you prefer different MySQL fetch methods, they're also in mysqli. Given the same query of SELECT username FROM users, these example functions all print the same results: // Fetch numeric arrays: while ($row = mysqli_… Read More
  • Sorting One Array at a Time PHP functions for sorting an array Effect Ascending Descending User-defined order Sort array by values, then reassign indices starting with 0 sort( ) rsort( ) usort( ) Sort array by values … Read More
  • MySQL Data on the Web Before we leap forward, it’s worth taking a step back for a clear picture of our ultimategoal. We have two powerful tools at our disposal: the PHP scripting languageand the MySQL database engine. It’s important to understand… Read More
  • What is difference between mysql_fetch_array(),mysql_fetch_row() and mysql_fetch_object() ? mysql_fetch_array():: fetches a result row as a associated array, numeric arraymysql_fetch_object: Fetaches a result row as object.mysql_fetch_row::fetches a result row as array … Read More
  • Convert CSV to PHP Every once in a while, I have a static list of values that I don't want to put into a database, but that I do want to use in my PHP application. That static data can come from a variety of sources, but often it's in a s… Read More
  • XML Extensions in PHP 5 SimpleXML A new PHP 5-only extension that excels at parsing RSS files, REST results, and configuration data. If you know the document's format ahead of time, SimpleXML is the way to go. However, SimpleXML supports o… Read More
  • Using Cookie Authentication PHP Store authentication status in a cookie or as part of a session. When a user logs insuccessfully, put their username in a cookie. Also include a hash of the username and a secretword so a user can't just make up an authentic… Read More
  • Create Dynamic Database Access Objects Some simple PHP that makes for a surprisingly robust script <?php require_once( "DB.php" ); $dsn = 'mysql://root:password@localhost/books'; $db =& DB::Connect( $dsn, array() ); if (PEAR::isError($db)) { die($db->g… Read More
  • SELECTING DATA IN PHP You frequently select data in a MySQL table using PHP. Selecting data through a PHP programusing a MySQL command takes four steps:1. Make a connection to the database.2. Create a safe query with the command.3. Run the query.… Read More
  • Advanced SQL 15.1 Exploring with SHOW The SHOW command is useful for exploring the details of databases, tables, indexes, and MySQL. It's a handy tool when you're writing new queries, modifying database structure, creating repor… Read More
  • php and pdf  Documents and Pages A PDF document is made up of a number of pages. Each page contains text and/or images.Put text onto  the pages, and send the pages back to the browser when you're done.    … Read More
  • Using mysql_fetch_array fanc we accessed attributes in order using the foreach loop statement. In many cases, you'll also want to access the attributes in another way, and this is usually best achieved by using the attribute names themselves. It's muc… Read More