spacer
Web Development Tutorials PHP Tutorials
 Developer Newsletter

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn AJAX
Learn JavaScript
Learn Pear
Free White Papers

Developer Resources
Developer Tools
Developer Content
Survey Software
Dedicated Servers




Introduction to PHP Programming

By PHP Catalyst
2007-11-19


Expressions and Operators

Expressions

PHP can also be called an expression-oriented language that is because everything you write in PHP is an expression. An earlier example where we've assigned 100 to $a, an integer value of 100 or either a string 100 is being stored in the variable $a. You may also assign a string "hello" to $a by writing $a = hello; Now, if you were to write $b = $a, then $b is containing the string "hello".

Using PHP it is also possible to assign a function to a variable by writing $a = function_a(); and this is all possible because PHP is an expression-oriented language as well. Another good example of expression orientation is pre- and post-increment and decrement. This is achieved using variable++ and variable--. These are known as increment and decrement operators. For example: if you want to increment $a by 1, you can simply write '$a++' or '++$a'. But if you want to add more than 1 to $a, say 4, then you will have to write '$a++' or '++$a' multiple times. A better practice is to write '$a = $a + 4'. '$a + 4' evaluates to the value of $a plus 4, and is assigned back into $a, which results in incrementing $a by 4. This can also be written as '$a += 3' which executes faster than previous ways of incrementing values. Extending this, any two-place operator can be used in this operator-assignment mode, for example '$a -= 10' (subtract 10 from the value of $a), '$b *= 5' (multiply the value of $b by 5), etc.

A very common type of expressions are comparison expressions. These expressions evaluate to either FALSE or TRUE. PHP supports > (bigger than), >= (bigger than or equal to), == (equal), != (not equal), < (smaller than) and <= (smaller than or equal to). The language also supports a set of strict equivalence operators: === (equal to and same type) and !== (not equal to or not same type). These expressions are most commonly used inside conditional execution, such as if statements.

Operators

An operator is something that you feed with one or more values which yields another value. There are three types of operators.

  • Unary operator - which operates on only one value
  • Binary operators
  • Ternary operator: ?: - used to select between two expressions depending on a third one

Arithmetic Operators

It's like basic arithmetic. You add, multiply, subtract, divide.

<?
//Example Code for Arithmetic Operators
-$a// Negation
$a $b//Addition - This adds values of $a and $b
$a $b// Subtraction - Difference of $a and $b
$a $b// Multiplication - Product of $a and $b
$a $b// Division - Quotient of $a and $b - returns a float value anytime
$a $b// Modulus - Remainder of $a divided by $b
?>

Assignment Operators

The basic assignment operator is "=" which means that the left operand gets set to the value of the expression on the right.

<?
//Example Code
$a 10//sets value of $a to 10 
$b $a//sets value of $b to the value of $a 
echo $b//you will see output as 10 
$a = ($b 20) + 10// $a is equal to 30 now, and $b has been set to 20 
?> 

Comparison Operators

One of the most important operators are Comparison Operators. They help you compare two values. Take a look at this table:

ExampleNameResult
$a == $bEqualTRUE if $a is equal to $b.
$a === $bIdenticalTRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)
$a != $bNot equalTRUE if $a is not equal to $b.
$a <> $bNot equalTRUE if $a is not equal to $b.
$a !== $bNot identicalTRUE if $a is not equal to $b, or they are not of the same type. (introduced in PHP 4)
$a < $bLess thanTRUE if $a is strictly less than $b.
$a > $bGreater thanTRUE if $a is strictly greater than $b.
$a <= $bLess than or equal to TRUE if $a is less than or equal to $b.
$a >= $bGreater than or equal to TRUE if $a is greater than or equal to $b.

Remember, when comparing an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers.



Tutorial Pages:
» What can I do with PHP?
» Popoular Features of PHP
» Basics of PHP
» Variables in PHP
» Data Types in PHP
» Expressions and Operators
» Control Structures in PHP
» Functions in PHP
» Declaring Functions in PHP
» Scope of a Variable
» Built-in Functions in PHP
» Handling Strings in PHP
» Printing Strings in PHP
» String Comparisons in PHP
» Manipulating Strings in PHP
» Arrays in PHP
» Types of Arrays in PHP
» Creating Arrays in PHP
» Array Operations in PHP


 | Bookmark Print |   Write For Us
Related Tutorials:
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK
» Installing PHP on Windows



About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: