0% found this document useful (0 votes)
24 views14 pages

Unit-I PHP Notes PDF

The document provides an overview of PHP programming concepts, including variable references, data types, operators, and decision-making control statements. It details eight primitive data types in PHP, such as integers, floating-point numbers, strings, and arrays, along with examples of their usage. Additionally, it explains various operators, including arithmetic, comparison, and logical operators, as well as control structures like if statements and switch statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views14 pages

Unit-I PHP Notes PDF

The document provides an overview of PHP programming concepts, including variable references, data types, operators, and decision-making control statements. It details eight primitive data types in PHP, such as integers, floating-point numbers, strings, and arrays, along with examples of their usage. Additionally, it explains various operators, including arithmetic, comparison, and logical operators, as well as control structures like if statements and switch statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

**********Unit 01: *********************************

Variable References:
====================
$a=100;
$b=&$a;

Variable Scope:
====================

1) Global Scope
2) Local Scope

User defined function declaration syntax:

function functionName(Parameter list)


{
//body
}
==========================
Data Types
==========================
- PHP Supports eight primitive data types.

1) Integer:

- It is a collection of 0 to 9 digits combination value without


decimal points.
- Its range is similiar to long int data type in C.
- Its range is -2147483648 to 2147483647
- You can represent Integer number in decimal,octal and
hexadecimal format.
- If it is decimal, it is just the number. For example: 145
- If it is Octal,then number should preceeded with zero(0). For
example: 0123
- If it is hexadecimal, then number should preceeded with 0X.
For example: 0X23
- In PHP, we can test whether value is int or not by using is_int()
or is_integer()
Example:
<?php
//Integer data Type
$num=0x34;
if(is_int($num))
{
echo "Number is integer";
}
else
{
echo "Number is not integer";
}
?>

2) Floating Point Number:

- Floating point number is called as real number.


- It is a collection of 0 to 9 digits combination value with decimal
points.
- In PHP, floating point number will display value upto 15 digits decimal
places.
- Its range is 1.7E-308 to 1.7E+308
- You can represent floating point number using two different format.
I) Fractional Format : 23.45, 67.8, 143.78
II) Exponential Format: 1.3E-56, 2.3E89
- In PHP, we can test whether value is float or not by using is_float()
or is_real()
- Example:
<?php
//float data Type
$num=3.14;
if(is_float($num))
{
echo "Number is float";
}
else
{
echo "Number is not float";
}
?>

3) String:

- Collection of characters is known as String.


- We can represent string using single or double quotes.
- For example: "VJTech" or 'VJTech' both are same.
- PHP provided method is_string() to check whether given value is string
or not.
- Exmple:
<?PHP
$name="VJTech";
//echo "My class name is $name";
if(is_string($name))
{
echo "$name is String";
}
else
{
echo "$name is not String";
}
?>

4) Boolean:

- Boolean value can be either TRUE or FALSE value.


- Both values are case-insensitive.
- is_bool() method used to check whether value is boolean or not.
- Example:
<?PHP
$m=true;
if(is_bool($m))
{
echo "$m is Boolean";
}
else
{
echo "$m is not Boolean";
}
?>
- In PHP, the following values are false:
=> The keyword false,0,0.0,empty string(""),NULL

5) Arrays:

- Array is used to store multiple values in single variable.


- In PHP,Collection of different types of values is known as Array.
- Array index should begin with 0 and end with SIZE-1.
- We can retrive the individual array elements using subscript and index
number.
- Example:
<?PHP
$p=array('a','b','c','d');
$q=array('first'=>'10','second'=>'20','third'=>'30');

echo "Value of p array 0 index = $p[0]";


echo "<br>Value of p array 1 index = $p[1]";
echo "<br>Value of p array 2 index = $p[2]";

echo "<br>Value of q array = ".$q['first'];


echo "<br>Value of q array = ".$q['second'];
echo "<br>Value of q array =".$q['third'];
?>

6) Objects:

- PHP supports OOP.


- Objects are the special instance of the class.
- Class is a collection of properties and methods.
- We can define the class using keyword 'class'.
- Once class is created then we can create no of objects.
- We can access members of the class using -> symbol.
- Example:
<?php
class Student
{
public $name=' ';
function get_stud_info($nm)
{
$this->name=$nm;
}
function disp_stud_info()
{
echo "Hello $this->name";
}
}
$s1=new Student();
$s1->get_stud_info("Dennis");
$s1->disp_stud_info();
?>

7) Resource:

- Resource is a special PHP data type.


- It is used to refer to external resources like file,images, etc.
- It is used to interact with outside the world.
- For example: Database connection function return resource which is used
to identify
that connection. That resource we use while calling query.
- One more benefits of resource is that they are garbage collected when
no longer in use.
- is_resource() function to test whether value is resource or not.
- Example:
<?php
$result=database_connect();
database_query($result);
if(is_resource($result))
{
echo "This is resource data type";
}
else
{
echo "This is not resource data type";
}
?>

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:

I) Negation (-) : -$a;


II) Addition(+) : $a+$b
III) Substraction(-) : $a-$b
IV) Multiplication(*) : $a*$b
V) Division(/) : $a/$b
VI) Modulus(%) : $a%$b
VII) Exponentiation(**) : $a**$b

- 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";
?>

2) AutoIncrement(++) & AutoDecrement(--) Operators:

- Increment operator increase value of variable by one.


- Decrement operator decrease value of variable by one.
Example:
$a=100; $a=100; $b=100; $b=100;
$a++; ++$a; $b--; --$b;
$a=101; $a=101; $b=99; $b=99;

<?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";

3) String concatenation Operators:

- We use dot(.) operator for concatenation.


- Example:
<?php
$str1="Welcome";
$str2=$str1." Friends";
echo "Concatenated String is $str2";
?>

4) Assignment Operator:

- The symbol of assingment operator is (=).


- It is used to assign right side expression result or value to the left
side variable.
- We use assignment operators in different ways(= ,+=,-=,*=,/=,etc)
- Example:
<?php
$a=100;
$b=200;
$a+=$b;
echo "Addition = $a";
?>

5) Comparison Operator:

- PHP provides comparison operators to compare two operands.


- It return boolean value, either true or false.
I) Equal(==) : $a==$b
II) Identical(===) : $a===$b
III) Not Equal(!=,<>) : $a!=$b
IV) Not Identical(!==) : $a!==$b
V) Less than(<) : $a<$b
VI) Greater than(>) : $a>$b
VII) Less than or equal to(<=) : $a<=$b
VIII)Greater than or equal to(>=): $a>=$b
-Example:
<?php
$a=100;
$b=200;
$c=$a<$b;
echo "$a<$b = $c";
$c=$a>$b;
echo "<br>$a>$b =$c";
$c=$a<=$b;
echo "<br>$a<=$b =$c";
$c=$a>=$b;
echo "<br>$a>=$b =$c";
$c=$a==$b;
echo "<br>$a==$b =$c";
$c=$a!=$b;
echo "<br>$a!=$b =$c";
$c=$a!==$b;
echo "<br>$a!==$b =$c";
$c=$a===$b;
echo "<br>$a===$b =$c";
?>

6) Logical Operator:

I) Logical AND(&&,and) : True if both sides are true otherwise false


II) Logical OR(||,or) : False if both sides are false otherwise true
III) Logical XOR(xor) : If both sides are identical then it is false
otherwise TRUE
IV) Logical NOT (!) : Reverse the result
-Example:
<?php
$a=100;
$b=200;
$c= ($a<$b and $a<=$b);
echo "($a<$b and $a<=$b) : $c";

$c= ($a<$b or $a>=$b);


echo "<br>($a<$b or $a<=$b) : $c";

$c= ($a<$b xor $a<=$b);


echo "<br>($a<$b xor $a<=$b) : $c";
$c= !($a<$b and $a<=$b);
echo "<br>!($a<$b and $a<=$b) : $c";
?>

7) Bitwise Operator:

I) Bitwise AND (and) : If both bits are 1 then output would be 1


otherwise 0
II) Bitwise OR(|) : If both bits are 0 then output would be 0
otherwise 1
III) Bitwise XOR (^) : If both bits are same then output would be 0
otherwise 1
IV) Bitwise NOT(~) : Change 1 to 0 or 0 to 1
V) Bitwise Left Shift(<<)
VI) Bitwise Right Shift(>>)
-Example

<?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";
?>

8) Conditional Operator(?:)/Ternary Operator:

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);

echo "Area of Circle is $area";


?>
====================================
Decision Making Control Statements:
====================================
1) if statement:
- Syntax:
if(condition)
{
//body of if
}
- Example-1:
<?php
$str1="VJTech";
$str2="VJTech";
if($str1==$str2)
{
echo "Both strings are equal";
}
echo "<br>End of the program!!!";
?>
-Example-2:
<?php
$age=readline("Enter your Age:");
if($age>=18)
{
echo "You are eligible for Voting!!!";
}
echo "<br>End of the program!!!";
?>
-Exmple-3:
<?php
$a=10000;
$b=2000;
$c=300;

if($a>$b && $a>$c)


{
echo "Greatest Number is $a";
}
if($b>$a && $b>$c)
{
echo "Greatest Number is $b";
}
if($c>$a && $c>$b)
{
echo "Greatest Number is $c";
}
?>
-Example-4:
<?php
$a=100;
$b=20;
$c=300;

if($a<$b && $a<$c)


{
echo "Smallest Number is $a";
}
if($b<$a && $b<$c)
{
echo "Smallest Number is $b";
}
if($c<$a && $c<$b)
{
echo "Smallest Number is $c";
}
?>

2) if-else statement:

- if condition is true then program controller executes if body.


- if condition is false then program controller executes else body.
- Syntax:
if(condition)
{
//body of if
}
else
{
//body of else
}
- Example-1:
<?php
$x=100;
$y=500;
if($x>$y)
{
echo "Greatest Number is $x";
}
else
{
echo "Greatest Number is $y";
}
?>
- Example-2:
<?php
$no=14; if($no
%2==0)
{
echo "Number $no is EVEN!!!";
}
else
{
echo "Number $no is ODD!!!";
}
?>

3) Nested-if statement:

- One if-else within another if is known as nested if-else.


- Syntax:
if(condition)
{
if(condition)
{
//body of if
}
else
{
//body of else
}
}
else
{
//body of else
}
- Example:
<?php
$no=-1; if($no!
=0)
{
if($no>0)
{
echo "$no is positive";
}
else
{
echo "$no is negative";
}
}
else
{
echo "0 is neither positive nor negative";
}
?>

***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";
}
?>

You might also like