You are on page 1of 35

PROGRAM:

<!DOCTYPE html>
<html>
<body>
<?php
//Initialize matrix a
$a = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);

//Calculates number of rows and columns present in given matrix


$rows = count($a);
$cols = count($a[0]);

//Calculates sum of each row of given matrix


for($i = 0; $i < $rows; $i++){
$sumRow = 0;
for($j = 0; $j < $cols; $j++){
$sumRow = $sumRow + $a[$i][$j];
}
print("Sum of " . ($i+1) ." row: " . $sumRow);
print("<br>");
}

//Calculates sum of each column of given matrix


for($i = 0; $i < $cols; $i++){
$sumCol = 0;
for($j = 0; $j < $rows; $j++){
$sumCol = $sumCol + $a[$j][$i];
}
print("Sum of " . ($i+1) . " column: " . $sumCol);
print("<br>");
}
?>
</body>
</html>
OUTPUT:
PROGRAM:

<?php

$count = 0;

$num = 2;

while ($count < 15 )

$div_count=0;

for ( $i=1; $i<=$num; $i++)

if (($num%$i)==0)

$div_count++;

if ($div_count<3)

echo $num." , ";

$count=$count+1;

$num=$num+1;

?>
OUTPUT:
PROGRAM:

<?php

// Declare variable and initialize

// it to email

$email = "monika.cs@jeppiaararts.in";

// Validate email

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

echo("$email is a valid email address");

else {

echo("$email is not a valid email address");

?>
OUTPUT:
PROGRAM:

<?php

function word_digit($word) {

$warr = explode(';',$word);

$result = '';

foreach($warr as $value){

switch(trim($value)){

case 'zero':

$result .= '0';

break;

case 'one':

$result .= '1';

break;

case 'two':

$result .= '2';

break;

case 'three':

$result .= '3';

break;

case 'four':

$result .= '4';

break;

case 'five':
$result .= '5';

break;

case 'six':

$result .= '6';

break;

case 'seven':

$result .= '7';

break;

case 'eight':

$result .= '8';

break;

case 'nine':

$result .= '9';

break;

return $result;

echo word_digit("nine;five;three;one;six;eight")."<br>";

echo word_digit("one;five;eight")."\n";

?>
OUTPUT:
PROGRAM:

<?php

// current time

echo date('h:i:s') . "\n";

// sleep for 5 seconds

sleep(5);

// wake up

echo date('h:i:s')."\n";

?>
OUTPUT:
PROGRAM:

<?php

// Text to replace

$text = "Jeppiaar College";

// The preg_replace is used here to replace the

// color of first character of the word

$text = preg_replace('/(\b[a-z])/i','<span style="color:red;">\1</span>',


$text);

// Display the text value

echo $text

?>
OUTPUT:
PROGRAM:

<!DOCTYPE html>

<html>

<body>

<center>

<h1 .000000bnstyle="color: green;">

WELCOME TO JCAS

</h1>

<h3>

Program to print multiplication<br>

table of any number in PHP

</h3>

<form method="POST">

Enter a number:

<input type="text" name="number">

<input type="Submit"

value="Get Multiplication Table">

</form>

</center>

</body>

</html>

<?php

if($_POST) {
$num = $_POST["number"];

echo nl2br("<p style='text-align: center;'>

Multiplication Table of $num: </p>

");

for ($i = 1; $i <= 10; $i++) {

echo ("<p style='text-align: center;'>$num"

. " X " . "$i" . " = "

. $num * $i . "</p>

");

?>
OUTPUT:
PROGRAM:

<?php

$num=4;

$factorial=1;

for($x=$num; $x>=1; $x--)

$factorial=$factorial*$x;

echo “Factorial of $num is $factorial”;

?>
OUTPUT:
PROGRAM:

<?php

$file=file('text.txt');

$fp=fopen('new.txt','w');

$read_rev = array_reverse($file);

$count=0;

foreach($read_rev as $dis_line)

print_r($dis_line);

fwrite($fp,$dis_line);

echo"<br/>";

?>
OUTPUT:
PROGRAM:

<html>

<body>

<h2>Change the Extension of all the text files in a directory</h2>

<br>

<form method ="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

Enter the directory (.for current directory )<input type="text" name="dir"

size="50"><br> Enter the new file extension <input type="text" name="ext"

size="20"><br>

<input type="submit" name="submit" value="submit form"><br>

</form>

<?php

if(isset($_POST['submit']) && trim($_POST['dir']) !='')

$dirname=$_POST['dir'];

$newextension=$_POST['ext'];

$files=scandir($dirname)

; foreach($files as $file)

if (trim($dirname) !=".")

$newfile=$dirname."/".$f

ile; else

$newfile=$file;

echo "<br>";

if (pathinfo ($newfile,PATHINFO_EXTENSION)=="txt")

{
echo "<br>Old File:".$newfile;

$file_woext=substr($newfile,0,strrpos($newfile,"."));

rename($newfile,$file_woext.".".$newextension);

echo"<br>New File:".$file_woext.".".$newextension;

if(isset ($_POST['submit']) && trim($_POST['dir'])=='')

echo"<br>Directory name can not be empty!";

?>

</body>

</html>
OUTPUT
PROGRAM:

<?php

$dirpath=getcwd();

$dirpath.="\*.php";

$files=array();

$files=glob($dirpath);

usort($files,function($

x,$y){

return filemtime($x) <filemtime($y);


});

foreach($files as $item){
echo basename($item)."=>Last Modified On".@date('F d,Y,H:i:s',
filemtime($item))."<br/>";
}

?>
OUTPUT:
PROGRAM:

CREATE DATABASE dbbookstore;


CREATE TABLE 'books"
('id' int(11) NOT NULL,
`title` varchar(500) NOT NULL,
"author_name` varchar(500) NOT NULL,
`price` varchar(500) NOT NULL,
ISBN varchar(50) NOT NULL,
`category` varchar(100) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
INSERT INTO 'books' ('id', 'title', 'author_name', 'price', 'ISBN', 'category') VALUES
(1, 'C++ By Example', 'John', '500', 'PR-123-A1', 'Programming'),
(2, 'Java Book', 'Jane davis', '450', 'PR-456-A2', 'Programming'),
(3, 'Database Management Systems', 'Mark', '300', 'DB-123-ASD', 'Database'),
(4, 'Harry Potter and the Order of the Phoenix', 'J.K. Rowling', '650', 'FC-123-456', 'Novel'),
(5, 'Pride and Prejudice', 'Jane Austen', '450', 'FC-456-678', 'Novel'),
(6, 'Learning Web Development', 'Michael', '300', 'ABC-123-456', 'Web Development'),
(7, 'Professional PHP & MYSQL', 'Programmer Blog', '340', 'PR-123-456', 'Web Development');
ALTER TABLE 'books' ADD PRIMARY KEY ('id');
ALTER TABLE 'books' MODIFY 'id' int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=8
OUTPUT:

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 7 rows affected (0.01 sec)


Records: 7 Duplicates: 0 Warnings: 0

Query OK, 0 rows affected (0.01 sec)


PROGRAM:

Email.xml

<?xml version="1.0" encoding="utf-8"?>

<company>

<employee>

<department>Production</department>
<empname>Raman</empname>

<empid>1000</empid>

<email>raman@gmail.com</email>

</employee>
<employee>

<department>Sales</department>

<empname>Sunil</empname>

<empid>1003</empid>
<email>sunil@gmail.com</email>

</employee>

<employee>

<department>Quality Control</department>

<empname>Rani</empname>
<empid>1011</empid>

<email>rani@gmail.com</email>
</employee>

</company>
<?php

$xml= simplexml_load_file('email.xml') or die ("Unable to load XML");

echo "There are".count($xml->employee)."employees working in this

company."; echo"<br>Employee ID and their E-Mail addresses

are:";

foreach ($xml->employee as $emp)


{

echo"<br>".$emp-

>empid; echo"-

".$emp->email;
}

?>
OUTPUT:
PROGRAM:

<?php

$domdoc=new DOMDocument();

$domdoc->load('tree.xml');

$domdoc->preserveWhiteSpace=false;

$docroot=$domdoc->documentElement;

$name=$domdoc->getElementsByTagName("name")->item(0)-

>nodeValue; echo"<br>Name accessed using

getElementsByTagName()method:".$name; foreach ($docroot-

>childNodes as $child)

echo"<br>Name accessed using childNodes property:".$child->nodeValue;

echo"<br>Name accessed using childNodes without traversal of all the elements:"

.$docroot->childNodes->item(0)->nodeValue;

?>
OUTPUT:
PROGRAM

conn.php
<?php
Sconn=mysqli_connect("localhost","root", "", "basic command");
//Check connection
if (mysqli_connect_errno())
echo "Failed to connect to MySQL:". mysqli_connect_error();
?>
index.php
<!DOCTYPE html>
<html>
<head>
<title>Basic MySQLi Commands</title>
</head>
<body>
<div>
<form method="POST" action="add.php">
<label>Firstname:</label><input type="text" name="firstname">
<label>Lastname:</label><input type="text" name="lastname">
<input type="submit" name="add">
</form>
</div>
<br>
<div>
<table border="1">
<thead>
<th>Firstname</th>
<th>Lastname</th>
<th></th>
</thead>
<tbody>
<?php
include('conn.php');
$query=mysqli_query(Sconn, "select * from "user");
while($row=mysqli_fetch_array($query)){
?>
<tr>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td>
Srow['userid']; ?>">Edit</a>
<a href="edit.php?id=<?php echo
<a href="delete.php?id=<?php echo
$row['userid']; ?>">Delete</a>
</td>
</tr
<?php
?>
</tbody>
</table>
</div>
</body>
</html>
add.php
<?php
include('conn.php');
Sfirstname=$_POST['firstname'];
Slastname=$_POST['lastname'];
mysqli_query($conn, "insert into "user" (firstname, lastname) values ('$firstname',
'$lastname')");
header('location:index.php");
?>
delete.php
<?php
Sid=$_GET['id'];
include('conn.php");
mysqli_query(Sconn, delete from user where userid="Sid");
header('location:index.php");
7>
edit.php
<?php
include('conn.php');
$id=$_GET['id'];
$query=mysqli_query(Sconn, "select * from 'user' where userid="Sid");
$row=mysqli_fetch_array($query);
<!DOCTYPE html>
<html>
<head>
<title>Basic MySQLi Commands</title>
</head>
body>
<h2>Edit</h2>
<form method="POST" action="update.php?id=<?php echo Sid; ?>">
<label>Firstname:</label><input type="text" value="<?php echo $row['firstname']; ?>"
name="firstname">
<label>Lastname:</label><input type="text" value="<?php echo Srow['lastname']; ?>"
name="lastname">
<input type="submit" name="submit">
<a href="index.php">Back</a>
</form>
</body>
</html>
update.php
<?php
include('conn.php');
$id=$_GET['id'];
$firstname=S_POST['firstname'];
$lastname=$_POST['lastname'];
mysqli_query($conn, "update 'user' set firstname='Sfirstname', lastname='$lastname'
where userid="$id");
header('location:index.php');
?>
OUTPUT:

XML generated successfully.

You might also like