PHP Internal Practical Code's
PHP Internal Practical Code's
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"
);
<?php
// Input string
$inputString = "hello world";
?>
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>";
// 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";
}
}
// 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);
?>
10. Write a program to design a registration from using text box , radio
button , checkbox and button and also use GET method
<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>
<?php
// Set cookie values
$cookie_name = "user";
$cookie_value = "John Doe";
$expiration = time() + (86400 * 30); // Cookie expires in 30 days (86400 seconds = 1 day)
<?php
// Start session
session_start();
// Output a message
echo "Session started and variables are set.";
?>
14. Write a PHP code to delete and update data into table