(XII) PHP lab works for 2022/23

Steps to write php code and see Output 1. Open XAMPP 2. Start Apache and MySQL 3. Open text editors like Sublime , Notepad++ etc 4. Save -> Local disk(c) -> XAMPP -> htdocs -> filename.php 5. Type the code and save it

6. Click web browser like chrome type localhost/filename.php 7. Press Enter key

1. WAP to find sum of two numbers. Here, numbers are entered via html form.

<h1> PHP program to find sum of two numbers</h1> <form method="post"> First Number : <input type="text" name="firstnum"><br><br> Second Number : <input type="text" name="secondnum"><br><br> <input type="submit" name="submit" value="sum"> </form> <?php if(isset($_POST['submit'])) { $n1=$_POST['firstnum']; $n2=$_POST['secondnum']; $s=$n1+$n2; echo "Sum of two numbers is ".$s; } ?>

2. Write a PHP program to swap two numbers.

<h2>PHP program to swap two numbers</h2> <form method="post"> First Number : <input type="text" name="num1"><br><br> Second Number : <input type="text" name="num2"><br><br> <input type="submit" name="submit" value="submit"> </form> <?php if(isset($_POST['submit'])) { $a=$_POST['num1']; $b=$_POST['num2']; echo " Before swapping "; echo " <br>First Number ".$a; echo " <br> Second Number ".$b; $temp=$a; $a=$b; $b=$temp; echo " <br>After swapping "; echo "<br>First Number ".$a; echo "<br> Second Number ".$b; } ?>

3. Write a PHP program to input name and address using form and display them.

<h2> PHP program to enter name and address </h2> <form method="post"> Enter Name: <input type="text" name="name"><br><br> Enter Address : <input type="text" name="address"><br><br> <input type="submit" name="submit" value="Submit"> </form> <?php if(isset($_POST['submit'])){ $n=$_POST['name']; $a=$_POST['address']; echo "<br> Name = ".$n; echo "<br> Address = " .$a; } ?>

4. Write a PHP program to find the greatest number among three numbers. Here, numbers are entered via html form.

<h2> PHP program to find greatest number among three numbers</h2> <form method="post"> First Number : <input type="text" name="num1"> <br><br> Second Number: <input type="text" name="num2"> <br><br> Third Number: <input type="text" name="num3"> <br><br> <input type="submit" name="submit"> </form> <?php if(isset($_POST['submit'])) { $n1=$_POST['num1']; $n2=$_POST['num2']; $n3=$_POST['num3']; if($n1>$n2&&$n1>$n3) echo $n1; else if($n2>$n1&&$n2>$n3) echo $n2; else echo $n3; } ?>

5. Write a PHP program to input any number via form and check whether the number is positive or negative and be sure number should not be zero.

<h2> PHP program to input any number via form and check whether the number is positive or negative and be sure number should not be zero</h2> <form method="post"> Enter number: <input type="text" name="num"><br><br> <input type="submit" name="submit" value="Submit"> </form> <?php if(isset($_POST['submit'])){ $n=$_POST['num']; if($n==0) echo '<script>alert("Enter non zero number")</script>'; else if($n>0) echo "Positive number"; else echo "Negative number"; } ?>

6. Write a PHP program to display your name 100 times.

<?php $name = "Alex Lal Karn"; for($i=0;$i<100;$i++) { echo $name; echo "<br>"; } ?>

7. Write a PHP program to print a multiplication table of a number. Input the number via form.

<form method="post"> Enter Number: <input type="text" name="num"> <br><br> <input type="submit" name="submit"> </form> <?php if(isset($_POST['submit'])) { $n=$_POST['num']; for($i=1;$i<=10;$i++) { echo $n*$i; echo '<br>'; } } ?>

8. Write a PHP program to display the factorial value of a number.

<form method="post"> Enter number : <input type="text" name="num"><br><br> <input type="submit" name="submit"> </form> <?php if(isset($_POST['num'])) { $n=$_POST['num']; $f=1; for($i=1;$i<=$n;$i++){ $f=$f*$i; } echo "Factorial = ".$f; } ?>

9. Write a PHP to print factors of a number.

<form method="post"> Enter number : <input type="text" name="num"><br><br> <input type="submit" name="submit"> </form> <?php if(isset($_POST['num'])) { $n=$_POST['num']; $f=1; for($i=1;$i<=$n;$i++) { if($n%$i==0) { echo $i; echo "<br>"; } } } ?>

10. Write a PHP program to store some strings in an array and print them.

<?php $subjects = array("Compulsory English", "Compulsory Nepali", "Social","Accounts","Economics","Computer"); for ($i=0;$i<count($subjects);$i++) { echo $subjects[$i]; echo "<br>"; } ?>

11. Write a PHP program to sort data stored in an array. [Use sort() function and print_r() to print.]

<?php $numbers = array(2,0,4,5); sort($numbers); print_r($numbers); ?>

12. Write a PHP program to know whether two strings are the same or not.

<?php $str1 = "Alex Lal Karn"; $str2 = "Computer Science"; if(strcmp($str1, $str2) == 0){ echo "Strings are same"; }else{ echo "Strings are not same"; } ?>

13. Write a PHP program to input a string through a text box and find its length.[use strlen()]

<form method="post"> <input type="text" name="userText" placeholder="Enter text"> <input type="submit" value="Find Length"> </form> <?php if(isset($_POST['userText'])) { $str=$_POST["userText"]; echo "Length=".strlen($str); } ?>

14. Write a PHP program to connect a database named ‘student’.

<?php $servername="localhost"; $username="root"; $password=""; $dbname="student"; $conn=new mysqli($servername,$username,$password,$dbname); if($conn->connect_error) die("Connection failed:".mysqli_connect_error()); else echo "Database connected successfully"; ?>

15. Write a PHP program to insert records in a database named student with fields id, name and grade.

<?php $servername="localhost"; $username="root"; $password=""; $dbname="student"; $conn=new mysqli($servername,$username,$password,$dbname); if($conn->connect_error) die("Connection failed:".mysqli_connect_error()); else echo "Database connected successfully"; $sql="insert into studentrec (id,name,grade)values (101,'Tsering Sherpa',12)"; if($conn->query($sql)===true) { echo "Inserted data successfully"; } else{ echo "Error in inserting data:".$conn->error; } $conn->close(); ?>

16. Write a PHP program to display all records (previous question) of students who are in grade 11.

<?php $servername="localhost"; $username="root"; $password=""; $dbname="student"; $conn=new mysqli($servername,$username,$password,$dbname); if($conn->connect_error){ die("Connection failed:".mysqli_connect_error()); } $sql="SELECT *FROM studentrec WHERE grade='11'"; $result=$conn->query($sql); if($result->num_rows>0) { while($row=$result->fetch_assoc()) { echo "<br> id:".$row["id"]." - Name: ".$row["name"]." - Grade : ".$row["grade"]; } } else { echo "0 results"; } $conn->close(); ?>

17. Write a PHP program to delete records of a student whose id is 3.[previous question]

<?php $servername="localhost"; $username="root"; $password=""; $dbname="student"; $conn=new mysqli($servername,$username,$password,$dbname); if($conn->connect_error) die("Connection failed:".mysqli_connect_error()); $sql="DELETE FROM studentrec WHERE id=3"; if($conn->query($sql)===true) { echo "Data deleted "; } else{ echo "Error in deleting data:".$conn->error; } $conn->close(); ?>

18. Write a PHP program to update a record of student whose id is 2.[Try same program with name/grade.]

<?php $servername="localhost"; $username="root"; $password=""; $dbname="student"; $conn=new mysqli($servername,$username,$password,$dbname); if($conn->connect_error) die("Connection failed:".mysqli_connect_error()); else echo "Database connected successfully"; $sql="UPDATE studentrec SET name='ABC' WHERE id=2;"; if($conn->query($sql)===true) { echo "Data updated successfully"; } else{ echo "Error in data update :".$conn->error; } $conn->close(); ?>

Case Study

1. Download a webpage template 2. It contains some navigations menu like Home, About, Gallery, Register and view records. Here view records contain update and delete buttons. Click for reference answer