11. Operator

Arithmetic operators: (+ Addition),(- subtraction), (* multiplication), (/ division),( % modulus)

Assignment Operators:   =

Comparison Operators:

  •     ==  equal to            [ echo ( 7 == “7” ) ] Only Data  check, if 7 equal 7 otherwise false
  •     ! = not equal to      [ echo (7 != 6)  ]  if 7 doesn’t equal 6, otherwise false
  •     === identical to     [ echo 6=== “6” ]  Data and Data type check. if 6 doesn’t equal 6 or they aren’t same type, otherwise false
  •     < less than                            [ echo 3<5 ]
  •     > greater than                      [ echo 5>4 ]
  •     <= less than equal to
  •     >= greater than equal to

Bitwise Operator:
Binary: Binary is a 0 , 1 and binary base is a 2.
How to get binary 255

1 Byte ( 8 bits )
256 128 64 32 16 8 4 2 1
8 7 6 5 4 3 2 1 0
2 2 2 2 2 2 2 2 2
128 255 1
64 127 1
32 83 1
16 51 1
8 35 1
4 23 1
2 21 1
1 20 1
128 64 32 16 8 4 2 1 total=255
1 1 1 1 1 1 1 1

How to get binary 102

1 Byte ( 8 bits )
128 64 32 16 8 4 2 1
7 6 5 4 3 2 1 0
2 2 2 2 2 2 2 2
64 102 1
32 38 1
16 6 0
8 6 0
4 6 1
2 2 1
1 0 0
64 32 16 8 4 2 1 total=102
1 1 0 0 1 1 0

What is a byte? A byte is made up of 8 bits and the highest value of a byte is 255, which would mean every bit is set.
1 Byte ( 8 bits )

Example Name Result
$a & $b And Bits that are set in both $a and $b are set.
$a | $b Or Bits that are set in either $a or $b are set.
$a ^ $b Xor Bits that are set in $a or $b but not both are set.
~ $a Not Bits that are set in $a are not set, and vice versa.
$a << $b Shift left Shift the bits of $a $b steps to the left (each step means “multiply by two”)
$a >> $b Shift right Shift the bits of $a $b steps to the right (each step means “divide by two”)

$a & $b: And

<?php	
	$a=255;
	$b=102;
	echo $a & $b;
?>	

This would output the number 102, why? Lets see.

128 64 32 16 8 4 2 1
1 1 1 1 1 1 1 1 255
1 1 0 0 1 1 0 102

Only Common bits 64+32+4+2=102

$a | $b: Or

<?php	
	$a=255;
	$b=102;
	echo $a | $b;
?>	

This would output the number 255, why? Lets see.

128 64 32 16 8 4 2 1
1 1 1 1 1 1 1 1 255
1 1 0 0 1 1 0 102

Common & uncommon  bits 128+64+32+16+8+4+2+1=255

$a ^ $b: Xor

<?php	
	$a=255;
	$b=102;
	echo $a ^ $b;
?>	

This would output the number 153, why? Lets see.

128 64 32 16 8 4 2 1
1 1 1 1 1 1 1 1 255
1 1 0 0 1 1 0 102

Only  uncommon  bits 128+16+8+1=153

$a  ~$b: Not
vice versa is  
0110 
1001

<?php	
	$a=255;
	$b=102;
	echo $a & ~$b;
?>	

This would output the number 153, why? Lets see.

128 64 32 16 8 4 2 1
1 1 1 1 1 1 1 1 255
1 1 0 0 1 1 0 102

Common  bits 128+16+8+1=153

$a <<2: Left shift         

<?php	
	$a=16;
	echo $a << 2;
?>	

This would output the number  64, why? Lets see.
16 8 4 2 1
..1 0 0 0 0=  16

1 Byte ( 8 bits )
Place Value 128 64 32 16 8 4 2 1
$a – BEFORE! 0 0 0 1 0 0 0 0 = 16
$a – AFTER! 0 1 0 0 0 0 0 0 = 64

$a >> 2: Right shift   

<?php	
	$a=16;
	echo $a >> 2;
?>	

This would output the number  4, why? Lets see.
16 8 4 2 1
..1 0 0 0 0=  16

1 Byte ( 8 bits )
Place Value 128 64 32 16 8 4 2 1
$a – BEFORE! 0 0 0 1 0 0 0 0 = 16
$a – AFTER! 0 0 0 0 0 1 0 0 = 4

Increment & Decrement:

//echo $i++;      [ post increment ]
//echo $i– –;
//echo ++$i;      [ pre increment ]
//echo — –$i;

Ternary Operators:
Makes code shorter and easier. Ternary operator logic is the process of using “(condition) ? (true return value) : (false return value)” statements to shorten your if/else structures.

<?php (condition)? "true" : "false"; ?>
<?php echo (5>4)? "Five" : "Four"; ?>
<?php
    $agestr = ($age < 16) ? 'child' : 'adult';
?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s