PHP Pratical
PHP Pratical
<?php
echo "Hello World";
?>
2. calculate sum
Write PHP script that will take three integer values for and calculate sum of it
<?php
$a = 3;
$b = 5;
$c = 10;
$sum = $a + $b + $c;
?>
<?php
$a = 3;
$b = 5;
$c = 10;
?>
<?php
$string1 = "Hello";
$string2 = "World";
echo $concatenatedString;
?>
5. swap two integer values
Write a PHP Script that will assign two integer values and swap their values.
<?php
$int1 = 5;
$int2 = 8;
$temp = $int1;
$int1 = $int2;
$int2 = $temp;
?>
<?php
$int1 = 5;
$int2 = 8;
$int1 += $int2;
$int2 = $int1 - $int2;
$int1 -= $int2;
?>
<?php
$int1 = 5;
$int2 = 8;
echo "<table>";
echo "<tr>
<th>Operator</th>
<th>Result</th>
</tr>";
echo "<tr>
<td>$int1 + $int2</td>
<td>" . ($int1 + $int2) . "</td>
</tr>";
echo "<tr>
<td>$int1 - $int2</td>
<td>" . ($int1 - $int2) . "</td>
</tr>";
echo "<tr>
<td>$int1 x $int2</td>
<td>" . ($int1 * $int2) . "</td>
</tr>";
echo "<tr>
<td>$int1 / $int2</td>
<td>" . ($int1 / $int2) . "</td>
</tr>";
echo "</table>";
?>
<?php
$num1 = 5;
$num2 = 8;
if ($num1 == $num2) {
echo "The numbers are equal";
} else {
echo "The numbers are not equal";
}
?>
<?php
$num1 = 5;
$num2 = 8;
?>
/+
10. check number is positive or negative
Write a PHP Script that will check number is positive or negative
<?php
$num = 8;
if ($num > 0) {
echo "The number is positive";
} elseif ($num < 0) {
echo "The number is negative";
} else {
echo "The number is zero";
}
?>
<?php
$num = 5;
if ($num % 2 == 0) {
echo "The number is even";
} else {
echo "The number is odd";
}
?>
<?php
$num = 91;
?>
13. Pattern
1
12
123
1234
<?php
?>
14. pattern
1
22
333
4444
<?php
?>
15. Pattern
1234
123
12
1
<?php
?>
16. pattern
4444
333
22
1
<?php
<?php
?>
<?php
?>
19. function that will take an integer value and return sum of digits.
Write a PHP function that will take an integer value and return sum of digits.
<?php
function sumOfDigits($n)
{
$sum = 0;
while ($n > 0)
{
$sum += $n % 10;
$n /= 10;
}
return $sum;
}
?>
<?php
function factorial($n)
{
$res = 1;
for ($i = 2; $i <= $n; $i++)
$res *= $i;
return $res;
}
?>
<?php
function isPrime($n)
{
if ($n <= 1) return false;
for ($i = 2; $i < $n; $i++)
if ($n % $i == 0)
return false;
return true;
}
?>
<?php
function reverseString($str)
{
$newString = "";
for ($i = strlen($str) - 1; $i >= 0; $i--)
$newString .= $str[$i];
return $newString;
}
?>
23. function that checks whether a passed string is palindrome or not
Write a PHP function that checks whether a passed string is palindrome or not?
<?php
function isPalindrome($str)
{
$i = 0;
$j = strlen($str) - 1;
$i++;
$j--;
}
return true;
}
?>
24. simple PHP class which displays the following string : 'MyClass class has
initialized !'
Write a simple PHP class which displays the following string : 'MyClass class has
initialized
!'
<?php
class MyClass
{
public function __construct()
{
echo 'MyClass class has initialized!';
}
}
?>
<?php
class MyClass
{
public function __construct($name)
{
echo "Hello All, I am $name";
}
}
?>
<?php
class ArraySorter
{
public function sort($arr)
{
sort($arr);
return $arr;
}
}
?>
<?php
class Calculator
{
public function __construct($x, $y)
{
$this->x = $x;
$this->y = $y;
}
?>
28. Calculate the difference between two dates using PHP OOP approach.
Calculate the difference between two dates using PHP OOP approach.
Sample Dates : 1981-11-03, 2013-09-04
Expected Result : Difference : 31 years, 10 months, 1 days
<?php
class DateDifference
{
public function calculateDifference($date1, $date2)
{
$date1 = new DateTime($date1);
$date2 = new DateTime($date2);
$difference = $date2->diff($date1);
$years = $difference->y;
$months = $difference->m;
$days = $difference->d;
?>
<?php
?>
30. take name and message from user and display it.
Write a HTML Form & PHP Script that will take name and message from user and
display it.
<html>
<head>
<title>Name & Message Form</title>
</head>
<body>
<form action="displayMessage.php" method="post">
<label for="name">Name: </label>
<input type="text" name="name" />
<?php
?>
<?php
if(isset($_POST['guess'])){
$guess = $_POST['guess'];
if($guess == $secretNumber){
echo "You guessed correctly!";
} else {
echo "Your guess was incorrect. The correct answer was ".$secretNumber.".";
}
}
?>
<?php
if(isset($_POST['guess'])){
$guess = $_POST['guess'];
$attempts = $_POST['attempts'];
if($guess == $secretNumber){
echo "You guessed correctly!";
} else {
$attempts = $attempts + 1;
echo "Your guess was incorrect. The correct answer was ".$secretNumber.".";
}
}
?>
if (isset($_POST['submit'])) {
// collect the user input
$name = $_POST['name'];
$subject1 = $_POST['subject1'];
$subject2 = $_POST['subject2'];
$subject3 = $_POST['subject3'];
$subject4 = $_POST['subject4'];
$subject5 = $_POST['subject5'];
$totalMarksObtained = $subject1 + $subject2 + $subject3 + $subject4 +
$subject5;
$totalMarks = 500;
?>
<?php
?>
<!-- Create the form -->
<form action="" method="POST">
To: <input type="text" name="to">
<br><br>Subject: <input type="text" name="subject">
<br><br>Message:<br><textarea name="message" rows="10" cols="50"></textarea>
<br><br><input type="submit" value="Send Mail">
</form>
<?php
?>
<?php
?>
<?php
?>
<?php
?>
<?php
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr><th>ID</th><th>Subject</th><th>Grade</th></tr>";
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["subject"]."</td><td>".
$row["grade"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
<?php
?>
?>
<?php
$file = 'data.txt';
if(file_exists($file)){
$data = file_get_contents($file);
echo json_encode($data);
}
?>
<script>
$.ajax({
type: 'GET',
url: 'data.php',
dataType: 'json',
success: function(data){
// Do something with the returned data
console.log(data);
}
});
</script>
if(isset($_GET['term'])){
$term = $_GET['term'];
$list = array();
foreach($names as $name){
if(stristr($name, $term)){
$list[] = $name;
}
}
echo json_encode($list);
}
?>
<script>
$('#name').keyup(function(){
var term = $(this).val();
$.ajax({
type: 'GET',
url: 'names.php',
data: {
term: term
},
dataType: 'json',
success: function(data){
// Do something with the returned data
console.log(data);
}
});
});
</script>
<?php
$employees = array(
array('name' => 'John', 'age' => '30', 'role' => 'Manager'),
array('name' => 'Jane', 'age' => '25', 'role' => 'Developer'),
array('name' => 'Jack', 'age' => '27', 'role' => 'Designer')
);
if(isset($_GET['name'])){
$name = $_GET['name'];
foreach($employees as $employee){
if($employee['name'] == $name){
echo json_encode($employee);
break;
}
}
}
?>
<script>
$('#names').change(function(){
var name = $(this).val();
$.ajax({
type: 'GET',
url: 'employees.php',
data: {
name: name
},
dataType: 'json',
success: function(data){
// Do something with the returned data
console.log(data);
}
});
});
</script>
46. jquery code that hide / show text on button click event
Write a jquery code that hide / show text on button click event.
47. jquery code that will fade in, fade out,fade toggle images on click event
Write a jquery code that will fade in, fade out,fade toggle images on click event.
48. slide up, slide down and slide togle panel on click event.
Write a jquery code that will slide up, slide down and slide togle panel on click
event.