Web Development

PHP Operators

Using PHP Operators

This operator allowed us to assign values to variables and arrays (which we recently learned about).� The other PHP operator we discussed was the array operator

")

which allowed us to match array “keys” to array “values”.� There are also the “concatenation” operators which combine strings.� Here are examples of them all:

echo "$variable\n"; //remember the "newline" character I taught last lesson...

/* there is also a lesson to be learned here, you can't print arrays inside double quotes like you would variables.
You either have to assign the specific array element to a variable and print that or use the concatenation method
which we learned in lesson 4 */

Pay particular attention to the above comments as some of them were not discussed in previous lessons.� In this lesson I’ll go over some other common operators you might want to use.

The PHP arithmetic operators are used to do simple mathematical operations such as addition, subtraction, multiplication, and division.� Here are examples of each:

$answer = $number1 - $number2;
echo "$answer\n"; //prints 9
$answer = $number1 * $number2;
echo "$answer\n"; //prints 36
$answer = $number1 / $number2;
echo "$answer\n"; //prints 4

A few other common PHP operators you will find yourself using often are the following:

Here is a combination operator, concatenation and assignment in one.� This operator takes the original string and adds a new string onto it.

Here is a combination operator, addition and assignment in one.� This operator takes the original number and adds a new number to it.

That’s about it for this lesson.� There really isn’t much to teach about PHP operators, you use them as you need them.� And if you don’t know if what you want exists, you just check the PHP documentation.

Check out PHP’s documentation for explanations of all the operators:
http://www.php.net/manual/en/language.operators.php

About the author

Written by Darren W..

Darren Hedlund is a freelance Web developer, writer, and data analyst. Darren has a degree in Computer Information Science and has spent the last 15 years developing application and environments from hand held, windows, web, virtual science, gaming, artificial intelligence and graphics design.

Darren's coding knowledge ranges from C+, Visual Basic, .NET, PHP, JSP, REXX, KIXX, and many others. His graphical and environmental knowledge stems in Macromedia Flash, 3D studio Max, Curious Labs Poser, Adobe Photoshop, and many others. Darren works in many platforms ranging from database, visual design, and, system development.

If you found this post useful you may also want to check these out:

  1. Perl Range Operator
  2. Control Structures and While Loops
  3. Using Variable Referencing
  4. The PHP Explode Function, Split a String by String
  5. Perl Sorting
  6. Displaying Current Date with JavaScript and Basic Math Calculations