WT - Unit - 4 (Partial)
WT - Unit - 4 (Partial)
OPERATORS IN PHP
1) Arithmetic operator
2) Increment/decrement operator
3) Assignment operator
4) Comparison operator
5) Logical operator
6) Ternary operator
7) String operator
Program:
<?php
$a=3;
$b=2;
echo $a+$b;
?>
Output:
Program:
<?php
$a=3;
$a++;
echo $a;
?>
Output:
Program:
<?php
$a=3;
$a+=5;
echo $a;
?>
Output:
8
Program:
<?php
$a=3;
$b=5;
if($a==$b)
{
echo"a and b are equal \n";
}
else
{
echo"a and b are not equal \n";
}
?>
Output:
Program:
<?php
$a=3;
$b=5;
$c=8;
if(($a>$b)&&($a>$c))
{
echo $a." is big \n";
}
else if($b>$c)
{
echo $b." is big \n";
}
else
{
echo $c." is big \n";
}
?>
Output:
8 is big
6) Ternary operator (condn?true:false)
Program:
<?php
$a=3;
$b=5;
$c=($a>$b)?$a:$b;
echo $c." is biggest number \n";
?>
Output:
5 is biggest number
Program:
<?php
$a="You are writting ";
$b="Test3";
echo $a.$b;
?>
Output:
CONDITIONAL STATEMENTS
1) if (if only)
The if statement is same as in other programming languages. It is used to perform basic condition
based task. It is used to decide whether a certain statement or block of statements will be executed
or not i.e if a certain condition is true then a block of statement is executed otherwise not.
Syntax:
if(condition)
{
# code to be executed
}
Program:
<?php
$a=2;
$b=2;
if($a == $b)
{
echo"Both numbers are same \n";
}
?>
Output:
2) if-else
The if statement evaluates the code if the condition is true but what if the condition is not true, here
comes the else statement. It tells the code what to do when the if condition is false.
Syntax:
if(condition)
{
# code if condition is true
}
else
{
# code if condition is false
}
Program:
<?php
$a=3;
$b=2;
if($a == $b)
{
echo"Both numbers are same \n";
}
else
{
echo "Both numbers are different \n";
}
?>
Output:
3) if-else-if
Here, a user can decide among multiple options. The if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement associated with that get
executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else
statement will be executed.
Syntax:
if(condition1)
{
# code to be executed if condition1 is true
}
elsif(condition2)
{
# code to be executed if condition2 is true
}
elsif(condition3)
{
# code to be executed if condition3 is true
}
...
else
{
# code to be executed if all the conditions are false
}
Program:
<?php
$a=3;
$b=5;
$c=8;
if(($a>$b)&&($a>$c))
{
echo $a." is big \n";
}
else if($b>$c)
{
echo $b." is big \n";
}
else
{
echo $c." is big \n";
}
?>
Output:
8 is big
Looping Statements
1)for loop
2)while loop
3) do-while loop
1) For loop
Program:
<?php
$a=(int)readline("enter the limit value: ");
for($i=0;$i<=$a;$i++)
{
echo"$i \n";
}
?>
Output:
2)while loop
Program:
<?php
$a=(int)readline("enter the limit value: ");
$i=0;
while($i<=$a)
{
echo"$i \n";
$i++;
}
?>
Output:
3) do-while loop
Program:
<?php
$a=(int)readline("enter the limit value: ");
$i=0;
do
{
echo"$i \n";
$i++;
}while($i<=$a);
?>
Output:
ARRAYS IN PHP
Description:
An array is a special variable that can hold many values under a single name, and you can access
the values by referring to an index number or name.
Types of Array
1) Indexed Array
2) Associative Array
3) Multi dimensional Array
1) Indexed Array:
Program:
<?php
$arr = array(1,2,3,4,5);
foreach ($arr as $x)
{
echo"$x \n";
}
?>
Output:
1
2
3
4
5
2) Associative Array
We can associate name with each array elements in PHP using => symbol.
Program:
<?php
$arr = array("a"=>1,"b"=>2,"c"=>3,"d"=>4,"e"=>5);
foreach ($arr as $x)
{
echo"$x \n";
}
?>
Output:
1
2
3
4
5
PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in
an array. PHP multidimensional array can be represented in the form of matrix which is represented
by row * column.
Program:
<?php
$arr = array(
array(101,"anbu","2k"),
array(201,"baby","3k"),
array(301,"charlin","5k")
);
for($row=0;$row<3;$row++)
{
for($col=0;$col<3;$col++)
{
echo $arr[$row][$col]." ";
}
echo"\n";
}
?>
Output:
101 anbu 2k
201 baby 3k
301 charlin 5k
Program:
<?php
$arr = array("a"=>1,"b"=>2,"c"=>3,"d"=>4,"e"=>5);
$arr["u"]=10;
foreach ($arr as $x)
{
echo"$x \n";
}
?>
Output:
1
2
3
4
5
10
Program
<?php
$arr = array("a"=>1,"b"=>2,"c"=>3,"d"=>4,"e"=>5);
$arr["k"]=15;
foreach ($arr as $x)
{
echo"$x \n";
}
?>
Output:
1
2
3
4
5
15
Program:
<?php
$arr = array("a"=>1,"b"=>2,"c"=>3,"d"=>4,"e"=>5);
unset($arr["b"]);
foreach ($arr as $x)
{
echo"$x \n";
}
?>
Output:
1
3
4
5
4) Array Sorting
Program:
<?php
$arr = array("a"=>1,"b"=>2,"c"=>3,"d"=>4,"e"=>5);
rsort($arr);
foreach ($arr as $x)
{
echo"$x \n";
}
?>
Output:
5
4
3
2
1
FUNCTIONS
Description:
PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a
specific task.
Classification:
1) Simple function program
2) Function with arguments
3) Function with default argument value
4) Function with return value
Program:
<?php
function mydinner()
{
echo "I like chapathi \n";
}
mydinner();
Output:
I like chapathi
Arguments are specified after the function name, inside the parentheses. You can add as many
arguments as you want, just separate them with a comma.
Program:
<?php
function mynumber($a)
{
echo "a value = ".$a."\n";
}
mynumber(35);
Output:
a value = 35
Program:
<?php
function mynumber($a=35) /*default argument value*/
{
echo "a value = ".$a."\n";
}
mynumber(25);
mynumber();
mynumber(45);
Output:
a value = 25
a value = 35
a value = 45
Program:
<?php
function mysweet($a)
{
$a+=5;
return $a;
}
echo "a value = ".mysweet(25)."\n";
?>
Output:
a value = 30
FILES OPERATIONS
Modes Description
r Open a file for read only. File pointer starts at the beginning of the file
Open a file for write only. Erases the contents of the file or creates a new file if it doesn't
w
exist. File pointer starts at the beginning of the file
Open a file for write only. The existing data in file is preserved. File pointer starts at the
a
end of the file. Creates a new file if the file doesn't exist
x Creates a new file for write only. Returns FALSE and an error if file already exists
r+ Open a file for read/write. File pointer starts at the beginning of the file
Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't
w+
exist. File pointer starts at the beginning of the file
Open a file for read/write. The existing data in file is preserved. File pointer starts at the
a+
end of the file. Creates a new file if the file doesn't exist
x+ Creates a new file for read/write. Returns FALSE and an error if file already exists
1) File Create
2) File Write
3) File Read
4) File Close
1) File Create
Description:
The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is
created using the same function used to open files.
If you use fopen() on a file that does not exist, it will create it, given that the file is opened for
writing (w) or appending (a).
Program:
<?php
$myfile = fopen("testfile.txt","w");
echo"New file created successfully \n";
?>
Output:
2) File Write
Description:
The first parameter of fwrite() contains the name of the file to write to and the second
parameter is the string to be written.
Program:
<?php
$myfile = fopen("testfile.txt","w") or die("Unable to open file");
$a = "welcome to php";
fwrite($myfile, $a);
fclose($myfile);
Output:
3) File Read
Description:
The first parameter of fread() contains the name of the file to read from and the second
parameter specifies the maximum number of bytes to read.
Program:
<?php
$myfile = fopen("testfile.txt","r") or die ("Unable to open file");
echo fread($myfile,filesize("testfile.txt"));
fclose($myfile);
?>
Output:
welcome to php
4) File Close
Description:
Program:
<?php
$myfile = fopen("testfile.txt","r");
fclose($myfile);
echo "File closed successfully \n";
?>
Output:
FORM HANDLING
Description:
We can create and use forms in PHP. To get form data, we need to use PHP superglobals $_GET
and $_POST.
The form request may be get or post. To retrieve data from get request, we need to use $_GET, for
post request $_POST.
Description:
Get request is the default form request. The data passed through get request is visible on the URL
browser so it is not secured. You can send limited amount of data through get request.
form1.html
welcome.php
<?php
$name=$_GET["name"];//receiving name field value in $name variable
echo "Welcome, $name";
?>
Description:
Post request is widely used to submit form that have large amount of data such as file upload, image
upload, login form, registration form etc.
The data passed through post request is not visible on the URL browser so it is secured. You can
send large amount of data through post request.
form1.html