Monday 28 January 2013

PHP Tutorial : Chapter 2 . Variables and Syntax (Part 3)

Integers and Doubles contd.

Numbers can be assigned to variable in any of the three formats :-
1) Decimal
2) Octal
3) Hexadecimal

Decimal is the default format , to assign a number in octal the octal value should be preceded by a leading 0 , and to assign a number in hexadecimal , the number should be assigned with a leading 0x . The following example assigns the value 4 in decimal , octal and hexadecimal .

$a = 4;
$b = 04;
$c = 0x4;

The largest integer that can be assigned to a variable is 2^31 - 1 and the smallest integer is -(2^31-1) .
Keep in mind that double added to an integer will always yield a double . And doubles print with as minimum places after the decimal as possible . For example 3.645+0.055 will yield 3.7 and not  3.700 .

Booleans

Booleans can attain the value true or false . Howerver how will you determine which constant gives what boolean value ?

Suppose we have something like this :-

if($x)
{
//Perform Something
}

$x might not necessarily contain a boolean value , however the expression inside if will be cast into a boolean value during the execution of the code . So what are the rules followed during such a casting ?

a) Remember that a NULL value evaluates to false .
b) Empty string,array evaluates to false .
c) Any numeric value evaluates to false .

Also i will come to variables holding resources and so on while covering databases afterwards . For now just remember that a variable holding a "failed" value such as a failed database connection , or failed socket connection holds a false value in most of the cases . However it may depend on the situation .

1 comment: