Unit-I PHP Notes PDF
Unit-I PHP Notes PDF
Variable References:
====================
$a=100;
$b=&$a;
Variable Scope:
====================
1) Global Scope
2) Local Scope
1) Integer:
3) String:
4) Boolean:
5) Arrays:
6) Objects:
7) Resource:
8) Null:
- This data type is having only one value denoted by the keyword Null.
- The numm value represent a variable that has no value.
- A Variable considered to be null if:
I) It has been assigned constant NULL
II) It has not been set to any value.
III) It has been unset();
- is_null() function used to test whether value is null or not.
- Example:
<?php
$result=null;
if(is_null($result))
{
echo "This is null data type value";
}
else
{
echo "This is not null data type value";
}
?>
===================
Operators:
===================
- Operators are the symbol which indicate operation to be perform.
- There are three main categories of the operators
I) Unary Operators: require single operand.
II) Binary Operators: require two operands.
III) Ternary Operators: require three operands.
1) Arithmetic Operators:
- Example:
<?php
//Arithmetic Operators
$no1=100;
$no2=50;
$result=$no1+$no2;
echo "Addition is $result";
$result=$no1-$no2;
echo "<br>Substraction is $result";
$result=$no1*$no2;
echo "<br>Multiplication is $result";
$result=$no1/$no2;
echo "<br>Division is $result";
$result=$no1%$no2;
echo "<br>Modulus is $result";
$result=$no1**$no2;
echo "<br>Exponentiation is $result";
$result=-$no1;
echo "<br> Negation is $result";
?>
<?php
$a=100;
echo "Value of a before incrementation=$a";
$a++;
echo "<br>Value of a after incrementation=$a";
?>
<?php
$a=100;
echo "Value of a before decrementation=$a";
$a--;
?> echo "<br>Value of a after decrementation=$a";
4) Assignment Operator:
5) Comparison Operator:
6) Logical Operator:
7) Bitwise Operator:
<?php
$a=12;
$b=13;
$c=$a and $b;
echo "Bitwise AND = $c";
$c=$a | $b;
echo "<br>Bitwise OR = $c";
$c=$a ^ $b;
echo "<br>Bitwise XOR = $c";
$c=~12;
echo "<br>Bitwise NOT = $c";
$c=14<<2;
echo "<br>Bitwise left Shift = $c";
$c=14>>2;
echo "<br>Bitwise right Shift = $c";
?>
Condition?Expression-1 : Expression-2
Constants:
===========
- Constant is a variable which can not change value during the execution
of program.
- Syntax: define("ConstantVariableName",value);
- Example: define("PI",3.14);
<?php
define('PI',3.14);
$radius=2;
$area=(PI*$radius*$radius);
2) if-else statement:
3) Nested-if statement:
***Switch Statement***
========================
- switch, case ,default and break are predefined keywords.
- In switch case, we compare value to multiple cases.
- Syntax:
switch(Expression)
{
case value-1 : //block of code
break;
case value-2 : //block of code
break;
case value-3 : //block of code
break;
-
-
case value-N : //block of code
break;
default: //block of code
}
- Example:
<?php
$day="xyz";
switch($day)
{
case "Mon": echo "It is Monday";
break;
case "Tues": echo "It is Tuesday";
break;
case "Wed": echo "It is Wednesday";
break;
case "Thurs": echo "It is Thursday";
break;
case "Fri": echo "It is Friday";
break;
case "Sat": echo "It is Saturday";
break;
case "Sun": echo "It is Sunday";
break;
default: echo "Invalid Day!!!";
}
?>
======================
Loop Control Structures
======================
1) for loop
2) while loop
3) do-while loop
4) foreach loop
***for loop:
- syntax:
for(initialization;condition;increment/decrement)
{
//body of for loop
}
- Example:
<?php
for($x=1; $x<=5 ;$x++)
{
echo "<br>Value of x : $x";
}
?>
***while loop:
- syntax:
while(condition)
{
//body of while loop
}
- Example:
<?php
$x=1;
while($x<=5)
{
echo "<br>Value of x : $x";
$x++;
}
?>
***Do-while loop:
- syntax:
do
{
//body of while loop
}while(condition);
- Example:
<?php
$x=1;
do
{
echo "<br>Value of x : $x";
$x++;
}while($x<=5);
?>
***foreach loop:
- foreach loop provides an easy way to iterate over an array.
- syntax:
foreach(Array_Expression as $value)
{
//body of foreach loop
}
- Example:
<?php
$Num=array(10,20,30,40,50);
echo " Array Elements are:";
foreach($Num as $x)
{
echo "$x ";
}
?>
================
break statement
================
- when break statement is executed then program controller comes out of
loop.
- Example:
<?php for($i=1;$i<=5;$i+
+)
{
if($i==3)
{
break;
}
echo "<br>Value of i = $i";
}
?>
================
continue statement
================
- when continue statement is executed then program controller goes to
next iteration.
- Example:
<?php for($i=1;$i<=5;$i+
+)
{
if($i==3)
{
continue;
}
echo "<br>Value of i = $i";
}
?>
++++++++++++++++
Practice Program
++++++++++++++++
- Write a PHP program which acceptn five subjects marks from user and
display total and
percentage of student.
- Write a PHP program to find area of Rectangle.
- Generates following output in PHP.
*
* *
* * *
* * * *
* * * * *
- Generates following output in PHP.
*
* *
* * *
* * * *
* * * * *
- Write a PHP program to check whether no is prime or not.
<?php
$no=readline("Enter Any Integer Number:");
$flag=0; for($i=2;$i<$no;
$i++)
{
if($no%$i==0)
{
$flag=1;
break;
}
}
if($flag==0)
{
echo "Number is prime";
}
else
{
echo "Number is not prime";
}
?>