Searching...
Tuesday 18 June 2013

PHP Basics - Part 6 :: Expressions – Part 2

Assignment Operators

The assignment operators assign a data value to a variable. The simplest form of assignment operator just assigns some value, while others (known as shortcut assignment operators) perform some other operation before making the assignment. Table below lists examples using this type of operator.

Example Label Outcome
$a = 5 Assignment $a equals 5
$a += 5 Addition-assignment $a equals $a plus 5
$a *= 5 Multiplication-assignment $a equals $a multiplied by 5
$a /= 5 Division-assignment $a equals $a divided by 5
$a .= 5 Concatenation-assignment $a equals $a concatenated with 5
Assignment Operators

String Operators

PHP’s string operators (see Table below) provide a convenient way in which to concatenate(to connect) strings together. There are two such operators, including the concatenation operator (.) and the concatenation assignment operator (.=) discussed in the previous table.

Example Label Outcome
$a = "abc"."def"; Concatenation $a is assigned the string "abcdef"
$a .= "ghijkl"; Concatenation-assignment $a equals its current value concatenated with "ghijkl"
String Operators

Here is an example involving string operators:

// $a contains the string value "Spaghetti & Meatballs";
$a = "This blog" . “is";
$a .= "awesome."
// $a contains the value "This blog is awesome."

Increment and Decrement Operators

The increment (++) and decrement (--) operators listed in Table below present a minor convenience in terms of code clarity, providing shortened means by which you can add 1 to or subtract 1 from the current value of a variable.

Example Label Outcome
++$a, $a++ Increment Increment $a by 1
--$a, $a-- Decrement Decrement $a by 1
Increment and Decrement Operators

These operators can be placed on either side of a variable, and the side on which they are placed provides a slightly different effect. Consider the outcomes of the following examples:

$inv = 15; // Assign integer value 15 to $inv.
$oldInv = $inv--; // Assign $oldInv the value of $inv, then decrement $inv.
$origInv = ++$inv; // Increment $inv, then assign the new $inv value to $origInv.

As you can see, the order in which the increment and decrement operators are used has an important effect on the value of a variable. Prefixing the operand with one of these operators is known as a preincrement and predecrement operation, while postfixing the operand is known as a postincrement and postdecrement operation.

Logical Operators

Much like the arithmetic operators, logical operators (see Table below) will probably play a major role in many of your PHP applications, providing a way to make decisions based on the values of multiple variables. Logical operators make it possible to direct the flow of a program and are used frequently with control structures such as the if conditional and the while and for loops. Logical operators are also commonly used to provide details about the outcome of other operations, particularly those that return a value:

file_exists("filename.txt") OR echo "File does not exist!";

One of two outcomes will occur:
  • The file filename.txt exists.
  • The sentence “File does not exist!” will be output.

Example Label Outcome
$a && $b AND True if both $a and $b are true
$a AND $b AND True if both $a and $b are true
$a || $b OR True if either $a or $b is true
$a OR $b OR True if either $a or $b is true
!$a NOT True if $a is not true
NOT $a NOT True if $a is not true
$a XOR $b Exclusive OR True if only $a or only $b is true
Logical Operators

0 comments:

Post a Comment

 
Back to top!