Tomorrow I LL Learn PHP Global Variables. This Is A Bad Command: Del C: .
Tomorrow I LL Learn PHP Global Variables. This Is A Bad Command: Del C: .
php
// write php to display the following:
//'Tomorrow I \'ll learn PHP global variables.'
//'This is a bad command : del c:\\*.*'
echo 'Tomorrow I \'ll learn PHP global variables.';
echo 'This is a bad command : del c:\\*.*';
?>
Q2. <?php
// write php to display count, from 5 to 15
$count = 5;
while ($count <= 15) {
echo $count . "<br>";
$count++;
}
?>
Q3. // Create a simple form that will collect data from user, such as user name, email and message text using PHP
// The form should be displayed on the page using HTML and CSS.
// The form should be submitted to the same page using POST method.
// The form should be validated to make sure that the user has entered all the required data.
// The form should be validated to make sure that the user has entered a valid email address.
// The form should be validated to make sure that the user has entered a message that is at least 10 characters long.
// The form should be validated to make sure that the user has entered a message that is no more than 500 characters long.
<html>
<head>
<title>Form</title>
<style>
.error {
color: red;
}
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="name">Name:</label>
<input type="text" name="name" id="name">
<span class="error"><?php echo $nameErr; ?></span>
<br>
<label for="email">Email:</label>
<input type="text" name="email" id="email">
<span class="error"><?php echo $emailErr; ?></span>
<br>
<label for="message">Message:</label>
<textarea name="message" id="message" cols="30" rows="10"></textarea>
<span class="error"><?php echo $messageErr; ?></span>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Q4. // Write a PHP Library file which has methods for sorting strings using bubble sort.
// Design Sortingservice page where it asks user to select sorting technique and Ask user to enter strings to be sorted.
// When user clicks on sort button it should display results in text area which displays input strings and proper output strings
in sorted area.
// Use PHP classes to implement this.
<html>
<head>
<title>Bubble Sort</title>
</head>
<body>
<form method="post">
<table>
<tr>
<td>Enter String</td>
<td><input type="text" name="string"/></td>
</tr>
<tr>
<td>Select Sorting Technique</td>
<td>
<select name="sort">
<option value="bubble">Bubble Sort</option>
<option value="insertion">Insertion Sort</option>
<option value="selection">Selection Sort</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Sort"/></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit'])){
$string = $_POST['string'];
$sort = $_POST['sort'];
$string = explode(",",$string);
$string = array_map('trim',$string);
$string = array_map('strtolower',$string);
$string = array_map('ucfirst',$string);
if($sort == "bubble"){
$string = bubbleSort($string);
}
else if($sort == "insertion"){
$string = insertionSort($string);
}
else if($sort == "selection"){
$string = selectionSort($string);
}
echo "<textarea>";
foreach($string as $value){
echo $value."\n";
}
echo "</textarea>";
}
function bubbleSort($string){
$n = count($string);
for($i=0;$i<$n;$i++){
for($j=0;$j<$n-1;$j++){
if($string[$j]>$string[$j+1]){
$temp = $string[$j];
$string[$j] = $string[$j+1];
$string[$j+1] = $temp;
}
}
}
return $string;
}
function insertionSort($string){
$n = count($string);
for($i=1;$i<$n;$i++){
$key = $string[$i];
$j = $i-1;
while($j>=0 && $string[$j]>$key){
$string[$j+1] = $string[$j];
$j--;
}
$string[$j+1] = $key;
}
return $string;
}
function selectionSort($string){
$n = count($string);
for($i=0;$i<$n-1;$i++){
$min = $i;
for($j=$i+1;$j<$n;$j++){
if($string[$j]<$string[$min]){
$min = $j;
}
}
$temp = $string[$i];
$string[$i] = $string[$min];
$string[$min] = $temp;
}
return $string;
}
?>
</body>
</html>
Q5. <?php
$str = trim(fgets(STDIN));
$arr = explode(' ', $str);
$arr2 = array_count_values($arr);
$word1 = array_search(max($arr2), $arr2);
$word2 = '';
foreach ($arr as $s) {
if (strlen($s) > strlen($word2)) {
$word2 = $s;
}
}
printf("%s %s\n", $word1, $word2);
?>
Q6. // Create a database testdb in Mysql from PHP and then create a table student (emp_id, emp_Name, emp_address,
emp_salary, joining date) and perform following operations:
// 1. Insert record in the table
// 2. Display all records from the table
// 3. Delete a record from the table
// 4. Update a record in the table
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
$servername = "localhost:8080";
$username = "root";
$password = "root";
$dbname = "testdb";
?>
<form action="q6.php" method="post">
<table>
<tr>
<td>Employee ID</td>
<td><input type="text" name="emp_id" /></td>
</tr>
<tr>
<td>Employee Name</td>
<td><input type="text" name="emp_name" /></td>
</tr>
<tr>
<td>Employee Address</td>
<td><input type="text" name="emp_address" /></td>
</tr>
<tr>
<td>Employee Salary</td>
<td><input type="text" name="emp_salary" /></td>
</tr>
<tr>
<td>Joining Date</td>
<td><input type="text" name="joining_date" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit'])){
$emp_id = $_POST['emp_id'];
$emp_name = $_POST['emp_name'];
$emp_address = $_POST['emp_address'];
$emp_salary = $_POST['emp_salary'];
$joining_date = $_POST['joining_date'];
}
session_start();
$_SESSION['emp_id'] = $emp_id;
$_SESSION['emp_name'] = $emp_name;
$_SESSION['emp_address'] = $emp_address;
$_SESSION['emp_salary'] = $emp_salary;
$_SESSION['joining_date'] = $joining_date;
session_write_close();
?>
</body>
</html>