
Manage the Exam Results online using Result Management System!

Learning More on Operators in PHP
More Operators in PHP

First Operand
|
Second Operand
|
Type of Comparison
|
Number
|
Number
|
Numeric
|
String that is entirely numeric (Numeric String)
|
String that is entirely numeric (Numeric String)
|
Numeric
|
String that is entirely numeric (Numeric String)
|
Number
|
Numeric
|
String that is not entirely numeric (Non-numeric String)
|
Number
|
Lexicographic
|
String that is entirely numeric (Numeric String)
|
String that is not entirely numeric (Non-numeric String)
|
Lexicographic
|
String that is not entirely numeric (Non-numeric String)
|
String that is not entirely numeric (Non-numeric String)
|
Lexicographic
|
Using Operators and Expressions in PHP
Forming Expressions Using Operators in PHP
![]() |
Expressions are elements that can be evaluated to produce some value during the program execution. The simplest expressions are literals and variables.
A literal evaluates to itself, while a variable
evaluates to the value stored in the variable.More complex expressions can be formed using simple expressions and
operators. An operator is a symbol used to specify the operation to
be performed on operands. Some operators modify their operands,
while most do not.
PHP borrows most of its operands from C and Perl. Most operators in PHP are binary operators; they combine two operands (or expressions) into a single, more complex expression. PHP also supports a number of unary operators and a single ternary operator that combines three expressions into a single expression.
Operator Precedence & Associativity:
The order in which operators in an expression are evaluated
depends on their relative precedence. For e.g., among arithmetic
operators, the operators * and / are in the higher precedence
(evaluated first). And the operators + and – are having lower
precedence (evaluated last).
To force a particular order, we can group operands with the
appropriate operators in parenthesis. Operators placed within
parenthesis are evaluated first (i.e., they get the highest priority
to be executed first). But, within parenthesis, they are evaluated
in the order of their precedence. For example, the expression:
(2 + 4 * 3) / 2
evaluates the expression to 7 – multiplication first, addition next
(with in parenthesis) and then divide the result by 2. This is
because of the highest priority given to the expression within
parenthesis. The remaining operators, which are placed outside the
parenthesis, are evaluated in their order of priority.
Associativity defines the order in which operators with the
same order of precedence are evaluated. For example, the expression
(2 / 2 * 2) is evaluated as:
(2 / 2) * 2 = 2
and not as: 2 / (2 * 2) = 0.5
Here, the associativity is left to right; that means, in case of ambiguity (expression having operators with same precedence), the operators are evaluated from left to right.
Implicit Casting:
Many operators in PHP require that both their operands are of same
type. PHP variables can store values of any data type, which include
integers, floating-point numbers, strings and more. The conversion
of a value from one type to another is called casting.
PHP converts values from one type to another as necessary. This kind of casting (automatic conversion) is called implicit casting or type juggling in PHP. The rules for type juggling done by arithmetic operators are shown in the below table:
Type of First Operand
|
Types of Second Operand
|
Conversion Performed
|
Integer
|
Floating-point number
|
The integer is converted to a floating-point number
|
Integer
|
String
|
The string is converted to a number; if the value after conversion
is a floating-point number, the integer is converted to a
floating-point number.
|
Floating-point number
|
String
|
The string is converted to a floating-point number
|
Table: Implicit Casting rules for Binary Arithmetic Operators
We can use a string anywhere PHP expects a number. The string is
presumed to start with an integer or floating-point number. If no
number is found at the start of the string, the numeric value of that
string is 0. If the string contains a period (.)
or upper- or lower-case e, evaluating it numerically produces
a floating-point number. For e.g.,
“9 Lives” -1; //8 (int)
“3.14 Pies” * 2; //6.28 (float)
“1E3 Points of Light” + 1 //1001 (float)
Types of Operators:
- Arithmetic operators
- String Concatenation operator
- Auto increment and Auto Decrement operator
- Comparison operators
- Bitwise operators
- Logical operators
- Casting operators
- Assignment operators
- Miscellaneous operators
The first and foremost operator of all PHP operators is arithmetic
operators. Arithmetic operators are set of operators that perform
arithmetic operations on numeric values. Most of the arithmetic
operators are binary; however, the arithmetic negation (-) and
arithmetic assertion (+) operators are unary.
Binary arithmetic operators are: +, -, *, / and % and they
perform operations like addition, subtraction, multiplication,
division and modulo division respectively. If they are applied on
non-numeric values, they are converted into numeric values
automatically.
The unary arithmetic operator arithmetic negation (-)
multiplies the value of its operand by -1 and hence changes its sign.
For e.g., -(3-4) evaluates to 1. The arithmetic assertion operator
(+) returns the operand multiplied by +1, which has no effect. It is
used as a visual cue to indicate true sign of a value.
The second type of operator in PHP is String Concatenation
operator. This operator is denoted by the symbol dot (.),
which appends the right-hand operand to the left-hand operand and
returns the resulting string. Operands are first converted to
strings, if necessary. For e.g.:
$n = 5;
$s = ‘5 +’ . $n . ‘is 10’; //$s is ‘5 + 5 is 10’
Various Types of Scope of Variables in PHP
Fundamentals of PHP Programming
PHP Programming Basics

Language Basics:
Literals and Identifiers:
- The first character of an identifier must be an ASCII letter (uppercase or lowercase), the underscore (_), or any of the characters between ASCII 0x7F and ASCII 0xFF.
- After the initial character, we can have characters from alphabets, numbers or special characters (except white space).
Constants:
Keywords:
Here is a presentation that introduces PHP to the reader.
Test Your Knowledge on PHP & MySQL
- Tell whether PHP variable and keywords are case sensitive?
- List the different data types supported in PHP.
- Write a console application in PHP for generating Prime Numbers between 1 to 50.
- How do you create arrays in PHP. Give an example.
- Write a sample code in PHP for creating and using Objects.
- Discuss in details the functions used for generating Summary Reports in MySQL. (8)
- List any four string related functions in MySQL and explain its syntax. (8)
- Create a table called "project" which contains the following data: (16)
empno
|
projectID
|
projectname
|
budget
|
---|---|---|---|
15
|
1
|
Interface
Design
|
10000
|
17
|
2
|
E-Commerce
Solution
|
20000
|
19
|
3
|
Advertising
|
50000
|
- The company has decided that the budgets are too small. Update the project table. Add 5000 to each of the budgets.
- The company decides to take project number 3 from employee 19 and give it to employee 20. Update the table to reflect this change.
- a) How to use MySQL left join to select data from multiple tables? Explain (8)
- a) What is PHP? What does it do? List out its features? (8)
b) Mention the scalar data-types provided by PHP? Explain with Examples (8)
- Write a console application in PHP to find the factorial of first 10 numbers. (16)
Developing Applications in PHP
Programming Exercises on PHP
Exercise 1:Write a console application in PHP to check whether the given number is prime or not.
Solution:
Create a file with the extension Prime.php and place the following code in it:
<?php
$n = 50;
$prime = false;
if($n%2 == 0)
{
print("The number $n is not a Prime Number");
$prime = true;
}
if($n%3 == 0)
{
print("The number $n is not a Prime Number");
$prime = false;
}
if(!$prime)
print("The number $n is a Prime Number");
?>
Execute the above program from the PHP Client as follows:
php Prime.php
Exercise 2:
Develop a console application in PHP that uses a for-loop to generate the output as shown in the following table. Find the following for first 10 integers:
- Inverse (1/I) of 10 integers
- Square (I*I) value of first 10 integers
- Factorial (I!) of first 10 integers
I
|
I*I
|
I!
|
I/1
|
1
|
1
|
1
|
1
|
2
|
4
|
2
|
0.5
|
3
|
9
|
6
|
0.33333333333333
|
4
|
16
|
24
|
0.25
|
5
|
25
|
120
|
0.2
|
6
|
36
|
720
|
0.16666666666667
|
7
|
49
|
5040
|
0.14285714285714
|
8
|
64
|
40320
|
0.125
|
9
|
81
|
362880
|
0.11111111111111
|
10
|
100
|
3628800
|
0.1
|
Solution:
Exercise 3:
Create a web page containing Tables from 1 to 9, each having entries from 1 to 9. The output should look like this:
Multiplication table
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
1 x 1 = 1 | 2 x 1 = 2 | 3 x 1 = 3 | 4 x 1 = 4 | 5 x 1 = 5 | 6 x 1 = 6 | 7 x 1 = 7 | 8 x 1 = 8 | 9 x 1 = 9 |
1 x 2 = 2 | 2 x 2 = 4 | 3 x 2 = 6 | 4 x 2 = 8 | 5 x 2 = 10 | 6 x 2 = 12 | 7 x 2 = 14 | 8 x 2 = 16 | 9 x 2 = 18 |
1 x 3 = 3 | 2 x 3 = 6 | 3 x 3 = 9 | 4 x 3 = 12 | 5 x 3 = 15 | 6 x 3 = 18 | 7 x 3 = 21 | 8 x 3 = 24 | 9 x 3 = 27 |
1 x 4 = 4 | 2 x 4 = 8 | 3 x 4 = 12 | 4 x 4 = 16 | 5 x 4 = 20 | 6 x 4 = 24 | 7 x 4 = 28 | 8 x 4 = 32 | 9 x 4 = 36 |
1 x 5 = 5 | 2 x 5 = 10 | 3 x 5 = 15 | 4 x 5 = 20 | 5 x 5 = 25 | 6 x 5 = 30 | 7 x 5 = 35 | 8 x 5 = 40 | 9 x 5 = 45 |
1 x 6 = 6 | 2 x 6 = 12 | 3 x 6 = 18 | 4 x 6 = 24 | 5 x 6 = 30 | 6 x 6 = 36 | 7 x 6 = 42 | 8 x 6 = 48 | 9 x 6 = 54 |
1 x 7 = 7 | 2 x 7 = 14 | 3 x 7 = 21 | 4 x 7 = 28 | 5 x 7 = 35 | 6 x 7 = 42 | 7 x 7 = 49 | 8 x 7 = 56 | 9 x 7 = 63 |
1 x 8 = 8 | 2 x 8 = 16 | 3 x 8 = 24 | 4 x 8 = 32 | 5 x 8 = 40 | 6 x 8 = 48 | 7 x 8 = 56 | 8 x 8 = 64 | 9 x 8 = 72 |
1 x 9 = 9 | 2 x 9 = 18 | 3 x 9 = 27 | 4 x 9 = 36 | 5 x 9 = 45 | 6 x 9 = 54 | 7 x 9 = 63 | 8 x 9 = 72 | 9 x 9 = 81 |
Do the following before running PHP from browser (from Terminal
):
- Install Apache Server using the following command
sudo apt-get install apache2 - Run the following script for setting up Password for root user:
sudo passwd root - Use the following command from the Terminal to have a UI for root login in the login screen:
sudo sh -c 'echo "greeter-show-manual-login=true" >> /etc/lightdm/lightdm.conf'
- If Step 3 doesn't work on your computer, try the following command and include the command
greeter-show-manual-login=true
in lightdm.conf file:
gksudo gedit
/etc/lightdm/lightdm.conf
<?php
echo "<h1>Multiplication table</h1>";
echo "<table border=2 width=75%";
echo "<tr>";
for ($i = 1; $i<= 9 ; $i++)
echo "<td>".$i."</td>";
echo "</tr>";
for ($i = 1; $i <= 9; $i++ ) {
//this is the outer loop
echo "<tr>";
for ( $j = 1; $j <= 9; $j++ ) { // inner loop
echo "<td> $j x $i = ".$i * $j."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Exercise 4:
Create a registration form which contains fields name,Roll No,Gender and a submit button. All the details should be displayed in the server page when the user clicks the submit button.
Solution:
<html>
<body>
<form action="" method="post">
<table align="center">
<tr>
<td>Name</td>
<td> </td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Roll Number</td>
<td> </td>
<td><input type="text" name="roll"></td>
</tr>
<tr>
<td>Gender</td>
<td> </td>
<td><select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" value="Submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
<br><br>
<?php
if(isset($_REQUEST['submit']))
{
echo "<br>Name :".$_REQUEST['name'];
echo "<br>Roll Number :".$_REQUEST['roll'];
echo "<br>Gender :".$_REQUEST['gender'];
}
?>
Slightly modify the above program so that it can store and display all the students who have registered themselves through this form.
Solution:
Replace the PHP code from the above program with the following:
<?php
$con = mysql_connect("localhost","root","admin");
mysql_select_db("testdb",$con);
echo "Students who are registered already : <br/>";
echo "<table border=2 width=75%>";
echo "<tr>";
echo "<td>Student Name</td><td>Roll Number</td><td>Gender</td>";
echo "</tr>";
$result = mysql_query("SELECT * FROM Student");
if(mysql_num_rows($result)<>0)
{
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['Roll']."</td>";
echo "<td>".$row['Gender']."</td>";
echo "</tr>";
}
}
$name = $_REQUEST['name'];
$rollno = $_REQUEST['roll'];
$gender = $_REQUEST['gender'];
if ($name <> "")
{
$result = mysql_query("SELECT * FROM Student WHERE Name = '$name' and Roll='$rollno'");
if(mysql_num_rows($result)==0)
{
$sql = "INSERT INTO Student VALUES('$name','$rollno','$gender')";
mysql_query($sql,$con);
echo "<tr>";
echo "<td>".$name."</td>";
echo "<td>".$rollno."</td>";
echo "<td>".$gender."</td>";
echo"</tr>";
}
}
echo "</table>";
mysql_close($con);
?>
This program requires a database named testdb and a table named Student, which is created using the following command in the local mysql server (localhost):
Student | CREATE TABLE `Student` (
`Name` varchar(15) DEFAULT NULL,
`Roll` varchar(10) DEFAULT NULL,
`Gender` char(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1