Searching...
Wednesday 19 June 2013

PHP Basics - Part 6 :: Expressions–Part 3

 

Equality Operators

Equality operators (see Table below) , as the name suggest, are used to compare two values, testing for equivalence.

Example Label Outcome
$a == $b Is equal to True if $a and $b are equivalent
$a != $b Is not equal to True if $a is not equal to $b
$a === $b Is identical to True if $a and $b are equivalent and $a and $b have the same type
Equality Operators

It is a common mistake for even experienced programmers to attempt to test for equality using just one equal sign (e.g., $a = $b). Keep in mind that this will result in the assignment of the contents of $b to $a, thereby not producing the expected results(more about assignment operators). $a = 2 <- this expression assigns 2 to variable $a, whereas, $a == 2 <— this expression checks if value of variable $a is equal to 2 or not, and , hence, returns true or false respectively.

Comparison Operators

Comparison operators (see Table below), like logical operators, provide a method to direct program flow through an examination of the comparative values of two or more variables.

Example Label Outcome
$a < $b Less than True if $a is less than $b
$a > $b Greater than True if $a is greater than $b
$a <= $b Less than or equal to True if $a is less than or equal to $b
$a >= $b Greater than or equal to True if $a is greater than or equal to $b
($a == 12) ? 5 : -1 Ternary If $a equals 12, return value is 5; otherwise, return value is –1
Comparison Operators

Note:- The comparison operators should be used only for comparing numerical values. Although you may be tempted to compare strings data types with these operators, you will most likely not arrive at the expected outcome if you do so. There is a substantial set of predefined functions that compare string values we’ll be discussing it in detail later on the go.

Bitwise Operators

Bitwise operators examine and manipulate integer values(more about data types) on the level of individual bits that make up the integer value (thus the name). To fully understand this concept, you need at least an introductory knowledge of the binary representation of decimal integers. Table below presents a few decimal integers and their corresponding binary representations.
Decimal Integer Binary Representation
2 10
5 101
10 1010
12 1100
145 10010001
1,452,012 101100010011111101100
Binary Representations

The bitwise operators listed in Table below are variations of some of the logical operators but can result in drastically different outcomes.
If you are interested in learning more about binary encoding and bitwise operators and why they are important, check out Randall Hyde’s massive online reference, “The Art of Assembly Language Programming”.

Example Label Outcome
$a & $b AND And together each bit contained in $a and $b
$a | $b OR Or together each bit contained in $a and $b
$a ^ $b XOR Exclusive—or together each bit contained in $a and $b
~ $b NOT Negate each bit in $b
$a << $b Shift Left $a will receive the value of $b shifted left two bits
$a >> $b Shift Right $a will receive the value of $b shifted right two bits
Bitwise Operators

You have been introduced to operands and various operators namely Arithmetic, Assignment, String, Increment & Decrement, Logical, Equality, Comparison and Bitwise Operators, including there associativity and precedence. For any doubts related to expressions and previous lectures comment below or contact me.

0 comments:

Post a Comment

 
Back to top!