* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Operators
Survey
Document related concepts
Transcript
Operators
Operators are symbols such as + (addition),
- (subtraction), and * (multiplication).
Operators do something with values.
$foo = 25;
$foo – 15;
// $foo and 15 are the operands,
- is the operator
Arithmetic Operators
+
*
/
%
Addition
$a + $b
Subtraction
$a - $b
Multiplication
$a * $b
Division
$a / $b
Modulus (modulus divides the first value by
the second, and returns the remainder e.g.
$foo = 9;
echo ($foo % 4);
// 1 )
Assignment Operators
Assignment operators set the value of variables
=
Assignment
$a = $b
copies $b's
value into $a
=& Reference
$a =& $b
set $a to
reference $b
Concatenation Operator
Concatenation means to put things together.
The concatenation operator puts strings together.
.
Concatenation
appends 2nd value to 1st
$first = "Barack";
$second = "Obama"
Echo ($first . " " . $second);
// Barack Obama
Comparison Operators
== Equals
=== Identical
true if $a = $b
true if $a = $b and of same type
$x = 50;
// integer
$y = "50"; // string
if ($x == $y) {
echo "$x is the same as $y";
}
// 50 is the same as 50
$x = 50;
$y = "50";
// integer
// string
if ($x === $y) {
echo "$x is the same as $y";
} else {
echo "$x is not the same as $y";
}
// 50 is not the same as 50
Comparison Operators
!= Not Equal
!== Not Identical
<
>
<=
>=
true if $a is not equal to $b
true if $a is not identical to $b
Less Than
true if $a is less than $b
Greater Than true if $a is greater than $b
Less Than or Equal to
Greater Than or Equal to
Increment/Decrement Operators
++$a
$a++
--$a
$a--
pre-increment
increments $a by 1,
then returns $a
post-increment returns $a, then
increments $a by 1
pre-decrement decrements $a by 1,
then returns $a
post-decrement returns $a, then
decrements by 1
$a = 10;
echo $a++ . "<br />";
echo $a;
// 10
11
$a = 10;
echo ++$a;
// 11
Logical Operators
AND
true if both $a AND $b are true
$a = 10;
$b = 5;
if ($a > 5 AND $b >= 5) {
echo "This is pretty cool";
}
// This is pretty cool
&& is the same as AND
Logical Operators
OR
true if either $a AND $b are true
$a = 10;
$b = 5;
if ($a > 5 OR $b > 5) {
echo "This is pretty cool";
}
// This is pretty cool
|| is the same as OR