HTMLGoodies
The ultimate html resource
Earthweb.com


About the Double-Underlined Links


Become a Partner




Search Clipart.com:



internet.commerce















HTML Goodies : Beyond HTML : PHP: PHP Tutorial: Expressions and Operators

HTML GOODIES TO GO NEWSLETTER


Other Related Newsletters

PHP Tutorial: Expressions and Operators


By Vince Barnes

quot;Well, I'll be a monkey's uncle!" That's quite an expression! Fun as it may be, however, it's not the sort of expression we're interested in here. In PHP there are two types of expression for us to look at. One is something called a "regular expression", which is used in the manipulation of text. We'll talk more about them later in the series. The other is more mathematical in nature, expressing operations to be performed on values and variables. Here's a simple example:

c = a + b

Assuming a and b are numbers (remember data types?  -- the result would be different if they were strings -- more about that later) this expression would add a and b and put the sum into c (remember again, that the equals sign here makes this an assignment instruction.)  In the expression a + b the plus sign is an arithmetic operator.  Here are some more operators:

Arithmetic Operators:

+ Addition Add two values
- Subtraction Subtract the second value from the first
* Multiplication Multiply two values
/ Division Divide the first value by the second
% Modulus Divide the first value by the second and return only the remainder
(for example, 7 % 5 yields 2)

Comparison Operators:

= = Equal Checks for equal values
= = = Identical Checks for equal values and data types
! = Not Equal Checks for values not equal
! = = Not Identical Checks for values not equal or not the same data type
< Less than Checks for one value being less than the second
> Greater than Checks for one value being greater than the second
< = Less than or Equal to Checks for on value being less than or equal to the second
> = Greater than or Equal to Checks for on value being greater than or equal to the second

Logical Operators:

And Checks if two or more statements are true
&& Same as And
Or Checks if at least one of two statements is true
|| Same as Or
Xor Checks if only one of two statements is true
! Checks if a statement is not true

Increment and Decrement Operators:

++value Pre-Increment Adds 1 to the value before processing the expression which uses the value
--value Pre-Decrement Subtracts 1 from the value before processing the expression which uses the value
value++ Post-Increment Adds 1 to the value after processing the expression which uses the value
value-- Post-Decrement Subtracts 1 from the value after processing the expression which uses the value

The Concatenate Operator

I get in trouble all the time because I ask how this can be considered an "operator", but trust e, the world thinks it is!  The concatenate "operator" is a period (full stop to the English English!)  It simply says "join this to this", so that

$parta = "This is part one "
$partb = "of a sentence!"

print $parta.$partb

prints out "This is part one of a sentence!"

"Huh?" I say!  "That's not a part of a sentence -- that's a whole sentence!!" (Please forgive my pedantic humor!)


Combined Operators
:

There are some combination operators that you may see used (or may use!)  There are those who argue that these combination operators provide an advantage because they reduce typing and reduce the final size of the code.  In my humble opinion, however, they don't represent the way that most of us think of things, and are therefore somewhat confusing.  See what you think.

You remember from earlier examples that = is an assignment operator:

$a = 5

assigns the value 5 to $a.   This assignment can be combined with some other operators like this:

$a += 5

If $a already had a value of 5, then after this instruction it would be 10, as if the full instruction was:

$a = $a + 5

Valid combinations include:  +=  -=  *=  /=

 

Precedence and Associativity.

Think about this operation:

$a = 7 - 4 + 1

If the minus happens before the plus, the result would be 4.  If the plus happens first, the result is 2.  Since predictability is essential, an order of precedence was established, along with something called associativity.  The order of precedence establishes a "rank" for operators, where those with the highest rank will be evaluated first.  When there are several operators of the same rank, they will be processed from left to right if they are left associative, and right to left if they are right associative.  Some operators, such as the comparison operators, have neither left nor right associativity.

The increment and decrement operators  ( ++ and -- ) are right associative, while the others I have discussed here are left associative.  The order of precedence, from highest to lowest (with those of equal precedence on the same line) is as follows:

++  --
*  /  %
+  -
|
&&
| |
and
xor
or

You can override the order of precedence but wrapping part of an expression in parentheses.  The innermost parentheses are processed first, so that:

(7 - 4) + 1 will give a result of 4
7 - (4 + 1) will give a result of 2

2 + 3 * 3 will give a result of 11
2 + (3 * 3) will also give 11
(2 + 3) * 3 will give a result of 15

1 + 3 + 3 * 2 will give a result of 10
1 + ((3 + 3) * 2) will give a result of 13
(1 + (3 + 3)) * 2 will give a result of 14

I like to use parentheses whenever I create a complex expression so that it is easier for me to see what's going on when I come back to some code that I originally wrote months or years ago.  Following this simple rule makes your code closer to self documenting and make you your own best friend!

 

 

Continue to the next part of this Tutorial

Return to the Tutorial Series Index

Tools:
Add htmlgoodies.com to your favorites
Add htmlgoodies.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

IT Management Networking & Communications Web Development Hardware & Systems Software Development Earthwebnews.com

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES