Q.no 11
A function is a block of code that performs a particular task. Functions allow us to break a complex task into simpler and more manageable parts, making our code easier to read, maintain, and debug.
To declare a function, you must use the keyword “function“ followed by the name of the function, followed by parentheses (). To call a function, you must use the function name followed by parentheses ().
For example, let‘s say we want to create a function called sayHello that prints “Hello!” to the console. The function declaration and call would look like this:
1 2 3 4 5 6 7 |
//Declare the function function sayHello() { console.log("Hello!"); }; //Call the function sayHello(); |
Q.no 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
?php $sourcefile = 'source.txt'; $destfile = 'destination.txt'; $read_handle = fopen($sourcefile, 'r'); $write_handle = fopen($destfile, 'w'); $line_number = 0; if ($read_handle && $write_handle) { while (($line = fgets($read_handle)) !== false) { $line_number++; if ($line_number % 2 == 1) { fwrite($write_handle, $line); } } fclose($read_handle); fclose($write_handle); } else { // error opening the file. } ?> |
Q.No 14Does PHP have data type? why php isa called loosely typed lanagauge?
Yes, PHP has a variety of data types, including strings, integers, floats, booleans, arrays, and objects.
PHP is called a loosely typed language because it does not require explicitly declaring variable types before they can be used. This means that developers can assign any type of data to a variable and change it later on, allowing for greater flexibility in coding. Additionally, PHP automatically converts types when needed, meaning developers do not have to worry about explicit type conversion when using variables.
Q.no 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<html> <head> <title> Login page </title> </head> <body> <form action="process.php" method="post"> <h2>Login Form</h2> <div> <label for="username">Username:</label> <input type="text" name="username" id="username" /> </div> <div> <label for="password">Password:</label> <input type="password" name="password" id="password" /> </div> <div> <input type="submit" name="submit" value="Login" /> </div> </form> </body> </html> <?php session_start(); if(isset($_POST['submit'])){ $user = $_POST['username']; $pass = $_POST['password']; if($user == "username" && $pass == "password") { $_SESSION['username'] = $user; $_SESSION['start'] = time(); // Taking now logged in time. // Ending a session in 5 hours from the starting time. $_SESSION['expire'] = $_SESSION['start'] + (5 * 60 * 60); echo "Login Successful"; } else { echo "Username or Password is invalid"; } } ?> |
Group c
Q.no 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<form> <label>Date Of Birth:</label> <input type="date" name="dob" /> <input type="submit" value="Calculate Age in Days" /> </form> <?php if(isset($_GET['dob'])){ $dob = $_GET['dob']; $date1 = new DateTime($dob); $date2 = new DateTime(); $diff = $date2->diff($date1); $age_in_days = $diff->days; echo "Your age in days is: $age_in_days"; } ?> |
Q.no 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT name FROM plane WHERE source='Kathmandu' AND destination='Bharatpur' AND date='2022-12-25'"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "Name: " . $row["name"]. "<br>"; } } else { echo "0 results"; } $conn->close(); ?> |