Group A
1. Register_global variablesis an internal PHP setting which registers the $_REQUEST array’s elements as variables. If you submit a value in a form, via POST or GET, the value of that input will automatically be accessible via variable in the PHP script, named after the name of the input field.
2. To create a constant, weuse the define() function.
Syntax :- define(name, value)
Example:-
1 2 3 4 5 6 7 8 9 10 |
<?php define("GREETING", "Welcome to BIM-Notes.com!"); echo GREETING; ?> 3. The substr() function returns a part of a string. Example:- <?php // Positive numbers: echo substr("Hello Everyone",10)."<br>"; ?> |
4. A Web server is a program that uses HTTP (Hypertext Transfer Protocol) to serve the files that form Web pages to users, in response to their requests, which are forwarded by their computers’ HTTP clients. Dedicated computers and appliances may be referred to as Web servers as well.
5. The trim() function removes whitespace and other predefined characters from both sides of a string.
Example:-
1 2 3 4 5 6 |
<?php $str = " Hello World! "; echo "Without trim: " . $str; echo "<br>"; echo "With trim: " . trim($str); ?> |
Output:-
Without trim: Hello World!
With trim: Hello World!
6. A uniform resource locator (URL) is the address of a resource on the Internet. A URL indicates the location of a resource as well as the protocol used to access it.
7. Mysql_fetch_row fetch result row an numeric way. This function return a row where the valuew will come in the order as they are defined in the query, and the keys will span from 0 to one less than the number of columns selected
Mysql_fetch_assoc() fetch a result row as an associative array.This function will return a row as an associative array where the column names will be the keys storing corresponding value.
8.
1 2 3 |
<?php $mysqli = new mysqli("localhost", "username", "password", "dbname"); ?> |
9. The PHP date() function is used to format a date and/or a time.
Example:-
1 2 3 |
<?php echo "Today is " . date("Y.m.d") . "<br>"; ?> |
10. A CSV (Comma Separated Values) file is a text based format that represents the data typically found in a spreadsheet or a database table.
Advantages of CSV :-
- CSV files can be opened or edited by text editors like notepad.
- In data-warehouse, CSV follows a fairly flat, simple schema.
- Any programming language to parse CSV data is trivial, generating it is extremely easy.
- Importing CSV files can be much faster, and it also consumes less memory.
Group B
11. Write PHP code to perform server side validation. (check for empty, alphabets, numbers, email and password length validation)
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 41 42 43 44 |
<?php if(isset($_POST['submit'])){ if(isset($_POST['name']) && !empty($_POST['name']) && trim($_POST['name']) != ''){ $name = $_POST['name']; $regex = '/^[a-zA-Z ]*$/'; if(!preg_match($regex, $name)){ echo 'Only letters and space are allowed'; }else{ echo 'Name: '.$name; } }else{ echo 'Enter name'; } if(isset($_POST['phone']) && !empty($_POST['phone']) && trim($_POST['phone']) != ''){ $phone = $_POST['phone']; $regex = '/^([0-9]{10})$/'; if(!preg_match($regex, $phone)){ echo '<br ?I invalid phone pattern'; }else{ echo ' Phone: '.$phone; } }else{ echo ' Enter name'; } if(isset($_POST['email']) && !empty($_POST['email']) && trim($_POST['email']) != ''){ $email = $_POST['email']; $regex = '/^([a-zA-Z0-9\.\-\_])+\@(([a-zA-Z0-9\.\-])+\.)+([a-zA-Z0-9]{2,4})+$/'; if(!preg_match($regex, $email)){ echo ' Invalid email pattern'; }else{ echo ' Email: '.$email; } }else{ echo ' Enter email'; } if(isset($_POST['password']) && !empty($_POST['password']) && trim($_POST['password']) != ''){ if(strlen($_POST['password']) < 6){ echo ' Password must be greater than 6 character'; }else{ echo ' Password: '.$password = $_POST['password']; } }else{ echo ' Enter password'; } } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<body> <form action="" method="post"> <label>Name</label> <input type="text" name="name" /> <br /> <label>Phone</label> <input type="text" name="phone" /> <br /> <label>Email</label> <input type="text" name="email" /> <br /> <label>Password</label> <input type="text" name="password" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> |
12. Write a function to calculate sum of all even numbers that occurs between given two integers passed as parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php function even_sum($first_num, $second_num) { $sum = 0; for($i = $first_num; $i <= $second_num; $i++){ //inclusive if($i % 2 == 0){ $sum += $i; } } return $sum; } $total_sum = even_sum(10, 20); echo 'Total sum: '.$total_sum; ?> |
13. Create an array that contains your name, address, age, gender. Then write the array to the file name “profile.csv”.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $file = fopen('profile.csv', 'w') or die('Cannot open file'); $title = array('Name', 'Address', 'Age', 'Gender'); if(fputcsv($file, $title)){ echo 'Title written to the file'; } esle{ echo 'Title cannot written to the file'; } $student = array('name' => 'Prakash', 'address' => 'Bhaktapur', 'age' => 20, 'gender' => 'Male'); if(fputcsv($file, $student)){ echo 'Contents written to the file'; }else{ echo 'Contents cannot written to the file'; } fclose($file); |
14. Create a login system, making your own assumptions about database, authorize the login credentials.
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 $conn = mysqli_connect('localhost', 'root', '', 'registration'); $err = array(); if(isset($_POST['submit'])){ if(isset($_POST['username']) && !empty($_POST['username']) && trim($_POST['username']) != ''){ $username = mysqli_real_escape_string($conn, $_POST['username']); }else{ $err['username'] = 'Username is required'; } if(isset($_POST['password']) && !empty($_POST['password'])){ $password = mysqli_real_escape_string($conn, $_POST['password']); }else{ $err['password'] = 'Password is required'; } if(count($err) == 0){ $password = md5($password); //encrypt password before storing database $sql = "SELECT * FROM student WHERE username = '$username' AND password = '$password'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) == 1) { header('location: welcome.php'); }else{ $err['error'] = 'Wrong username/password combination'; } } } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<body> <form action="" method="post"> <?php if(isset($err['error'])){ echo $err['error']; } ?> <br /> <label>Username</label> <input type="text" name="username" /> <?php if(isset($err['username'])){ echo $err['username']; } ?> <br /> <label>Password</label> <input type="text" name="password" /> <?php if(isset($err['password'])){ echo $err['password']; } ?> <br /> <input type="submit" name="submit" value="Submit" /> </form> <p>Username: Prakash Adhikari</p> <p>Password: prakash</p> </body> |
15. Write a PHP function to calculate the difference between two dates in PHP.
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 |
<?php if (isset($_POST['submit'])) { if (isset($_POST['date1']) && !empty($_POST['date1']) && trim($_POST['date1']) != '') { $date1 = $_POST['date1']; }else{ echo 'Enter first date'; } if (isset($_POST['date2']) && !empty($_POST['date2']) && trim($_POST['date2']) != '') { $date2 = $_POST['date2']; }else{ echo 'Enter second date'; } if(isset($date1) && isset($date2)){ $d1 = strtotime($date1); $d2 = strtotime($date2); function diff_date($date1, $date2){ $diff = abs($date1 - $date2); $days = intval($diff / (24 * 60 * 60)); $years = intval($days / 365); $days = $days % 365; $months = intval($days / 30); $days= $days % 30; echo 'Difference between two dates is: '.$years.' years '.$months.' months '.$days.' days'; } diff_date($d1, $d2); } } ?> <body> <form action="" method="post"> <label>Date1</label> <input type="text" name="date1" placeholder="yyyy/mm/dd" /> <br /> <label>Date2</label> <input type="text" name="date2" placeholder="yyyy/mm/dd" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> |