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

Database

 . A Database is a repository for collection of related data or facts. A database contains a collection of related items or facts arranged in a specific structure. The most obvious example of a non computerized database is a telephone directory. Telephone companies now use an electronic database program to produce their printed phone books. Sometimes, we see a specialized phone book that is sorted not only by last name, but by other items such as phone number or street address. These books are easy to produce because the telephone company's electronic database can sort and organize the data in many different ways. Database programs can also sort lists of data, arranging them in alphabetical, numeric, or chronological order and can search or query data with different conditions. Features of Database  The data in a database should have following features Shared : Data in a database is sharing among different user and application. Validity :  Data should be correct with resp...

Introduce Linux, UNIX and Linux distribution.

 . Linux Linux is an open source operating system. The term Linux is actually referred to mean a kernel of Unix like operating system developed by a Finish software architect Linus Torvald. The name Linux is derived from Linus' Unix. Linux was originated from the inspiration of a small Unix like operating system MINIX by A.S Tanenbum. Linux is a complete multiuser, multiprocessing, secure and stable operating system which is also considered as UNIX clone. However no code from the proprietary from AT and T is included in Unix. It has flavor of Unix but is not Unix. UNIX  UNIX is a layered operating system. The innermost layer is the hardware that provides the service for the OS. The operating system, referred to in UNIX as the kernel, interacts directly with the hardware and provides the services to the user program. These user programs don't need to know anything about the hardware. They just need to know how to interact with the kernel and it's up to the kernel to provide ...