October 19, 2013

Operators in PHP

Operators in PHP

When comparing or processing variables and other values you use operators. Without them, PHP would be more of a word jumble instead of a language. In some unique cases, operators slightly alter the relationship between two variables or their function within PHP. Without further adieu,
here they are..

Basic Operators
Add ( + ): $a = 1; $a = $a + 5; // $a is equal to 6
Subtract ( - ): $s = 10; $s = $s - 5; // $s is equal to 5
Multiply ( * ): $m = 2; $m = $m * 10; // $m is equal to 20
Divide ( / ): $d = 20; $d = $d / 5; // $d is equal to 4
Modulus ( % ) Provides the remainder after division:
$u = 5; $u = $u % 2; // $u is equal to 1

Assignment Operators
Add ( += ): $a = 1; $a += 5; // $a is equal to 6
Subtract ( -= ): $s = 10; $s -= 5; // $s is equal to 5
Multiply ( *= ): $m = 2; $m *= 10; // $m is equal to 20
Divide ( /= ): $d = 20; $d /= 5; // $d is equal to 4
Modulus ( %= ) Provides the remainder after division:
$u = 5; $u %= 2; // $u is equal to 1
Concatenate ( .= ) Join onto the end of a string:
$c = 5; $c .= 2; // $c is now a string, '52'
See Also:
Concatenate – Join together in succession

Comparison Operators
Greater Than ( > ): 2 > 1
Less Than ( < ): 1 < 2
Greater Than or Equal To ( >= ): 2 >= 2 3 >= 2
Less Than or Equal To ( <= ): 2 <= 2 2 <= 3

Short-Hand Plus or Minus one
Also known as:
Increment ( $integer++; )
Decrement ( $integer--; )
Example:
$a = 1;
$a = $a + 1; // $a is now equal to 2
$a++; // $a is now equal to 3
$a--; // $a is now equal to 2 again, same as $a = $a – 1;

@ - Suppress Errors
Placing the commercial at symbol (@) before a function tells PHP to suppress
any errors generated by that function.
Examples:
include('DoesNotExist.txt');
Warning: include(DoesNotExist.txt) [function.include]: failed to open
stream: No such file or directory
@include('DoesNotExist.txt');
// blank output below because the error was suppressed

& - Pass by Reference

References allow two variables to refer to the same content. In other words,
a variable points to its content (rather than becoming that content). Passing
by reference allows two variables to point to the same content under
different names. The ampersand ( & ) is placed before the variable to be
referenced.
Examples:
$a = 1;
$b = &$a; // $b references the same value as $a, currently 1
$b = $b + 1; // 1 is added to $b, which effects $a the same way
echo "b is equal to $b, and a is equal to $a";
b is equal to 2, and a is equal to 2

0 comments:

Post a Comment

  • Blogger news

    This Blog Contains All Topics Related To Internet, Website Help, Interview Questions, News, Results From Various Resources, Visit Daily For More Interesting And Famous Topics.
  • Random Post

  • About

    We Provide All Information Which you Needed. We Maintain This Blog Very Carefully, If You Find Any Mistake or Any Suggestions Then Please Let Us Know, You Can Contact Us By Comments, On FB Page Or On Google+ Page. ~ Thank You