There are many types of operators in PHP. These include arithmetic operators, assignment operators, incrementing operators, decrementing operators, comparison operators, logical operators, and array operators. We will be manipulating the distance formula as our example.
Most of these are quite simple. They are especially useful in if statements but are also helpful in calculations. Just a refresher, $x means variable x.
Lets try this out for ourselves. Open up a new page in textwrangler and save it as "phparithmeticoperators.php" in your public_html folder. Then type the following starter code into your document.
Using your new arithmetic operators knowledge, create the distance formula in php and type it next to the line that says '$d='.
Use ( a , A ) as your initial point and ( b , B )as your final point.
The distance formula is the square root of (a-b)^2 + (A-B)^2. Change this to a formula acceptable in php using the variables provided.
Remember that in php, 'sqrt' is used find the square root of an expression.
To square a value, use either pow(base,2) or simply use the multiplication operator to multiply the expression by itself. In this case, since we are trying to learn arithmetic operators, use the multiplication operator.
After running the page, the answer ('15.811388300842') should be displayed on your screen.
If you are getting a different answer or an error message, make sure you have a semicolon after each line. Then compare to my code.
Feel free to mess around with the numbers!
We use assignment operators when we want to set one thing as something else, or give a variable a value. This is similar to how in math we say "x=5" to set the value of x at 5. These are also fairly straightforward.
We use incrementing and decrementing operators to continuously add or subtract 1 from a variable. They are most commonly used in for loops.
Open up phparithmeticoperators.php and alter the code as shown below.
Now delete the '????' and replace it with any incrementing or decrementing operator you want. Then run the page. You can also mess around with the other points and see how the distance changes as the for loop adds or subtracts 1 from a each time.
If you are having trouble, here is what the final code could look like.
These are used to compare 2 values to each other. We used these in the for loop from the last example. Comparison operators are frequently used in for loops.
These are the ands, ors, and xors. They are most commonly used in if statements.
Open up phparithmeticoperators.php and see if you can figure out a way to display $d only if it is between 16 and 30. Use a logical operator to do this. HINT: You need an if statement, which is in the format "if (condition) { }"
Having trouble? Take a look at my code.
Use in arrays
You are now finished with the tutorial! Bonus points if you noticed something special on each of the images of code. Watermark, anyone? #obnoxious
Here is the finished product. Look at all the operators we used. These operators sure are operational! /p>