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

PHP Programming Examples and Functions

The document contains a series of PHP programming exercises covering various topics such as conditional statements, loops, functions, arrays, object-oriented programming, form handling, cookies, sessions, email sending, and database operations. Each exercise includes sample code demonstrating the required functionality. The document serves as a comprehensive guide for learning PHP programming concepts.

Uploaded by

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

PHP Programming Examples and Functions

The document contains a series of PHP programming exercises covering various topics such as conditional statements, loops, functions, arrays, object-oriented programming, form handling, cookies, sessions, email sending, and database operations. Each exercise includes sample code demonstrating the required functionality. The document serves as a comprehensive guide for learning PHP programming concepts.

Uploaded by

folekiv959
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link] a program to check no. is positive or negative.

<?php

$a=12;

If($a>0){

Echo “Number is Positive”;

}else{

Echo “Number is Negative”;

?>

[Link] a program to find greatest amongst three numbers.

<?php

$a=10;

$b=57;

$c=11;

If ($a>$b && $a>$c){

Echo “a is greatst number”;

}elseif($b>$a && $b>$c){

Echo “b is greatest number”;

}else{

Echo “c is greatest number”;

?>

[Link] a program to print first 30 even numbers using while loop.

<?php

$a=0;
While($a<=30){

Echo $a,” By While loop<br>”;

$a+=2;

?>

[Link] a program using associate array.

<?php

$a=array(“t1”=>10,”t2”=>20,”t3”=>30);

Print_r($a);

Echo “<br>”,$a[“t1”];

?>

Q5. Write a program to calculate length of string.

<?php

$a=”Yash Gavali”;

Echo “Length of string: “,strlen($a);

?>

Q6 . Write a program to count no. of words in string without using string


function.

<?php

$a=”Wlcome to SIT Poly”;

$b=explode(“ “,$a);

$count=0;

Foreach ($b as $i){

$count+=1;

Echo “Number of words: “,$count;

?>
Q7. Write a program to demonstrate parameterized function.

<?php

Function add($a,$b){

Echo “Addition: “,$a+$b;

Function sub($a,$b){

Echo “<br>Substraction: “,$a-$b;

Add(10,30);

Sub(30,12)

?>

Q8. Write a program to create PDF document by using graphics concept.

<?php

Include(“E:\pdf186\[Link]”);

$pdf=new FPDF();

$pdf->Addpage();

$pdf->SetFont(“Arial”,’B’,20);

$pdf->cell(190,10,”Yash Gavali”);

$pdf->Output();

?>

Q9. Write a program to demonstrate multiple inheritance.

<?php

Trait a{

Function traitA(){

Echo “Method of trait a”;

}
}

Trait b{

Function traitB(){

Echo “<br>Method of trait b”;

Class c{

Use a , b ;

Function method(){

Echo “<br>Method of class c”;

$c=new c();

$c->traitA();

$c->traitB();

$c->method();

?>

Q10. Write a program to demonstrate multilevel inheritance.

<?php

Class a {

Function a(){

Echo “class a”;

Class b extends a{
Function b(){

Echo “<br>Class b inherite from a”;

Class c extends b{

Function c(){

Echo “<br>Class c inherite from b”;

$c=new c();

$c->a();

$c->b();

$c->c();

?>

Q11. Write a program to demonstrate parameterized constructor.

<?php

Class my{

Public $name;

Public $mobile;

Function __construct($name,$mobile){

$this->name=$name;

$this->mobile=$mobile;

Function dis(){
Echo “Name: “,$this->name;

Echo “<br>Mobile: “,$this->mobile;

$a=new my(“Yash”,”8830425522”);

$a->dis();

?>

Q12. Develop a program to demonstrate introspection and serialization.

<?php

$data=array(“name”=>”yash”,”age”=>17,”city”=>”sangli”);

$ser=serialize($data);

Echo “Serialized Data: “,$ser;

File_put_contents(“[Link]”,$ser)

?>

<?php

Class test{

Public $x;

Public function hello(){

Return “Hello”;

$obj=new test();

Echo “Class:”,get_class($obj),”<br>”;

Echo “Methods: “;

Print_r(get_class_methods($obj));

?>
Q13. Write a program to design a registration form using textbox, radio
button, checkbox and button.

<html>

<head>

<title>Registration Form</title>

</head>

<body>

<form>

Enter name:

<input type=”text” name=”txt1” required><br>

<input type=”radio” name=”gender” required>Male

<input type=”radio” name=”gender” required>Feamle<br>

<input type=”checkbox” name=”check” required>Agree terms and


conditions<br>

<button type=”submit”>Submit</button>

</form>

</body>

</html>

Q14. Write a program to design a form using list box, combo box and hidden
field box.

<html>

<head>

<title>Form</title>

</head>

<body>

<form>

Select Fruits:

<select name=”fruits[]” required>


<option value=”mango”>Mango</option>

<option value=”Apple”>Apple</option>

<option value=”Grapes”>Grapes</option>

<option value=”Orange”>Orange</option>

<option value=”Cherry”>Cherry</option>

</select><br><br>

Select State:

<select name=”state” required>

<option value=”Maharashtra”>Maharashtra</option>

<option value=”Goa”>Goa</option>

<option value=”Kerala”>Kerala</option>

<option value=”Manipur”>Manipur</option>

<option value=”Sikkim”>Sikkim</option>

<option value=”Nagaland”>Nagaland</option>

<option value=”Odisha”>Odisha</option>

</select><br><br>

<input type=”hidden” name=”hidden” value=”12345”><br>

<button type=”submit” >Submit</button>

</form>

</body>

</html>

Q15. Write a program to design a form using textbox, radio button, checkbox
and button. And do validation.

<html>

<head>

<title>Registration Form</title>
</head>

<body>

<form methos=”POST”>

Enter name:

<input type=”text” name=”txt1” required><br>

<input type=”radio” name=”gender” required>Male

<input type=”radio” name=”gender” required>Feamle<br>

<input type=”checkbox” name=”check” required>Agree terms and


conditions<br>

<button type=”submit”>Submit</button>

</form>

<?php

If($_SERVER[“REQUEST_METHOD”]==”POST”){

$name=$_POST[“txt1”];

$gender=$_POST[“gender”];

Echo “Name: “,$name;

Echo “Gender: “,$gender;

Echo “You can accept terms and conditions”;

?>

</body>

</html>

Q16. Write a program to create, modify and delete cookies.

<?php

Setcookie(“username”, “sai”, time() + 3600);

Setcookie(“username”, “yash”, time() + 3600);

Setcookie(“username”, “”, time() – 3600);


If (isset($_COOKIE[‘username’])) {

Echo “Cookie Value: “,$_COOKIE[‘username’];

} else {

Echo “Cookie is not set.”;

?>

Q17. Write a program to create session and destroy session.

<?php

Session_start();

$_SESSION[“username”] = “John”;

Echo “Username: “ . $_SESSION[“username”];

Session_destroy();

?>

Q18. Write a program to send and receive mail using PHP.

<?php

Use PHPMailer\PHPMailer\PHPMailer;

Use PHPMailer\PHPMailer\Exception;

Require ‘E:\PHPMailer-master/src/[Link]’;

Require ‘E:\PHPMailer-master/src/[Link]’;

Require ‘E:\PHPMailer-master/src/[Link]’;

$mail = new PHPMailer(true);

Try {

$mail->isSMTP();

$mail->Host = ‘[Link]’;

$mail->SMTPAuth = true;

$mail->Username = ‘manishagavali2018@[Link]’;
$mail->Password = ‘mwry dogh iqzp xbda’; // Use App Password

$mail->SMTPSecure = ‘tls’;

$mail->Port = 587;

$mail->setFrom(‘manishagavali2018@[Link]’, ‘Your Name’);

$mail->addAddress(‘yash21062006@[Link]’, ‘Receiver Name’);

$mail->isHTML(true);

$mail->Subject = ‘Test Email using PHPMailer’;

$mail->Body = ‘This is a <b>test email</b> sent using <i>PHPMailer


with SMTP</i>.’;

$mail->send();

Echo ‘Message has been sent successfully.’;

} catch (Exception $e) {

Echo “Message could not be sent. Error: {$mail->ErrorInfo}”;

?>

Q19. Write a program to retrieve and present data from database.

<?php

$conn = mysqli_connect(“localhost”, “root”, “”, “yash”);

If (!$conn) {

Die(“Connection failed: “ . mysqli_connect_error());

$sql = “SELECT rollno, name, address FROM student”;

If ($result=mysqli_query($conn, $sql)) {

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

Echo “Rollno.: “,$row[“rollno”],” – Name: “,$row[“name”],”Address: “,


$row[“address”],”<br>”;

}
} else {

Echo “No records found.”;

Mysqli_close($conn);

?>

Q20. Write a program to insert data into employee table.

<?php

$conn = new mysqli(“localhost”,”root”,””,”yash”);

If(!$conn){

Dia(“Error:”,$conn->error);

$sqli=”INSERT INTO employee (id,name,address,mobileno) VALUE


(121,’Yash’,’Sangli’,8830526601)”;

If ($conn->query($sqli)){

Echo “Insert data successfully”;

}else{

Echo “Faild to insert a data”;

?>

Q21. Write a program to Update table data from student database.

<?php

$conn=new mysqli(“localhost”,”root”,””,”yash”);

If(!$conn){

Dai(“Error: “,$conn->error);

$up=”UPDATE student SET name=’Ashish’ WHERE rollno=67”;

If($conn->query($up)){
Echo “Update successfully”;

}else{

Echo “faild to update a data”;

?>

Q22. Write a program to Delete table data from student database.

<?php

$conn=new mysqli(“localhost”,”root”,””,”yash”);

If(!$conn){

Dai(“Error: “,$conn->error);

$del=”DELETE FROM student WHERE rollno=67”;

If($conn->query($del)){

Echo “<br>Delete successfully”;

}else{

Echo “faild to delete a data”;

?>

Q23. Write a program to demonstrate anonymous function.

<?php

$a=function ($a,$b){

Echo “Addition: “,$a+$b;

};

$a(10,20)
?>

Q24. Develop a program using multidimensional array.

<?php

$a=array(

Array(10,20,30),

Array(40,50,60)

);

Print_r($a);

Echo “<br>”,$a[0][1];

?>

Q25. Write a recursive function to calculate factorial of a given no.

<?php

Function fact($n) {

If ($n==0 || $n==1) {

Return 1;

} else {

Return $n * fact($n – 1);

$num = 5;

Echo “Factorial :”,fact($num);

?>

You might also like