0% found this document useful (0 votes)
15 views16 pages

WT - Unit - 4 (Partial)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views16 pages

WT - Unit - 4 (Partial)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

MODULE 4

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

1) Arithmetic operator (+,-,*,/,%)

Program:

<?php
$a=3;
$b=2;
echo $a+$b;
?>

Output:

2) Increment/Decrement operator (++, --)

Program:

<?php
$a=3;
$a++;
echo $a;
?>

Output:

3) Assignment operator (=, +=, -=, *=, /=, %=)

Program:

<?php
$a=3;
$a+=5;
echo $a;
?>

Output:
8

4) Comparison operator (<,>,<=,>=,==,!=)

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:

a and b are not equal

5) Logical Operator (&&, ||, !)

&& => Logical AND


|| => Logical OR

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

7) String operator (., .=)

Program:

<?php
$a="You are writting ";
$b="Test3";
echo $a.$b;
?>

Output:

You are writting Test3

CONDITIONAL STATEMENTS

1)if (if only)


2) if-else
3)if-else-if

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:

Both numbers are same

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:

Both numbers are different

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:

enter the limit value: 5


0
1
2
3
4
5

2)while loop

Program:

<?php
$a=(int)readline("enter the limit value: ");
$i=0;
while($i<=$a)
{
echo"$i \n";
$i++;
}
?>

Output:

enter the limit value: 4


0
1
2
3
4

3) do-while loop

Program:

<?php
$a=(int)readline("enter the limit value: ");
$i=0;
do
{
echo"$i \n";
$i++;
}while($i<=$a);
?>

Output:

enter the limit value: 3


0
1
2
3

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:

Arrays with a numeric index

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

3) Multi dimensional Array

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

Types of Array Operation

1) Add the array value


2) Update the array value
3) Remove the array value
4) Array Sorting

1) Add the array value

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

2) Update the array value

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

3) Remove the array value

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

sort() - sort arrays in ascending order

• rsort() - sort arrays in descending order


• asort() - sort associative arrays in ascending order, according to the value
• ksort() - sort associative arrays in ascending order, according to the key
• arsort() - sort associative arrays in descending order, according to the value
• krsort() - sort associative arrays in descending order, according to the key

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

1) Simple Function Program

Program:

<?php
function mydinner()
{
echo "I like chapathi \n";
}
mydinner();

Output:

I like chapathi

2) Function with arguments

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

3) Function with default argument value

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

4) Function with return value

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

File Operations are :

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:

New file created successfully

2) File Write

Description:

The fwrite() function is used to write to a file.

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

echo "File wrote successfully \n";


?>

Output:

File wrote successfully

3) File Read

Description:

The fread() function reads from an open file.

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:

The fclose() function is used to close an open file.

Program:

<?php
$myfile = fopen("testfile.txt","r");
fclose($myfile);
echo "File closed successfully \n";
?>

Output:

File closed successfully

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.

Two types of form handling:

1) Php Get Form


2) Php Post Form

1) Php Get Form

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

<form action="welcome.php" method="get">


Name: <input type="text" name="name"/>
<input type="submit" value="visit"/>
</form>

welcome.php

<?php
$name=$_GET["name"];//receiving name field value in $name variable
echo "Welcome, $name";
?>

2) Php Post Form

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

<form action="login.php" method="post">


<table>
<tr><td>Name:</td><td> <input type="text" name="name"/></td></tr>
<tr><td>Password:</td><td> <input type="password" name="password"/></td></tr>
<tr><td colspan="2"><input type="submit" value="login"/> </td></tr>
</table>
</form>
login.php
<?php
$name=$_POST["name"];//receiving name field value in $name variable
$password=$_POST["password"];//receiving password field value in $password variable
echo "Welcome: $name, your password is: $password";
?>
Output:

You might also like