Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Operations and Comparisons

July 21, 2000

We've seen that PHP scalar variables contain values, whether numeric or string. In the course of your program, you'll often want to operate on these values -- an operation can include an arithmetic calculation, for example, or a comparison between values to find which is smaller, larger, or so on. PHP provides a basic set of operators good for most common data manipulations.

In fact, we've already worked with one operator -- the assignment operator -- also known as the equal sign used to assign a value to a variable, such as:

$val1 = 5;

Arithmetic operators, as the name implies, let you perform math on one or more values. Computers love doing math, so PHP always gets a kick out of performing arithmetic operations. The table of expressions below summarizes PHP's arithmetic operators and how they work. For all examples, assume that $val1 contains 5 and $val2 contains 12.

PHP Arithmetic Operators
Operator Name Description Example Yield
+
Addition Sum of two expressions.
$val1 + $val2
17
-
Subtraction Difference between two expressions.
$val1 - $val2
-7
*
Multiplication Product of two expressions.
$val1 * $val2
60
/
Division Dividend of two expressions.
$val2 / $val1
2.4
%
Modulus Remainder left over after the division of two expressions.
$val2 % $val1
2
++
Unary increment Increases value of a single variable by 1.
$val1++
6
--
Unary decrement Decreases value of a single variable by 1.
$val1--
4

Note in the table above that we often refer to the term "expressions", as in "sum of two expressions". It would be too limiting to say "sum of two variables", because we could write code such as:

$val3=(5-$val1)+$val2;

This statement actually consists of three expressions: the assignment operation, between $val3 and everything to the right of the equal sign; the addition operation between $val2 and the expression to the left of the plus sign; and finally, the subtraction operation within parentheses. Why the parentheses? This raises the issue of operator precedence. When an expression contains several operators, PHP has to determine in what order to perform the operations. Sometimes this matters greatly. Consider:

$val1 = 2 * 4 - 3;

What value should be assigned to $val1? If we first multiply 2 and 4, and then subtract 3, the result would be 5. But if we first subtract 3 from 4, and then multiply that by 2, the result would be 2. PHP has a set of precedence rules which define which operations take precedence -- in this case, multiplication is of higher precedence than subtraction, so in this case PHP would come up with the first interpretation, resulting in a value of 5.

To be honest, the operator precedence rules can be quite difficult to remember. Rather than memorizing them all, a better approach is often to use parentheses to group operations -- this way, we can determine our own precedence. For example, we can use parentheses to determine either of the previous two interpretations:

$val1 = (2 * 4) - 3;
$val1 = 2 * (4 - 3);

The first example will yield 5, while the second will yield 2. You're usually better off using parenthetical grouping this way rather than relying on PHP's implicit precedence rules, both because they can be difficult to remember, and because the above code will be easier to understand by a human at a later time.

String operators work with characters rather than numbers. And of course you can't really add, subtract, or multiply the words "hot" and "tamale". Then again, maybe you can "add" them, if by that you mean string them together. In fact, this is called concatenation, and it is the only string operator available in PHP. Rather than a plus sign, the PHP concatenation operator is a single dot, or period. Imagine that $firstName contains "Woodrow".

$fullName = $firstName . "Wilson";

Now, $fullName will contains "WoodrowWilson". Whoops -- we'd probably want a space between those words, something to keep in mind when you concatenate strings:

$fullName = $firstName . " Wilson";

Simple, indeed.

PHP's basic assignment operator is already familiar, being a simple equal sign (=). You can also combine the equal sign with an arithmetic or string operator to make a "shortcut" assignment. Confused? Let's illustrate -- suppose that you'd like to add a value of 5 to whatever value is currently contained in $val1:

$val1 += 5;

Thus, if $val1 was previously valued at 10, after the above statement it would contain 15. Similarly, you can use the same shortcut for string concatentation, if you want to add a string to the end of an existing string. Suppose $name contains "Thelonius":

$name .= " Monk";

And now $name will be "Thelonius Monk".

Comparing two values is frequently useful -- is a purchase total below a certain threshold, or is does one word come before another alphabetically? PHP's team of comparison operators is here to help! A comparison operator ultimately returns either a true or a false value, depending on the result of the comparison. What you do with this result value can vary, and we'll see this more closely when we talk about control statements. For the examples below, let's assume the following variable values:

$val1 = 15;
$val2 = 7;
$name1 = "Martin";
$name2 = "Michael";

PHP Variables and Web Forms
Welcome to PHP
PHP Comparison Operators


Up to => Home / Authoring / Languages / PHP / Welcome




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers