PHP OPERATORS :



In all programming languages, operators are used to manipulate or perform operations on variables and values.
There are many operators used in PHP, so we have separated them into the following categories to make it easier to learn them all.

·         Assignment Operators
·         Arithmetic Operators
·         Comparison Operators
·         String Operators
·         Logical Operators
·         Ternary Operators
·         Incremet/ Decrement Operators

Assignment Operators :


Assignment operators are used to set a variable equal to a value or set a variable to another variable's value. Such an assignment of value is done with the "=", or equal character.

Operator
Same as
Example
=
$x=$y
$x=$y
+=
$x += $y
$x = $x + $y
-+
$x -= $y
$x = $x - $y
*=
$x *= $y
$x = $x * $y
/=
$x /= $y
$x = $x / $y
%=
$x %= $y
$x = $x % $y

Example:


·         $my_var = 4;
·         $another_var = $my_var;

Now both $my_var and $another_var contain the value 4.


Arithmetic Operators:


Operator
English
Example
- $a
Negation
Opposite of $a
$a + $b
Addition
2 + 4
$a - $b
Subtraction
6 - 2
$a * $b
Multiplication
5 * 3
$a / $b
Division
15 / 3
$a % $b
Modulus
43 % 10

PHP Code:


Text Box: $addition = 2 + 4;
$subtraction = 6 - 2;
$multiplication = 5 * 3;
$division = 15 / 3;
$modulus = 5 % 2;
echo "Perform addition: 2 + 4 = ".$addition."<br />";
echo "Perform subtraction: 6 - 2 = ".$subtraction."<br />"; echo "Perform multiplication: 5 * 3 = ".$multiplication."<br />"; echo "Perform division: 15 / 3 = ".$division."<br />";
echo "Perform modulus: 5 % 2 = " . $modulus
. ". Modulus is the remainder after the division operation has been performed. In this case it was 5 / 2, which has a remainder of 1.";


Comparison Operators:

Comparisons are used to check the relationship between variables and/or values.

Comparison operators are used inside conditional statements and evaluate to either true or false. Here are the most important comparison operators of PHP.


Assume: $x = 4 and $y = 5;


Operator
English
Example
Result
==
Equal To
$x == $y
false
!=
Not Equal To
$x != $y
true
Less Than
$x < $y
true
Greater Than
$x > $y
false
<=
Less Than or Equal To
$x <= $y
true
>=
Greater Than or Equal To
$x >= $y
false

PHP Code:


Text Box: $clg_name = "somnath academy";
if ( $clg_name == "somnath academy" ) {
echo "Your Collage name is somnath academy!<br />";
}
else {
echo "Welcome to my homepage!";
}


Output:


Text Box: Your Collage name is somnath academy! //only if collage name is somnath academy


String Operators :
There are two string operators.

The first is the concatenation operator (“ . ”) , which returns the concatenation of the its right and left arguments.
The second is the concatenating assignment operator (“ .= ”), which appends the argument on the right side to the argument on the left side.


Text Box: $a = "somnath";
$b = $a.”academy”; //now b contains “somnath academy”
$a = “somnath”;
$a.=”academy”; //now a contains “somnath academy”



Pre/Post-Increment & Pre/Post-Decrement:


This may seem a bit absurd, but there is even a shorter shorthand for the common task of adding 1 or subtracting 1 from a variable. To add one to a variable or "increment" use the "++" operator:

·         $x++; Which is equivalent to $x += 1; or $x = $x + 1;

To subtract 1 from a variable, or "decrement" use the "--" operator:

·         $x--; Which is equivalent to $x -= 1; or $x = $x - 1;

In addition to this "shorterhand" technique, you can specify whether you want to increment before the line of code is being executed or after the line has executed. Our PHP code below will display the difference.

PHP Code:



Text Box: $x = 4;
echo "The value of x with post-plusplus = " . $x++;
echo "<br /> The value of x after the post-plusplus is " . $x;
$x = 4;
echo "<br />The value of x with with pre-plusplus = " . ++$x; echo "<br /> The value of x after the pre-plusplus is " . $x;

Output:


Text Box: The value of x with post-plusplus = 4
The value of x after the post-plusplus is = 5 The value of x with with pre-plusplus = 5 The value of x after the pre-plusplus is = 5

As you can see the value of $x++ is not reflected in the echoed text because the variable is not incremented until after the line of code is executed. However, with the pre-increment "++$x" the variable does reflect the addition immediately.

Ternary operator:


It is called the ternary operator because it takes three operands - a condition, a result for true, and a result for false. If that sounds like an if statement to you, you are right on the money - the ternary operator is a shorthand (albeit very hard to read) way of doing if statements. Here's an example:


Text Box: <?php
$agestr = ($age < 16) ? 'Child' : 'Not Child';
?>



First there is a condition ($age < 16), then there is a question mark, and then a true result, a colon, and a false result. If $age is less than 16, $agestr will be set to 'child', otherwise it will be set to 'not child'. That one-liner ternary statement can be expressed in a normal if statement like this:


Text Box: <?php
if ($age < 16) {
$agestr = 'child';
} else {
$agestr = 'child';
}
?>

So, in essence, using the ternary operator allows you to compact five lines of code into one, at the expense of some readability.

No comments:

Post a Comment

=>Advanced Java Programming (J2EE)

1.  Syllabus 2. Unit Wise Question/Material 3. Paper 4. Previous Paper