Web Programming Practical File 2024-25
Web Programming Practical File 2024-25
A function to determine if a number is prime checks divisibility from 2 up to the square root of the number. Begin by ensuring the number is greater than 1. If the number is 2, it is prime. For numbers greater than 2, loop from 2 to sqrt(num), checking if divisible by any number. If true, the number isn't prime; otherwise, it is. Example: `function isPrime($num) { if ($num <= 1) return false; for ($i=2; $i <= sqrt($num); $i++) { if ($num % $i == 0) return false; } return true; }` .
To extract URLs from a string using regex in PHP, utilize `preg_match_all()` with a pattern that matches typical URL formats. Example: `$pattern = "/\bhttps?:\/\/\S+/i"; preg_match_all($pattern, $string, $matches);`. The regex focuses on protocols (http, https) followed by any non-space sequence, effectively capturing URLs from text .
To sort an array in PHP, utilize the built-in functions `sort()` for ascending order and `rsort()` for descending order. Initially, call `sort($numbers)` to sort the array elements in ascending order. Then, use `rsort($numbers)` to re-sort the elements in descending order .
To determine if a number is even or odd in PHP using an 'if-else' statement, you can check the remainder when dividing the number by 2. If the remainder is 0, the number is even; otherwise, it is odd. Example code: `if ($number % 2 == 0) { echo "$number is even."; } else { echo "$number is odd."; }` .
Using PHP's mail() function requires specifying the recipient's address, subject, message, and headers (optional, for additional fields like 'From'). To send an email: `$to = "recipient@example.com"; $subject = "Test Email"; $message = "Hello!"; $headers = "From: sender@example.com"; if(mail($to, $subject, $message, $headers)) { echo "Email sent successfully!"; }` .
Creating a database and table in MySQL via PHP involves establishing a connection using `mysqli` constructors, then executing SQL queries to create the database and table. Begin by connecting to the server: `$conn = new mysqli($servername, $username, $password);`. Create the database: `$sql = "CREATE DATABASE students";`. Select the database and create a table: `$conn->select_db($dbname); $tableSql = "CREATE TABLE users (id INT, name VARCHAR(50), email VARCHAR(100))";`. Execute these queries with `query()` method checks .
A switch statement can map a number to a weekday by matching the number to case labels. For instance, a number between 1-7 can be associated with a weekday, like '1' for Monday and '2' for Tuesday. If no matching case exists, the default case executes, displaying an error message. Example implementation: `switch ($dayNumber) { case 1: echo "Monday"; break; ...default: echo "Invalid number! Please enter a number between 1 and 7."; }` .
To print the Fibonacci series up to 'n' terms using a for loop in PHP, initialize the first two terms, set a loop to iterate 'n' times, print each term, calculate the next term by summing the previous two, and update them: `$first = 0; $second = 1; for ($i = 0; $i < $n; $i++) { echo $first . " "; $next = $first + $second; $first = $second; $second = $next; }` .
Swapping two numbers without a temporary variable in PHP can be performed using arithmetic operations. For example: `$a = $a + $b; $b = $a - $b; $a = $a - $b;`. This method uses addition and subtraction to reassign values between two variables, effectively swapping them .
Type juggling in PHP refers to implicit type conversion during operations. PHP automatically converts a variable type to another type based on context. For example, adding a string to an integer (`$result = 10 + "5";`) converts the string to an integer, yielding 15. Similarly, PHP converts an integer to a float (`$sum = 10 + 3.5;`) or a boolean to an integer during arithmetic operations, enabling seamless calculations: integer + boolean (`$sum = 10 + true;` results in 11).