Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Tutorials > PHP Tutorials

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

PHP Tutorials PHP Tutorials

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-10-2007, 05:11 PM
clookid's Avatar   
clookid clookid is offline
Programmer
 
Join Date: Jan 2007
Posts: 148
Credits: 0
Rep Power: 7
clookid is on a distinguished road
Default Operators

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:

PHP Code:
  $foo 3;
  
$foo $foo // 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 :

PHP Code:
  $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:

PHP Code:
    *

      
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.

PHP Code:
   $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):

PHP Code:
   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.

PHP Code:
   $a == $b    Returns true if the contents of $a match those of $bfalse 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.

PHP Code:
&& 

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.

PHP Code:
&! 

AND NOT


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

PHP Code:
|| 

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.

PHP Code:


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:

PHP Code:
($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

Last edited by clookid; 01-10-2007 at 11:53 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-11-2007, 08:35 PM
xtraze xtraze is offline
Programming God
 
Join Date: Dec 2006
Location: Sri lanka
Posts: 921
Credits: 5
Rep Power: 0
xtraze is on a distinguished road
Send a message via MSN to xtraze Send a message via Skype™ to xtraze
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java:Reference - Operators John Java Tutorials 0 12-09-2006 10:05 AM
Operators (increment and decrement) Sionofdarkness Java Help 3 07-29-2006 12:08 PM


All times are GMT -5. The time now is 11:41 PM.

Contest Stats

Xav ........ 1097.16
MeTh0Dz|Reb0rn ........ 986.37
morefood2001 ........ 850.04
John ........ 841.93
WingedPanther ........ 684.54
marwex89 ........ 638.26
Brandon W ........ 492.36
chili5 ........ 292.12
Steve.L ........ 188.37
orjan ........ 187.41

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 79%

Ads