PHP Data Types A program can do many things, including perform calculations, sort names, prepare phone lists, display images, play chess, ad infinitum. To do anything, however, the program works with the data that is given to it. Data types specify what kind of data, such as numbers and characters, can be stored and manipulated within a program. PHP supports a number of fundamental basic data types, such as integers, floats, and strings. Basic data types are the simplest building blocks of a program. They are called scalars and can be assigned a single literal value such as a number, 5.7, or a string of characters, such as "hello", a date and time, or a boolean (true/false). There are two numeric values in PHP, they are: - integer: Integer numbers don’t have floating point, for example, 5, 12, 1567
- real: or floating point numbers, real number has a floating point, for example, 2.3
There are three ways to represent the numeric values: - base 10 numbers: They are represented using digits from 1 to 9, with an optional (.) for floating point if the value is real.
- base 8 numbers: They are represented by preceding the number by zero ‘0’, and it only permits the digits from 0 to 7, example 023, which is the same as 19
- base 16 numbers: They are represented by preceding zero and x ‘0x’, and it permits the digits from 0 – 9 and characters from ‘A’ to ‘F’, for example, 0x3f, which is the same as 63
Example:
12345 integer 23.45 float .234E–2 float in scientific notation .234e+3 float in scientific notation 0x456fff integer in base 16, hexadecimal 0x456FFF integer in base 16, hexadecimal 0777 integer in base 8, octal
Code example:
<html> <head><title>Printing Numbers</title> </head> <body bgcolor="lightblue"> <font face = "arial" size = '+1'> <?php print "The positive integer is <em><b>" . 5623 . " .</b></em><br />"; print "The negative integer is <em><b>" . -22 . ".</b></em><br />"; print "The floating point number is <em><b>" . 15.3 . " .</b></em><br />"; print "The number in scientific notation is <em><b> " . 5e3 . " . </b></em><br />"; print "\tThe string is: <em><b>I can't help you!</em></b><br />"; ?> </body> </html> An output for this example will be:
The positive integer is 5623 . The negative integer is -22. The floating point number is 15.3 . The number in scientific notation is 5000 . The string is: I can't help you!
String values are sequence of characters, included in a single quote or double quotes, for example,
‘This is a string’.
The value inside the quotes are not modified, so if you want to use quotes inside the string, then they have to be escaped, we escape characters in PHP by using backslash,
For example: ‘She wrote: \’PHP is easy to learn\’’.
The quote is not the only special character that can be escaped, other characters that can be escaped are:
\n Line feed \r Carriage return \t Tab \\ Backslash \” Double quotes
For example: “This is \n a string” this will evaluate(echo out) to: This is
a string
Boolean values are true or false, also 0 and empty string evaluates to false, and any numeric value rather than zero, or a string that is not empty evaluates to true.
An array is a collection of elements of heterogeneous type, arrays will be explained later.
An object is a collection of data and code, you will learn about Objects later in a this tutorial.
NULL means no value. It does not contain any meaningful data. PHP STRING
|