<?php
$name='bikash ranjan nayak';
echo gettype($name);
?>
$name='bikash ranjan nayak';
echo gettype($name);
?>
Application development and sharing our knowledge to developer to developer for helping knowledge to build a effective solution for web architect.
data type
is a set of values,
and the allowable operations on those values.
<?php $male = False; $r = rand(0, 1); $male = $r ? True: False; if ($male) { echo "We will use name John\n"; } else { echo "We will use name Victoria\n"; } ?>The script uses a random integer generator to simulate our case.
$r = rand(0, 1);The
rand()
function returns a random number from
the given integer boundaries. In our case 0 or 1.
$male = $r ? True: False;We use the ternary operator to set a
$male
variable. The variable is
based on the random $r
value. If $r equals to 1, the $male variable is
set to True. If $r equals to 0, the $male variable is set to False.
if ($male) { echo "We will use name John\n"; } else { echo "We will use name Victoria\n"; }We print the name. The if command works with boolean values. If the variable $male is True, we print the "We will use name John" to the console. If it has a False value, we print the other string.
<?php class Object {}; var_dump((bool) ""); var_dump((bool) 0); var_dump((bool) -1); var_dump((bool) "PHP"); var_dump((bool) array(32)); var_dump((bool) array()); var_dump((bool) "false"); var_dump((bool) new Object()); var_dump((bool) NULL); ?>In this script, we inspect some values in a boolean context. The
var_dump()
function shows information about
a variable. The (bool)
construct is called casting.
In its casual context, the 0 value is a number. In a boolean context, it is
False. The boolean context is when we use (bool) casting, when we use certain
operators (negation, comparison operators)
and when we use if/else, while keywords.
$ php boolean.php bool(false) bool(false) bool(true) bool(true) bool(true) bool(false) bool(true) bool(true) bool(false)Here is the outcome of the script.
<?php $var1 = 31; $var2 = 031; $var3 = 0x31; echo "$var1\n"; echo "$var2\n"; echo "$var3\n"; ?>We assign 31 to three variables using three notations. And we print them to the console.
$ php notation.php 31 25 49The default notation is the decimal. The script shows these three numbers in decimal.
$ uname -mo i686 GNU/Linux $ php -a Interactive shell php > echo PHP_INT_SIZE; 4 php > echo PHP_INT_MAX; 2147483647 php >On my 32bit Ubuntu system, an integer value size is four bytes. The maximum integer value is 2147483647.
<?php $var = PHP_INT_MAX; echo var_dump($var); $var++; echo var_dump($var); ?>We assign a maximum integer value to the $var variable. We increase the variable by one. And we compare the contents.
$ php boundary.php int(2147483647) float(2147483648)As we have mentioned previously, internally, the number becomes a floating point value.
<?php # number of baskets $baskets = 16; # number of apples in each basket $apples_in_basket = 24; # total number of apples $total = $baskets * $apples_in_basket; echo "There are total of $total apples \n"; ?>In our script, we count the total amount of apples. We use the multiplication operation.
$ php apples.php There are total of 384 applesThe output of the script.
<?php $a = 1.245; $b = 1.2e3; $c = 2E-10; $d = 1264275425335735; var_dump($a); var_dump($b); var_dump($c); var_dump($d); ?>In this example, we have two cases of notations, that are used by scientists to denote floating point values. Also the $d variable is assigned a large number, so it is automatically converted to float type.
$ php floats.php float(1.245) float(1200) float(2.0E-10) float(1264275425340000)This is the output of the above script.
$ php -a Interactive shell php > echo 1/3; 0.333333333333 php > $var = (0.333333333333 == 1/3); php > var_dump($var); bool(false) php >In this example, we compare two values that seem to be identical. But they yield unexpected result.
<?php # 100m is 0.1 km $distance = 0.1; # 9.87s is 9.87/60*60 h $time = 9.87 / 3600; $speed = $distance / $time; echo "The average speed of a sprinter is $speed \n"; ?>In this example, it is necessary to use floating point values.
$speed = $distance / $time;To get the speed, we divide the distance by the time.
$ php sprinter.php The average speed of a sprinter is 36.4741641337This is the output of the sprinter script. 36.4741641337 is a floating point number.
String
is a data type representing textual data in computer programs.
Probably the single most important data type in programming.
<?php $a = "PHP "; $b = 'PERL'; echo $a, $b; echo "\n"; ?>We can use single quotes and double quotes to create string literals.
$ php strings.php PHP PERLThe script outputs two strings to the console. The \n is a special sequence, a new line. The effect of this character is like if you hit the enter key when typing text.
<?php $names = array("Jane", "Lucy", "Timea", "Beky", "Lenka"); print_r($names); ?>The
array
keyword is used to create a collection of elements. In our case we
have names. The print_r
function prints a human readable information about a
variable to the console.
$ php init.php Array ( [0] => Jane [1] => Lucy [2] => Timea [3] => Beky [4] => Lenka )Output of the script. The numbers are indeces by which we can access the names.
NULL
. Basically, the data type means non existent,
not known or empty.
<?php $a; $b = NULL; $c = 1; unset($c); $d = 2; if (is_null($a)) echo "\$a is null\n"; if (is_null($b)) echo "\$b is null\n"; if (is_null($c)) echo "\$c is null\n"; if (is_null($d)) echo "\$d is null\n"; ?>In our example, we have four variables. Three of them are considered to be NULL. We use the
is_null()
function to determine, if the variable
is NULL.
$ php null.php $a is null $b is null $c is nullOutcome of the script.
php > echo "45" + 12; 57 php > echo 12 + 12.4; 24.4In the above example, we have two examples of implicit type casting. In the first statement, the string is converted to integer and added to the second operand. If either operand is a float, then both operands are evaluated as floats, and the result will be a float.
php > $a = 12.43; php > var_dump($a); float(12.43) php > $a = (integer) $a; php > var_dump($a); int(12) php > $a = (string) $a; php > var_dump($a); string(2) "12" php > $a = (boolean) $a; php > var_dump($a); bool(true)This code snippet shows explicit casting in action. First we assign a float value to a variable. Later we cast it to integer, string and finally boolean data type.