Chapter04 PHP Operators
Chapter04 PHP Operators
operators
Objectives:
1. To introduce and use the PHP operators.
2. The categories of PHP operators are assignment, arithmetic, logical and
comparison operators.
3. Assigning variables.
4. Operations on numbers using arithmetic operators.
5. Comparing values.
6. Combining statements using Boolean logical operators.
Assignment Operators.
Simple assignments
Var=expr Assign the value of $nom=3; (value 3 is assign the variable
expression into the variable. nom)
$a=50;
$b=$a; ($b receives the value of $a,
which is 30)
$hasil=($nom*3)+10; (value
produced on the right expression will be
assigned to variable hasil)
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 4:1
Chapter 4: PHP Operators
Arithmetic Operators.
- Subtraction pi - y $pi - $y
() First
*, /, % Second
+, - Last
average = a + b + c + d average = (a + b + c + d) / 4;
4
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 4:2
Chapter 4: PHP Operators
You’re to develop a simple web application to receive two numbers from the user. The
numbers is entered through a HTML form, sent to the server, and another file will receive
the number, add both of them and display the result of the operation.
Step 1. Create a HTML page with a form to receive two numbers, and a submit
button. Name it formadd2numbers.php.
<html>
<head>
<title>Add two numbers</title>
</head>
<body>
Enter two numbers<br>
<form method="GET" name="formNew" action="add2numbers.php">
Number 1 <input type="text" name="num1"><br>
Number 2 <input type="text" name="num2"><br>
<input type="submit" name="btnAdd" value="Add numbers">
</form>
</body>
</html>
Step 2. View the page in the browser. Key in any number. Don’t click the button
yet. Go to Step 3, finish the Step 3 and then you can hit the button.
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 4:3
Chapter 4: PHP Operators
Step 3. Create a new page, write the code below, and save as
add2numbers.php .
<html>
<head>
<title>Add 2 numbers </title>
</head>
<body>
<?php
$n1=$_GET["num1"]; //retrieve the first number
$n2=$_GET["num2"]; //retrieve the second number
$hasil=$n1+$n2; // here is the operation
echo " $n1 + $n2 = $hasil"; //display all the values
?>
</body>
</html>
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 4:4
Chapter 4: PHP Operators
Convert a currency value from RM into USD. Given RM1 is equivalent to USD3.40.
Step 1. Create a HTML page with a form to receive a value in Malaysian ringgit,
and a submit button. Name it formRMUSDConverter.php. Use the code
below.
<html>
<head>
<title>RM to USD converter</title>
</head>
<body>
RM to USD converter<br>
<form name="formconvert" action="rmusdconverter.php" method="get">
Value of RM <input name="txtrm" type="text">
<input name="btnconvert" type="submit" value="Convert to USD">
</form>
</body>
</html>
Step 2. Next, create a HTML page to receive the value in Malaysian ringgit, and
cenvert the value into USD. Save and name it as rmusdconverter.php.
Use the code below.
<html>
<head>
<title>USD to RM converter</title>
</head>
<body>
Conversion result<br>
<?php
$rmvalue=$_GET["txtrm"];
$usdvalue=$rmvalue/3.4;
printf("RM %.2f",$rmvalue);
echo " is equivalent to ";
printf("USD %.2f",$usdvalue);
?>
</body>
</html>
Step 3. Preview the page (in the browser) with the name
formRMUSDConverter.php, key in a value in the text box and click the
button “Convert to USD”. The browser should go to the next page named
rmusdconverter.php (as defined in the form’s action) and display the value
converted from RM into USD.
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 4:5
Chapter 4: PHP Operators
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 4:6
Chapter 4: PHP Operators
Example 3: Simple statistical web application for calculating sum and average of five
numbers.
Step 1. Create the form to receive five numbers from the user. Use the code
below, and save as form5numbers.php.
<html>
<head>
<title>operation for 5 numbers</title>
</head>
<body>
Enter two numbers<br>
<form method="GET" name="form5Numbers" action="ops5numbers.php">
Number 1 <input type="text" name="num1"><br>
Number 2 <input type="text" name="num2"><br>
Number 3 <input type="text" name="num3"><br>
Number 4 <input type="text" name="num4"><br>
Number 5 <input type="text" name="num5"><br>
<input type="submit" name="btnOps" value="Ops 5">
</form>
</body>
</html>
Step 2. Create another page to receive the numbers entered by the user. Save as
ops5numbers.php.
<html>
<head>
<title>Operation on 5 numbers </title>
</head>
<body>
<?php
//retrieve the numbers
$n1=$_GET["num1"];
$n2=$_GET["num2"];
$n3=$_GET["num3"];
$n4=$_GET["num4"];
$n5=$_GET["num5"];
$sum=$n1+$n2+$n3+$n4+$n5;
$average=$sum/5;
echo "The numbers are $n1, $n2, $n3, $n4, $n5<br>";
echo "The sum of the numbers: $sum<br>";
echo "The average the numbers: $average<br>";
?>
</body>
</html>
Step 3. View the form (form5numbers.php) in the browser and key-in a number in
each of the boxes, and click the button.
Step 4. The sum and the average of the five numbers are displayed.
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 4:7
Chapter 4: PHP Operators
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 4:8
Chapter 4: PHP Operators
(*For all the problems in question 3-10, you are required to create a HTML form for the
input page, and another page to display the result of the calculation).
5. Convert a currency value from US dollar (USD) into Malaysian ringgit (RM).
8. You are given the following page with a form to receive the price of item and the
quantity for the user to buy the item.
9. You are to develop a loan payment calculator. The first page is the form for the
user to key in the sum of loan, the interest rate, and the number of years to settle
the payment. After the user entered all the information, the user will click a button
and the information will be submitted to the server. In the server there is another
file waiting to calculate and display the monthly installment to be paid, and the
total sum of payment to be made.
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 4:9
Chapter 4: PHP Operators
Comparison Operators.
Logical Operators.
The logical operators are used for Boolean expression or logical comparison. The
Boolean expression produces true if the statement is true and false if the statement is
false.
Examples of expression:
Statement convert to PHP expression
z is multiplication of 5. ($z%5 == 0)
(z adalah gandaan 5)
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 4:10
Chapter 4: PHP Operators
Other Operators
() Bracket
// Line comment
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 4:11
Chapter 4: PHP Operators
2. Create the Boolean expression (in PHP format) for each of the following
statements.
a. $X is positive integer. j. $var2 is devisable by 5.
b. $Z is negative integer. k. $gaji is greater than 5000.
c. $W is between –100 to 100. l. $huruf1 is vocal.
d. $Y is multiplication of 10. m. $huruf2 is consonant.
e. $A is multiplication of 3. n. $huruf3 is upper case.
f. $num1 is not equal to 10. o. $huruf4 is lower case.
g. $num2 is even number. p. $huruf5 is equal to ‘m’.
h. $num3 is odd number. q. $huruf6 is not equal to ‘Z’.
i. $num4 is a prime number.
PHP Manual (All rights reserved (2008) Khirulnizam Abd Rahman - http://kerul.blogspot.com/) Chapter 4:12