PHP Internal Practical Code's

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

PHP Internal Practical Code’s

1. write a php program to demonstrate the use of looping structures using while
and do while loop

<?php
// While loop example
echo "While Loop Example:<br>";
$num1 = 1;
while ($num1 <= 5) {
echo "Number: $num1 <br>";
$num1++;
}
echo "<br>";
// Do-while loop example
echo "Do-While Loop Example:<br>";
$num2 = 1;
do {
echo "Number: $num2 <br>";
$num2++;
} while ($num2 <= 5);
?>

2. write a php program to demonstrate the use of looping structures using for
loop and foreach loop

<?php
// Using for loop
echo "Using for loop:\n";
for ($i = 0; $i < 5; $i++) {
echo "<br>Iteration $i";
}
echo "<br>";
// Using foreach loop
echo "\nUsing foreach loop:\n";
$colors = array("Red", "Green", "Blue", "Yellow","Orange");
foreach ($colors as $color) {
echo "<br>Color: $color\n";
}

?>
3. Write a PHP Program to Create Associative Array..

<?php
// Creating an associative array
$person = array(
"name" => "John",
"age" => 30,
"city" => "New York"
);

// Accessing elements of the associative array


echo "Name: " . $person["name"] . "<br>";
echo "Age: " . $person["age"] . "<br>";
echo "City: " . $person["city"] . "<br>";
?>

4. Write a PHP Program by using following function:- a.ucword() b. strpos()

<?php

// Input string
$inputString = "hello world";

// Capitalize the first character of each word


$capitalizedString = ucwords($inputString);

// Find the position of a substring in the string


$searchString = "world";
$position = strpos($inputString, $searchString);

// Output the results


echo "Original string: " . $inputString . "<br>";
echo "Capitalized string: " . $capitalizedString . "<br>";
echo "Position of '" . $searchString . "': " . $position . "<br>";

?>
5. Write a PHP program by using following function :- a. str_word_count()
b.strle() c.strtoUpper() d.strtoLower()

<?php
// Using str_word_count() function to count the number of words in a string
$string = "Hello world, how are you?";
$wordCount = str_word_count($string);
echo "Word count: " . $wordCount . "<br>";

// Using strlen() function to get the length of a string


$length = strlen($string);
echo "String length: " . $length . "<br>";

// Using strtoupper() function to convert string to uppercase


$upperCaseString = strtoupper($string);
echo "Uppercase string: " . $upperCaseString . "<br>";

// Using strtolower() function to convert string to lowercase


$lowerCaseString = strtolower($string);
echo "Lowercase string: " . $lowerCaseString . "<br>";
?>

6. Write a program to demonstrate anonymous function


<?php

// Define an array of numbers


$numbers = [1, 2, 3, 4, 5];

// Define an anonymous function to square a number


$square = function($x)
{
return $x * $x;
};

// Use array_map to apply the anonymous function to each element of the array
$squared_numbers = array_map($square, $numbers);
// Print the original and squared numbers
echo "Original numbers: " . implode(", ", $numbers) . "\n";
echo "<br>Squared numbers: " . implode(", ", $squared_numbers) . "\n";

?>
7. Write a program to create PDF using PHP

<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Practical No 7');
$pdf->Output();
?>
8. Write a PHP program to implement inheritance and constructor in the code

<?php
// Parent class
class Animal {
public $name;

// Constructor
public function __construct($name) {
$this->name = $name;
}

// Method
public function sound() {
echo "Animal makes a sound";
}
}

// Child class inheriting from parent class


class Dog extends Animal {
// Constructor overriding parent constructor
public function __construct($name) {
parent::__construct($name);
}

// Method overriding parent method


public function sound() {
echo "Dog barks";
}
}

// Create an instance of the child class


$dog = new Dog("Buddy");

// Output
echo "Name: " . $dog->name . "<br>";
echo "Sound: ";
$dog->sound();
?>
9. Write a simple PHP program on introspection

<?php
// Define a class
class MyClass {
public $prop1;
protected $prop2;
private $prop3;

// Constructor
public function __construct($val1, $val2, $val3) {
$this->prop1 = $val1;
$this->prop2 = $val2;
$this->prop3 = $val3;
}

// Method
public function myMethod() {
echo "This is myMethod";
}
}
// Create an instance of the class
$obj = new MyClass(1, 2, 3);

// Get class name


$class_name = get_class($obj);
echo "Class Name: $class_name <br>";

// Check if an object is an instance of a particular class


if (is_a($obj, 'MyClass')) {
echo "Object is an instance of MyClass <br>";
}

// Get class methods


$class_methods = get_class_methods($obj);
echo "Class Methods: " . implode(", ", $class_methods) . "<br>";

// Check if a method exists in a class


if (method_exists($obj, 'myMethod')) {
echo "Method myMethod exists in MyClass <br>";
}

?>
10. Write a program to design a registration from using text box , radio
button , checkbox and button and also use GET method

HTML Form (registration_form.html):


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration Form</title>
</head>
<body>
<h2>Registration Form</h2>
<form action="process_registration.php" method="GET">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>

<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>

<label for="gender">Gender:</label>
<input type="radio" id="male" name="gender" value="male" required>
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br><br>

<label for="interests">Interests:</label><br>
<input type="checkbox" id="sports" name="interests[]" value="sports">
<label for="sports">Sports</label><br>
<input type="checkbox" id="music" name="interests[]" value="music">
<label for="music">Music</label><br>
<input type="checkbox" id="reading" name="interests[]" value="reading">
<label for="reading">Reading</label><br><br>

<input type="submit" value="Submit">


</form>
</body>
</html>
PHP File to Process Form Data (process_registration.php):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration Form Submission</title>
</head>
<body>
<h2>Registration Form Submission</h2>
<?php
// Check if form fields are set and not empty
if (isset($_GET['name']) && isset($_GET['email']) && isset($_GET['gender']) &&
isset($_GET['interests'])) {
$name = $_GET['name'];
$email = $_GET['email'];
$gender = $_GET['gender'];
$interests = implode(', ', $_GET['interests']);

echo "<p>Name: $name</p>";


echo "<p>Email: $email</p>";
echo "<p>Gender: $gender</p>";
echo "<p>Interests: $interests</p>";
} else {
echo "<p>Form submission failed. Please make sure all required fields are filled.</p>";
}
?>
</body>
</html>
11.Write a simple PHP program to create cookies

<?php
// Set cookie values
$cookie_name = "user";
$cookie_value = "John Doe";
$expiration = time() + (86400 * 30); // Cookie expires in 30 days (86400 seconds = 1 day)

// Set the cookie


setcookie($cookie_name, $cookie_value, $expiration, "/");

// Check if cookie is set


if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
12. Write a program to start a session in PHP

<?php
// Start session
session_start();

// Set session variables


$_SESSION["username"] = "John";
$_SESSION["email"] = "john@example.com";

// Output a message
echo "Session started and variables are set.";
?>

13. Write a PHP code to insert data into table

14. Write a PHP code to delete and update data into table

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy