0% found this document useful (0 votes)
8 views9 pages

Complete PHP Programs Bhakti

The document is a comprehensive PHP question bank containing 25 questions and their corresponding code solutions. It covers various PHP concepts such as conditional statements, loops, functions, arrays, classes, forms, session management, and database operations. Each question includes a brief description and a PHP code snippet demonstrating the solution.

Uploaded by

sanikakabade07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Complete PHP Programs Bhakti

The document is a comprehensive PHP question bank containing 25 questions and their corresponding code solutions. It covers various PHP concepts such as conditional statements, loops, functions, arrays, classes, forms, session management, and database operations. Each question includes a brief description and a PHP code snippet demonstrating the solution.

Uploaded by

sanikakabade07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Complete PHP Question Bank - Bhakti

Q1. Check if a number is positive or negative

<?php

$num = -5;

if ($num > 0) {

echo "$num is positive";

} elseif ($num < 0) {

echo "$num is negative";

} else {

echo "Number is zero";

?>

Q2. Find the greatest among three numbers

<?php

$a = 10; $b = 20; $c = 15;

$max = max($a, $b, $c);

echo "Greatest number is $max";

?>

Q3. Print first 30 even numbers using while loop

<?php

$i = 1; $count = 0;

while ($count < 30) {

if ($i % 2 == 0) {

echo "$i ";

$count++;
}

$i++;

?>

Q4. Program using associative array

<?php

$student = array("Name"=>"Bhakti", "Age"=>20, "Course"=>"Computer");

foreach ($student as $key => $value) {

echo "$key: $value\n";

?>

Q5. Calculate length of a string

<?php

$str = "Hello World";

echo "Length of string: " . strlen($str);

?>

Q6. Count words in string without using string function

<?php

$str = "Hello this is PHP";

$count = 1;

for ($i = 0; $i < strlen($str); $i++) {

if ($str[$i] == ' ') {

$count++;

echo "Word count: $count";


?>

Q7. Parameterized function

<?php

function greet($name) {

echo "Hello, $name";

greet("Bhakti");

?>

Q8. Create PDF using graphics concept (use FPDF)

<?php

require('fpdf.php');

$pdf = new FPDF();

$pdf->AddPage();

$pdf->SetFont('Arial','B',16);

$pdf->Cell(40,10,'Hello Bhakti!');

$pdf->Output();

?>

Q9. Demonstrate multiple inheritance (via traits)

<?php

trait A {

function msgA() {

echo "Hello from A";

trait B {

function msgB() {
echo "Hello from B";

class C {

use A, B;

$obj = new C();

$obj->msgA();

$obj->msgB();

?>

Q10. Demonstrate multilevel inheritance

<?php

class A {

function showA() {

echo "Class A";

class B extends A {

function showB() {

echo "Class B";

class C extends B {

function showC() {

echo "Class C";

}
}

$obj = new C();

$obj->showA();

$obj->showB();

$obj->showC();

?>

Q11. Parameterized constructor

<?php

class Student {

function __construct($name) {

echo "Student name is $name";

$obj = new Student("Bhakti");

?>

Q12. Introspection and serialization

<?php

class Test {

public $a = 1;

public $b = 2;

$obj = new Test();

print_r(get_object_vars($obj));

$ser = serialize($obj);

echo $ser;

?>
Q13. Registration form with textbox, radio, checkbox, button

<form method="post">

Name: <input type="text" name="name"><br>

Gender: <input type="radio" name="gender" value="Male">Male

<input type="radio" name="gender" value="Female">Female<br>

Hobbies: <input type="checkbox" name="hobbies[]" value="Reading">Reading

<input type="checkbox" name="hobbies[]" value="Coding">Coding<br>

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

</form>

Q14. Form using list box, combo box, hidden field

<form method="post">

<select name="course">

<option>PHP</option>

<option>Java</option>

</select>

<input type="hidden" name="id" value="101">

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

</form>

Q15. Form with validation

<form method="post">

Name: <input type="text" name="name"><br>

<input type="submit" name="submit">

</form>

<?php

if(isset($_POST['submit'])){

if(empty($_POST['name'])){
echo "Name is required.";

} else {

echo "Hello " . $_POST['name'];

?>

Q16. Create, modify, delete cookies

<?php

setcookie("user", "Bhakti", time()+3600);

$_COOKIE["user"] = "Updated Bhakti";

setcookie("user", "", time()-3600);

?>

Q17. Create and destroy session

<?php

session_start();

$_SESSION["user"] = "Bhakti";

session_destroy();

?>

Q18. Send and receive mail using PHP

<?php

mail("[email protected]","Test Subject","Hello from PHP!");

?>

Q19. Retrieve and present data from database

<?php

$conn = mysqli_connect("localhost","root","","test");

$result = mysqli_query($conn,"SELECT * FROM students");


while($row = mysqli_fetch_assoc($result)) {

echo $row["name"];

?>

Q20. Insert data into employee table

<?php

$conn = mysqli_connect("localhost","root","","test");

mysqli_query($conn,"INSERT INTO employee (name) VALUES ('Bhakti')");

?>

Q21. Update table data

<?php

$conn = mysqli_connect("localhost","root","","test");

mysqli_query($conn,"UPDATE students SET name='Bhakti' WHERE id=1");

?>

Q22. Delete table data

<?php

$conn = mysqli_connect("localhost","root","","test");

mysqli_query($conn,"DELETE FROM students WHERE id=1");

?>

Q23. Anonymous function

<?php

$greet = function($name) {

return "Hello $name";

};

echo $greet("Bhakti");

?>
Q24. Multidimensional array

<?php

$marks = array(

"Bhakti" => array("Math" => 90, "Science" => 85),

"Amit" => array("Math" => 80, "Science" => 70)

);

print_r($marks);

?>

Q25. Recursive factorial

<?php

function factorial($n) {

if($n <= 1) return 1;

return $n * factorial($n - 1);

echo factorial(5);

?>

You might also like