Unit - 2 PHP Basics: Learning Objectives
Unit - 2 PHP Basics: Learning Objectives
Php Basics
Learning objectives
1) Advantages of PHP
2) Where to use PHP
3) How to install PHP
4) How to run the first PHP
5) Syntax used in PHP
6) Variables and constants used in PHP
7) Echo and print
8) Data types used in PHP
9) Operators in PHP
Structure
2.1 Introduction
2.2 Definitions
2.8 References
Step-1
The comments in PHP are of two types and they are single line comments and
multiline comments. The single line comments are started with // and multiline
comments are defined with /* and */.
<!DOCTYPE HTML>
<HTML>
<body>
<?PHP
$color = "red";
echo "My house is " . $color . "<br>";
echo "My shirt is " . $COLOR . "<br>";
echo "My coat is " . $coLOR . "<br>";
?>
</body>
</HTML>
Output
Output :-
Output
<?PHP
$x = 5;
$y = 10;
function Test() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
Test();
echo $y; // outputs 15
?>
function Test() {
static $x = 0;
echo $x.'<br>';
$x++;
Test();Test();Test(); ?>
Output
0
Output
Echo Print
Echo has no return value Print has a retune value and that is 1
Echo can take multiple parameters Print takes only one parameter
Echo is faster than Print Print slower than echo
<!DOCTYPE HTML>
<HTML><body>
<?PHP
echo 'foo', 'bar'; // Concatenates the 2 strings
print('foo', 'bar'); // Fatal error
$res = Print("hello"); //Prints Hello
echo $res //Prints 1
?></body></HTML>
A._____________________________________________________________
A.____________________________________________________________
A.____________________________________________________________
1. String
2. Integer
3. Float
4. Boolean
5. Array
6. Object
7. NULL
8. Resource
<?PHP
$my_name ="Sandilya";
$my_full_name='V.S.Sandilya';
Output
<?PHP
Output
<!DOCTYPE HTML>
<HTML>
<body>
<?PHP
$x = 15.365;
var_dump($x);
?> </body></HTML>
Output
<!DOCTYPE HTML>
<HTML>
<body>
<?PHP
$x = true;
$y = false;
echo($x);
Output
1
Output
Description
In the above example we have defined $nn as an array which means that this array
can contain any number of values basing on the requirement. We have defined
$nn[0]=Rohan which means that 0 is the array index and Rohan is stored in that
index.
Using the for loop we moved from the 0th index to the 2nd index and displayed the
values stored at the various indexes.
2.5.5..1.2 Indexing arrays manually
<?PHP
$nn = array();
$nn[0]= 'Shyam';
$nn[1]= 'riyaz';
$nn[2]= 'Naina';
for($i=0;$i<3;$i++)
{
echo($nn[$i]);
echo('<br>');
}
?>
Output
Output
Description
In the above example we got the salary drawn by Reshma by
$salary[Reshma];
2.5.5.2 .1 Use of Associative arrays in loops
<?PHP
$salary = array();
$salary['Rohan']=350000;
$salary['Reshma']=40000;
$salary['Alok']=20000;
foreach($salary as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
Output
Description
In the above example we have defined the $salary as an array. Next we have
defined the salary of each key which are Rohan, Reshma and Alok. We loop
through the salary array and get the key and value using the command as shown
below.
Foreach ($salary as $x => $x_value) {
ROWS
0 1 2
COLUMN
<?PHP
function addition()
return $this->result;
$add = $c -> addition(); // call of a function addition of the class calculation and
assign to variable
//add
?>
Output
The sum of two variables is : 30
Description
In the above example we have defined a class named calculation. This class is
comprised of 3 variables $var1,$var2 and $result.
We defined a function named addition for adding two numbers. In this function
we added two variables and assigned the result to addition to result. For this we
used the expression
Here $this stands for this class which is calculation. Since the variables are
defined in the class so $this->var1 means the current class calculation variable
$var1. In the function we used a word return which is used to return a single
value as functions take no values or multiple values but return a single value.
class a
var $aa;
$x=null;
$a1 = new a;
var_dump($a1->aa);
echo("<br>");
var_dump($x);
echo("<br>");
echo($y);
echo("<br>");
$y=null;
var_dump($y);
?>
Output
NULL
NULL
hi there
NULL
Description
In the above example we have defined a class a and inside it we defined a variable
aa but did not assign any value to it .If no value is assigned to it it is set to null.
Below the class we have defined a variable name $x and set to null. Hence the
value of $x is null. We defined an object a1 of class a. var_dump shows the type
of variable. At the end $y =null empties the variable $y.
$conn = mysqli_connect(localhost,"root","admin","animals");
Here $conn is the variable
A._____________________________________________________________
A.____________________________________________________________
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Increment/decrement operators
5. Logical operators
6. String operators
7. Array operators
2.6.1.1 Addition
<?PHP
$x = 20;
$y = 5;
echo $x + $y;
?>
Output
25
Description
In the above example $x is set to 20 and $y is set to 5. Echo shows the output of
sum of two numbers $x and $y which is 25.
2.6.1.2 Subtraction
<?PHP
$x = 20;
$y = 5;
echo $x - $y;
?>
Output
15
Description
In the above example $x is set to 20 and $y is set to 5. Echo shows the output of
subtraction of two numbers $x between $y which is 15.
Output
100
Description
In the above example $x is set to 20 and $y is set to 5. Echo shows the output of
multiplication of two numbers $x between $y which is 100.
2.6.1.4 Division
<?PHP
$x = 20;
$y = 5;
echo $x / $y; ?>
Output
4
Description
In the above example $x is set to 20 and $y is set to 5. Echo shows the output of
division of $x between $y which is 4
2.6.1.5 Modulus
<?PHP
$x = 20;
$y = 5;
echo $x %$y;
?>
Output
0
Description
In the above example $x is set to 20 and $y is set to 5. Echo shows the reminder
of division of $x between $y which is 0
2.6.1.6 Exponentiation
<?PHP
$x = 2;
$y = 5;
echo pow($x,$y)
?>
Output
32
Description
In the above example $x is set to 2 and $y is set to 5. Echo shows the
exponentiation of $x to $y which is 32
A._____________________________________________________________
The assignment operators used with numeric values are as shown below
<?PHP
$x=20;
$y=5;
$x = $y;
echo $x;
?>
As if you do not defined anything the browser searches for index.PHP in the
folder PHP_test
Output
Now to view the result we type
Or
<?PHP
$x=20;
$y=5;
$x += $y;
echo $x; ?>
Output
Now to view the result we type
2.6.2.3 Subtraction
This option is used to subtract a variable from the other.
Example :- Let us create a PHP file index.PHP . Now writing the code in
index.PHP as shown below
<?PHP
$x=20;
$y=5;
$x -= $y;
echo $x;
?>
Output
<?PHP
$x=20;
$y=5;
$x *= $y;
echo $x;
?>
Output
2.6.2.5 Division
This option is used to divide one variable by the other
<?PHP
$x=20;
$y=5;
Output
<?PHP
$x=20;
$y=5;
$x %= $y;
echo $x;
?>
Output
A._____________________________________________________________
Use of ==
Two variables are said to be equal if the values of both variables are equal
irrespective of their type.
<?PHP
$x = 10;
$y = "10";
$z = "20";
echo("<br>");
?>
Output
Use of ===
Two variables are said to be identical if the values of both variables are equal and
the types of both variable are also equal.
<?PHP
$x = 10;
$y = 10;
$z = "10";
echo("<br>");
var_dump($x === $z); // returns false because values are equal but types unequal
?>
Output
Use of !=
This option is used to check if both values of variables are not equal in PHP . It
returns true if both values are not equal.
<?PHP
$x = 10;
$y = 10;
$z = "20";
echo("<br>");
?>
Output
Output
Use of !==
This operator returns true if two variables are not identical.
<?PHP
$x = 10;
$y = 10;
$z = "10";
var_dump($x !== $y); // returns false because the two variables are identical
echo("<br>");
var_dump($x !== $z); // returns true because the two variables are not identical
?>
Output
Use of >
This comparative operator is used to find out if one variable is greater than the
other variable.
<?PHP
$x = 10;
$y = 10;
$z = 20 ;
var_dump($x > $y); // returns false because the two variable values are the same
echo("<br>");
var_dump($z > $x); // returns true because the two variables $x value is less than
the value of $z
?>
Output
Use of <
This operator is used to compare two variable and returns true if a variable is less
than the other variable. If two variables are the equal then this comparison returns
false.
$x = 10;
$y = 10;
$z = 20 ;
var_dump($x < $y); // returns false because the two variable values are the same
echo("<br>");
var_dump($x < $z); // returns true because the two variables $x value is less than
the value of $z
?>
Output
Use of >=
This operator returns true if a variable is greater than or equal to another variable.
<?PHP
$x = 10;
$y = 10;
$z = 20 ;
var_dump($x >= $y); // returns true because value of $x variable is the same as
$y.
echo("<br>");
var_dump($x >= $z); // returns false because value of $x variable is less than $z
?>
<?PHP
$x = 10;
$y = 10;
$z = 20 ;
var_dump($x <= $y); // returns true because value of $x variable is the same as
$y.
echo("<br>");
var_dump($z <= $x); // returns false because value of $x variable is less than $z
?>
Output
A._____________________________________________________________
<?PHP
$x = 20;
echo($x);
echo("<br>");
++$x;
echo($x);
echo("<br>");
?>
Output
<?PHP
$x = 20;
echo($x);
echo("<br>");
--$x;
Output
$a = 100;
$b = 200;
else
} ?>
Output
<?PHP
$a = 100;
$b = 200;
else
?>
Output
$a = 100;
$b = 200;
else
?>
Output
$a = 100;
$b = 200;
else
?>
Output
<?PHP
$a = 100;
$b = 200;
else
?>
<?PHP
$a = 100;
else
?>
Output
A._____________________________________________________________
A._____________________________________________________________
. Concatenation statement
.= Concatenation assignment
<?PHP
$a = "Hello";
$b = "World";
$c = $a.$b;
echo $a."<br>";
echo $b."<br>";
echo $c."<br>";
$c.= $d;
echo $c."<br>";
?>
Output
<?PHP
$motorcycle ={Honda,Yamaha,Suzuki,Pulsar};
?>
Description
In the above example we have seen 2 arrays .The first one is $motorcycle and the
second one is $cars
$motorcycle[0] is Honda
$motorcycle[1] is Yamaha
$motorcycle[2] is Suzuki
$motorcycle[3] is Pulsar.
$cars[0] is Maruti
$cars[1] is Mercedes
$cars[2] is BMW
$cars[3] is Venom
$cars[4] is Audi
<?PHP
$Cars = array
('e'=>'Maruti','f'=>'Mercedes','g'=>'BMW','h'=>'Venom','i'=>'Audi');
Output
Description
In the above example we have defined two arrays namely $motorcycle and
$cars. $motorcycle array is comprised of array indexes namely a,b,c,d. In
index a we stored the value Honda,index b we stored Yamaha,c we stored
Suzuki,d we stored Pulsar. $Cars is comprised of the 5 indexes namely e,
f,g,h,i. In index e we have stored the value Maruti,index f we stored the
value Mercedes, index g is comprised of BMW, index h is comprised of
venom, index i is comprised of Audi. $c= ($motorcycle + $Cars) is the union
of 2 arrays.
<?PHP
Output
<?PHP
//$Cars = array
('e'=>'Maruti','f'=>'Mercedes','g'=>'BMW','h'=>'Venom','i'=>'Audi');
?>
Output
Use of !=
<?PHP
$Cars = array
('e'=>'Maruti','f'=>'Mercedes','g'=>'BMW','h'=>'Venom','i'=>'Audi');
Output
Use of <>
<?PHP
$Cars = array
('e'=>'Maruti','f'=>'Mercedes','g'=>'BMW','h'=>'Venom','i'=>'Audi');
var_dump($motorcycle<> $Cars);
?>
Output
<?PHP
$motorcycle = array
('a'=>'Honda','b'=>'Yamaha','c'=>'Suzuki','d'=>'Pulsar');
$motorcycle1 = array ('a'=>'Honda','b'=>'Yamaha','c'=>'Pulsar','d'=>
'Suzuki');
var_dump($motorcycle !== $motorcycle1);
?>
A._____________________________________________________________
A._____ ________________________________________________________
Echo Print
Echo has no return value Print has a return value and that is 1
Echo takes multiple parameter Print takes one parameter
Echo is faster than print Print is slower than echo
A.1 The various types of data are string, integer, float, boolean, array, object, null,
resource.
A.2. Arrays are of various types and these are indexed arrays, associate arrays,
multidimensional arrays
A.1 Increment operator is the operator that increments the variable by 1 .The
increment operator is identified as ++ and decrement operator is the operator that
decrements the variable by 1 and is identified by --.
A.2 The operators that are used to combine conditional statements are logical
operators. These are and ,or,not.
A.1 The operators that work on strings are termed as string operators. The string
operators are classified into 2 types. Concatenation or , and concatenation
assignment or .=
A.2 Array operators are the operators that work on arrays. An array is a sequence
of values stored in a variable.