Bitwise Operators in PHP

And Bit Operator : &
usage ( $x & $y ), Bits that are set in both $x and $y are set.

Or bit operator ( inclusive or ) : |
usage ( $x | $y ), Bits that are set in either $x or $y are set.

Xor bit operator ( exclusive or ) : ^
usage ( $x ^ $y ), Bits that are set in $x or $y but not both are set.

Not bit operator : ~
usage ( ~$x ), Bits that are set in $x are not set, and viceversa.

Shift Left bit operator : <<
usage ( $x << $y ), Shift the bits of $x $y steps to the left (each step means “multiply by two”)

Shift right bit operator : >>
usage ( $x >> $y ), Shift the bits of $x $y steps to the right (each step means “divide by two”)

Logical Operators in PHP programming

Logical Operators are using for comparing between Boolean values and will return a Boolean result. Boolean values are TRUE and FALSE.

And Operator : and
Usage : ( $x and $y ), It will return TRUE if both $x and $y are TRUE, otherwise it will return FALSE.

Or Operator : or
Usage : ( $x or $y ). It will return TRUE if $x or $y is TRUE, otherwise it will return FALSE

Xor Operator : xor
Usage : ( $x xor $y ) , It will return TRUE if either $x or $y is TRUE, but both should not be TRUE. It will return FALSE if both are TRUE or FALSE.

Not Operator : !
usage ( !$x) , It will return TRUE if $x is not TRUE

And Operator : &&
Usage : ( $x && $y ), It will return TRUE if both $x and $y are TRUE, otherwise it will return FALSE.

Or Operator : ||
Usage : ( $x || $y ). It will return TRUE if $x or $y is TRUE, otherwise it will return FALSE

Here we can see that two operators are for And Operator ( and , && )
and
two operators are for Or Operator ( or, || ).
Both will do the same job.

Comparison Operators in PHP programming

Comparison Operators are used for compare two values.There are several types of comparison operators in PHP programming. See the details below.

Equal Operator : ==
Usage : ($x==$y) , It will return TRUE if value of $x is equal to $y, otherwise it will return FALSE.

Identical Operator : ===
It will check the value and type. Both should be same.
usage : ($x===$y), It will return TRUE if value and type of $x is same as value and type of $y, otherwise it will return FALSE.

Not Equal Operator : !=
Usage : ( $x != $y ), It will return TRUE if value of $x is not equal to value of $y, otherwise it will return FALSE.

Not Equal Operator : <>
Usage : ( $x <> $y ), It will return TRUE if value of $x is not equal to calue of $y, otherwise it will return FALSE.

Not Identical Operator : !==
Usage : ($x !== $y ), It will return TRUE if value of $x not equal to value of $y or type of $x is not same as type of $y.

Less than Operator : <
Usage ( $x < $y ), It will return TRUE if value of $x is less than the value of $y, Otherwise it will return FALSE.

Greater than Operator: >
Usage ( $x > $y ), It will return TRUE if value of $x is greater than the value of $y, Otherwise it will return FALSE.

Less than or Equal to Operator : <=
Usage ( $x <= $y ), It will return TRUE if value of $x is less than or Equal to value of $y, Otherwise it will return FALSE.

Greater than or Equal to Operator : >=
Usage ( $x >= $y ), It will return TRUE if value of $x is greater than or equal to value of $y, Otherwise it will return FALSE

These are the main comparison Operators in PHP. You should be very thorough about all these, because its usage is very much needed in our PHP script programming.

PHP Value Assignment Operators

Here we can learn about the operators used for assigning values in PHP.

Operator “=”
Usage Sample program

<?php
$x = 10;
$y = $x;
?>

Operator “+=”
x +=y is same as x = x + y
Usage Sample program

<?php
$x = 10;
$y = 20;
$x += $y;
echo $x ;
?>

Output of this PHP program will be :
30

Operator “-=”
x -=y is same as x = x – y
Usage Sample program

<?php
$x = 30;
$y = 20;
$x -= $y;
echo $x ;
?>

Output of this PHP program will be :
10

Operator “*=”
x *=y is same as x = x * y
Usage Sample program

<?php
$x = 5;
$y = 6;
$x *= $y;
echo $x ;
?>

Output of this PHP program will be :
30

Operator “/=”
x /=y is same as x = x / y
Usage Sample program

<?php
$x = 10;
$y = 5;
$x /= $y;
echo $x ;
?>

Output of this PHP program will be :
2

Operator “.=”
x .=y is same as x = x . y ( Concatenation operation )
Usage Sample program

<?php
$x = "Syam";
$y = "Here";
$x .= $y;
echo $x ;
?>

Output of this PHP program will be :
Syam Here

Operator “%=”
x %=y is same as x = x % y ( Division Remainder )
Usage Sample program

<?php
$x = 10;
$y = 3;
$x %= $y;
echo $x ;
?>

Output of this PHP program will be :
1

Arithmetic calculation operators in php

There are several operators in PHP available for arithmetic calculations and all.

Addition Operator : +
Use : For the addition of two numbers.
Example

<?php
$a = 30;
$b = 10;
$c = $a + $b;
echo $c;
?>

Output will be ( 30 + 10 ):
40

Subtraction Operator : –
Use : Subtract one value from another value
Example

<?php
$a = 30;
$b = 10;
$c = $a - $b;
echo $c;
?>

Output will be ( 30 – 10) :
20

Multiplication Operator : *
Use: Mutiply one value with another
Example

<?php
$a = 30;
$b = 10;
$c = $a * $b;
echo $c;
?>

Output will be ( 30 * 10) :
300

Division Operator : /
Use : for the division of one value by another
Example

<?php
$a = 30;
$b = 10;
$c = $a / $b;
echo $c;
?>

Output will be ( 30 / 10) :
3

Modulus Operator : %
It will return the division remainder
Example 1:

<?php
$a = 30;
$b = 10;
$c = $a % $b;
echo $c;
?>

Output will be remainder of 30 / 10 :
0

Example 2:

<?php
$a = 5;
$b = 2;
$c = $a % $b;
echo $c;
?>

Output will be remainder of 5 / 2) :
1

Example 3:

<?php
$a = 10;
$b = 4;
$c = $a % $b;
echo $c;
?>

Output will be the remainder of 10 / 4 :
2

Increment Operator : ++
Use: Its a short code for using against + 1
Example

<?php
$a = 30;
$a++;
echo $a;
?>

Output will be ( 30 + 1 ):
31

Decrement Operator : —
Use: Its a short code for using against – 1
Example

<?php
$a = 30;
$a--;
echo $a;
?>

Output will be ( 30 – 1 ):
29

Negation Operator : will return the opposite of number
Example

<?php
$a = 30;
$b = -$a;
echo $b;
?>

Output will be :
-30

strpos() – PHP string first occurance position

PHP strpos function is using for checking the occurrence position of a character or string within a string.It will return the numeric position number of the first occurrence of the searched string.
If strpos function failed to find the searched string , it will return “FLASE”.

For example if we want to know the position of letter “M” in word “MALAYALAM” , strpos function will return the value 0 ( zero ) . It means the searched letter’s position is first on that string.
Please note : Note count is starting from 0 (zero) not from 1 (one )

See the following sample PHP program with strpos function

<?php
$main_string="MALAYALAM";
$search_string="M";
$position  = strpos($main_string, $search_string);
if ($pos==false)
 {
  echo "Searched string was not found in the main string";
 }
else
 {
 echo "found at position ".$position ;
 }
?>

The output of this PHP script will be :
found at position 0

See another example

<?php
$main_string="Hello world";
$search_string="world";
$position  = strpos($main_string, $search_string);
if ($pos==false)
 {
  echo "Searched string was not found in the main string";
 }
else
 {
 echo "found at position ".$position ;
 }
?>

The output of this PHP script will be :
found at position 6

Ok. Now we got the idea about how to get a string’s first occurrence position in another string. But in some situations we need to get the first occurrence position with a offset. It means if need to search the occurrence of a string from a fixed position of the main string than from its starting. ( like we need to start the search from position 3 and avoid first 4 position of the main string ).
You will get a clear picture after seeing the following sample program

<?php
$main_string="MALAYALAM";
$search_string="M";
$position  = strpos($main_string, $search_string, 1);
if ($pos==false)
 {
  echo "Searched string was not found in the main string";
 }
else
 {
 echo "found at position ".$position ;
 }
?>

Output of this program will be
found at position 8

By using the optional offset parameter 1 with strpos function , search will ignore anything before the specified offset value 1 in the main string. So in this example the first M in string MALAYALAM wont consider because it is before the specified offset value 1.

echo in PHP

echo is using for output one or more strings in PHP.

echo is basically a language construct and not a function. So we can use without parentheses with it.

We can run shell commands also using shell_exec in PHP echo syntax.But its only used in advanced PHP programming and we will discuss more about this in our later posts.

We can use the HTML formatting tags with PHP echo statement

See the following sample program with echo in PHP programs

<?php
echo "Good Morning";
?>

Its out put will be :
Good morning

<?php
echo "<b>Good Morning</b>";
?>

Its out put will be :
Good morning

<?php
$variable1="PHP";
$variable2="MySQL";
echo $variable1."&".$variable2;
?>

Its out put will be :
PHP&MySQL

<?php
$variable1="PHP";
$variable2="MySQL";
echo $variable1."<br />".$variable2;
?>

Its out put will be :
PHP
MySQL

strlen() – PHP string length function

While our PHP programming , in some cases we need to get the length of a string for processing. strlen() if the function in PHP to get the length of a string.

See the sample program with strlen() function:

<?php
$variable="Check length";
echo strlen($variable);
?>

It will output the result

12

strlen function will count the blank spaces also contains in the string.

For looping through a string, we can use this function to know the end of loop by storing the length of string to a variable. And in other cases we can use this function for data checking like password length and all.

It will return 0 ( zero ) if the string is empty.

Concatenate strings in PHP using concatenation operator

Concatenation is the process of joining multiple strings into one.In PHP concatenate  strings is doing by concatenation operator ( “.” dot symbol ).Using of concatenation is highly used one in PHP programs. We need to use this for string concatenations and for printing the results in desired order etc.

Here is one Sample PHP program with Concatenate strings  using concatenation operator

<?php
$variable_1 =  "Syam";
$variable_2 = "is a PHP professional";
$variable_3 = "From India";
echo $variable_1." ".$variable_2." ".$variable_3;
?>

Output of this PHP program will look like
Syam is a PHP professional From India

In Some other cases we need to store the concatenated strings as a variable

See the following code

<?php
$variable_1 =  "Syam";
$variable_2 = "is a PHP professional";
$variable_3 = "From India";
$variable_4 = $variable_1." ".$variable_2." ".$variable_3;
echo $variable_4;
?>

In this code concatenated string is stored in the variable “$variable_4”  for later use in the program. Above code will ouput the same result as our previous example.

Variable Declarations in PHP

Variables are used for storing the data like strings, numbers , arrays etc in programs.

In PHP a variable starts with a $ sign. Forgetting to add $ sign is a very common mistake for the beginners.

Example for a PHP variable : $variable

And the declaration will look like  $variable = VALUE ;

A PHP variable name can start with a letter or underscore ( _ ) symbol and it shouldn’t contain any space or other special characters like *, % , # etc.

See the valid and invalid variable names

  • $employee name   ( Invalid variable )
  • $employee_name ( Valid variable )

There is no type declaration is needed in PHP variable declaration.variable will automatically converted to the type of assigned value to the variable.

For example :

  • $variable = 10 ; ( Here $variable will treat as integer automatically, we dont need to specify type as Integer at the declaration command )
  • $variable = “I love you”; ( Here $variable will treat as a string automatically, we dont need to specify type as string at the declaration command )

PHP Sample program with variable declaration

<?php
$variable_age = 30;
$variable_name= "Syam";
$variable_desp="has aged";
echo $variable_name."  ". $variable_desp."  ".$variable_age;
?>

The result will be look ke

Syam has aged 30

Share
Share