Skip to main content

Operators and Variables in PHP

 .

Operator 

An Operator is a symbol that perform certain mathematical and logical operation on operator. It is define as an action upon the two possible values. Operators are used to program operation on the variables and values. 

PHP Operators are of following types:
  1. Arithmetic Operator
  2. Assignment Operator 
  3. Logical Operator
  4. Comparison Operator
  5. Conditional Operator
  6. Increment / Decrement Operator
  7. Concatenation Operator
1. Arithmetic Operator
Arithmetic operator are used to preform arithmetic operation on numeric values.
2. Assignment Operator
An assignment operator is mainly responsible for assigning a value to a variable in a program. It is used to value to the variable. 

Assignment

Same As

Description

x = y

x = y

left operand and right operand are same.

x + = y

X = X + y

left operand get added value of right operand.

x - = y

x = x - y

left operand get subtracted value of right operand.

x * = y

x = x * y

left operand get multiplication value of right operand.

x / = y

x = x / y

left operand get divide of value of right operand.

x % = y

x = x % y

left operand get Remainder of value of right operand.


3. Logical Operator
They are used to combine condition statement.

Operator

Name

Example

Description

&&

Logical AND

x && y

True if both x & y are true.

||

Logical OR

x || y

True if one of them is true.
False if both x & y are false.

!

Logical NOT

! x

True if x is false.

and

Logical AND

x and y

True if both operands are true.  

OR

Logical OR

x or y

True if either one operand is true.

4. Compression Operator

Operator

Name

Example

Result

==

Equal

x == y

return true x is equal

===

Identical

x === y

x & y are  same

!=

Not equal

x!= y

x is not equal to y

!==

Not identical

x!==y

x is not equal to same y

> 

Greater than

x>y

True x is greater than y

<

Less than

x<y

True y is less than x

>=

Greater than & equal

x>=y

True x is greater equal to y

<=

Less than & equal

x<y

True y is less equal to x

5. Conditional Operator
An operator which takes pair of operator ("?") to evaluate an expression. It is also called Ternary operator because its takes three operands for manipulation. 
Syntax:
        expression 1? expression 2 : expression 3
Example:
    <? php
        $x = 5;
         $y = $x%2 == 0? "It is even". "It is odd';
            echo $y;
    ?>
Output:
It is odd

6. Increment / Decrement Operator
 It is used to increment / decrement values.

Operator

Name

Description

++a

Pre-increment

Increment by 1 & return.

a++

Post-increment

Return a then increment by 1.

--a

Pre-decrement

Decrement by 1 & return.

a--

Post-decrement

Return a then decrement by 1

7. Concatenation (string) Operator

Operator

Name

Example

Result

.

Concatenation

$a.$b

Concatenation of $a & $b

.=

Concatenation

$a.=$b

Appends $a to $b

Example:
    <? php
         $a="Hello";
         $b="Nepal";
         $c=$a.$b;
         $a.="world";
            echo "$c<br>";
            echo $a;
    ?>

Variable

Variable are like container which are the name of the memory location were the value are stored.

Creating / Declaring variable
    In PHP variable name start with $ sign following by the name of variable.
Example:
    <? php
        $text = 5;
        $My-Name = "Gopal";
        $Text1 = "Hello World";
    ?>

Rules of Declaring Variable
  1. A variable stats with $ sign followed bu variable name.
  2. A variable name must start with letter or underscore but cannot start with number.
  3. Variable name can only contain alphabetic characters and underscore (A-Z, 0-9, - ).
  4. Variable name is case-sensitive ($age, $AGE are different).

Global variables & Local Variables

Global Variable
A variables declare outside of the function has a global scape and can only be accessed outside of a function.
Example:
    <? php
        $x = 5;
        function my text( ){
            echo 'value is : $x";
            }
        my text ( );
            echo "value is : $x";
    ?>
Output:
The value is :
The value is : 5

Local Variable
A variable declare within a function has a local scape and can only be accessed within that function.
Example:
    <? php
        function my text ( ){
            $y = 10;
        echo "variable inside function is : $y";
            }
        my text ( );
            echo "variable outside function is : $y";
    ?>




Comments

Popular posts from this blog

BCA mathematics -I (Unit I Set)

 .

BCA Mathematics -I (Unit I Complex Number)

 .

BCA Mathematics -I (Unit I Natural Number)

.