Jump to content

Operators

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
clookid

clookid

    Programmer

  • Members
  • PipPipPipPip
  • 125 posts
Operators

There are some operators that work directly on variables to save you time and effort in programming. For example, ++ and -- work to add and remove 1 from contents of the variable given:

  $foo = 3;
  $foo = $foo + 1 // The 'old' way of doing this.
  $foo++; // $foo is now 5
  ++$foo; // $foo is now 6

The difference between ++$foo and $foo++ is the point at which the number is added. If you're using $foo in a statement more complicated than those above, you'll find that :

  $foo = 3;
  $bob = $foo++;  // $bob is 3, $foo is 4.
  $foo = ++$bob;  // $bob and $foo are both 4

In the first line, we set $foo to 3. The following line reads in plain English as "Set $bob to the value of $foo, then add one to $foo", whereas the last line reads "Add one to $bob and set $foo to the new value". In general, if you have to think about what's going to happen, then you should write your code in a more simple way -- think of the poor git who's going to replace you! You're excused if:

    *

      you're not coding for money
    *

      you won't be replaced
    *

you remember why you do everything that you do and never have to check your own notes.

If you want to add/subtract more than one, use the += and -= operators. For strings you can use the .= operator.

   $foo = "Fred is ";
   $foo .= " Bob's friend"; // $foo now contains "Fred is  Bob's friend";

   $foo = 1;
   $bob = 3;
  
   $foo += 2;  // $foo is now 3
   $foo += $bob; // $foo is now 6

The usual operators

Use + to add, - to subtract, / to divide, * to multiply, and % to get the modulus (remainder). These make sense with numbers. They don't with words. Use . to join multiple items (like pieces of text):

   echo "You have " . $items . " items.";

Comparison operators

Use comparison operators in tests like if, while and for loops to test a condition. These operators will compare the items on either side and will return true or false. Again, most of these only work meaningfully with numbers.

   $a == $b    Returns true if the contents of $a match those of $b, false otherwise.
   $a < $b     True if $a is less than $b
   $a > $b     True if $a is greater than $b
   $a <= $b    True if $a is less than, or equal to $b
   $a >= $b    True if $a is greater than, or equal to $b

Logical operators

As above, these operators will return true or false depending on the outcome of the test it performs on the two sides of the operator. It takes each side to be a binary value, either true or false. In PHP, 0 and NULL are false, everything else (including negative numbers) is TRUE.

&&


AND


Returns true if both sides of the operator evaluate to true. However, if the left hand side isn't TRUE, PHP won't bother testing the right hand side.

&!


AND NOT


Returns true if the left side is TRUE and right side is FALSE.

||


OR


Returns true if the left side or the right side is true. Note that PHP won't bother checking the right hand side if the left hand side is true.

!


NOT


This is one-sided only. ! $a will be TRUE if $a is FALSE.

Because PHP (Like C and Perl) "short-circuits" these operators when it can, you can use them as control structures:

($fp = fopen("filename.txt", 'r')) || die("Couldn't
  open file for read");

PHP runs the first side, ($fp = fopen("filename.txt", 'r')). If the fopen() function call opens the file and the file pointer is stored in $fp, then this side of the line returns TRUE. || means "one side or the other (or both) must be true", PHP doesn't bother running the right hand side because it knows that one side is already true which is what the operator requires. Now, if the file-open function had failed, the left hand side would've returned FALSE so PHP would -=have=- to check the right hand side to see if that's true. The right hand side runs the die() function which quits PHP with an error message, in this case "Couldn't open file for read". In short, PHP quits if it can't open the file.

This tutorial was written by another one of my friends, if you would like to use this tutorial please send me a PM

#2
xtraze

xtraze

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 910 posts
a Simple shopping cart and some more.
Great. And I have just googled and found some and as you saif before I'm not going to put them here. PM me if it's OK.