PHP Pre Board solution

4th semester

Asian School of Management and Technology

 
Group A
Write some words about php?
PHP: Hypertext preprocessor is a widely used open source general purpose scripting language that is especially suited for web development and can be embedded into HTML.
How can you differentiate static and dynamic website?

Static Website Dynamic Website
Content is fixed and each page is coded in plain HTML and CSS. Content is dynamic which changes according to user’s preferences.
Display the same design and content to every user who visits the website. Offers a unique mix of dynamic contents, multimedia elements and interaction features.
More control and flexibility as each page design is unique. User has complete control over updating the design and changing the content.

What is the uses of foreach in php?
The foreach loop is mainly used for looping through the values of an array. It loops over the array and each value for the content array element is assigned to $value and the array pointer is advanced by one to go the next element is the array.
Syntax:

What is the uses of strops() function.
The strops() function finds the position of the first occurrence of a string inside another string.
Example:

What does mysqli_fetch_array() does?
This function is used to fetch the mysql result into a numeric array one row at a time. It returns a numeric array if the data found otherwise it returns null.
Syntax:

Differentiate between include(), include_once(), require() and require_once() ?
The include() statement includes and evaluates a specified line ie. It will include a file based in the given path.
The include_once statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include statement, with the only difference being that if the code from a file has already been included, it will not be included again, and include_once returns TRUE.
Require() does the same thing except upon failure it will generate a fatal error and halt the script whereas include() will just give a warning and allow script to continue.
Require_once() will check if the file already has been included and if so it will not include the file again.
Define cookie in php.
A cookie is often used to identify a user. A cookie is small file that the server embeds on the user’s computer requests a page with the browser, it will send the cookie.
Example:

Give the condition when the variable has local scope.
A variable declared inside a function has a local scope. This variable is unique to this function and can only be accessed within the function.
Example:

Write statement to connect mysql from PDO in php.
<?php
/*syntax:
new PDO(“host; database”, username, password);
Example:

 
In the above example we assume the BIM_Study_Notes is our database name.
What do you know about the die() function in php?
The die() function is an inbuilt function in php used to print message and exit from the current php script.
Example:

Group B

Exercise Problems:

  1. Make the following html table using PHP. Store values in multi-dimensional associative array.
S_id Name Address Phone
1 Ram Paknajol 422813
2 Shyam Ason 435012
3 Hari Balaju 456921

 

 
Write the function? List the types of function. Write an example of dynamic function call.
Functions are important tools in any programming language which is define block of statement that perform particular job in a program. Function is a logical unit composed of number of statements group in a single unit. It also defines as section of a unit performing a specific job. Function is code3 snipped that accept values and produce result. Although to be supplied values to produce result. A function in a program is important because of following reasons.

  • Code reusability
  • Manageability
  • Reduce redundancy in programming
  • Logical clarity
  • Easy to divide task among programmer.

User defined Function:
User define function are just a function which are define by a programmer at the time of writing program. A function is just a segment of code that separate rest of code which can perform the similar type of task. The basic syntax for defining user defined function.
<?php

Built-in Function:
These function are already define in the application. These function are used by programmer to design the program. It makes easy to programmer in php, the built-in function are: echo(), print_r(), die(), etc.
The example of dynamic function call:

  1. Write a program to save your personal details into a file “MyFile.csv”.

  1. What is session and why it is used? Give an example of session with session variables and their values.

A session is a way to store information (in variables) to be used across multiple pages. When you work with an application, you open it, do some changes, and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn’t maintain state.
Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser.
So, Session variables hold information about one single user, and are available to all pages in one application.
A session is started with the session_start() function.
Session variables are set with the PHP global variable: $_SESSION.
Example:
Login.php

Home.php

Logout.php

  1. Write a program to read the string from form and display first and last character of string. Also validate the string for alphabet only.

form.html

string.php

Group C

Comprehensive Answer Question:

  1. Write a PHP code for displaying a simple calendar of current months and year.

 

  1. How can you insert form data safely into database? Write an example.

We can insert form data safely into database by using POST method.
form.php

insert.php

NCC

  1. What are the basic rules of php?

Every piece of PHP code begins with “<?php”, The way to signify that the PHP code is finished is by adding “?>” at the end.A variable starts with the $ sign, followed by the name of the variable.

  1. What are strpos and strcmp function?

Strops-It is used to find the position of first occurrence of a string inside another string.
Strcmp- It compares two string and returns:
0 – if the two strings are equal
<0 – if string1 is less than string2
>0 – if string1 is greater than string2
 

  1. Explain include and require

If include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function.
The Require () function is also used to put data of one PHP file to another PHP file. If there are any errors then then require () function produces a warning and a fatal error and stops the execution of the script i.e. the script will continue to execute.

  1. Explain array padding with example

Array padding is used to padding (insert) a specified number of elements, with a specified value, into an array. If the specified value is positive then the array is padded on the right, if it is negative then padded on the left.

  1. Explain mysqli_select_db

Change the default database for the connection.

  1. Difference between mysqli_fetch_assoc and mysqli_fetch_array

mysqli_fetch_assoc: Fetch a result row as an associative array.
mysqli_fetch_array: Fetch a result row as a numeric array and as an associative array.

  1. What is a cookie?

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

  1. Explain a, x+ mode in file.

Mode (a) – Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist.
Mode (x+) – Creates a new file for read/write. Returns FALSE and an error if file already exists.

  1. How can you get current date and time in PHP?

  1. Explain SERVER_ADDR and REMOVE_ADDR

SERVER_ADDR is the address of the server PHP code is run on. You don’t need to collect it. REMOTE_ADDR is the one you want. The $_SERVER[‘REMOTE_ADDR’] returns the IP address from which the user is viewing the current page.

  1. Swap two numbers without using third variable.

Create a form that accepts name, email, phone number and a radio button for gender. Validate required fields and the name must be 6 letters long.

Display information of database and database table in table.

 
 

  1. Write the array created in “details.csv”

  1. What does mktime do ? Display current date using switch statement.

The mktime() function is an inbuilt function in PHP which is used to return the Unix timestamp for a date.

 
16.
An array is a variable which can hold multiple value of different data type at a time
Example [“Kathmandu”,25,”Nepal”].                                                                                                                                                   An string is a collection of character like “WEB TECHNOLOGY”.

Output:-
Array
(
[College] => NCC
[Course Code] => IT 219
[Course Name] => Web tech-II
[Exam] => preboard
[Faculty] => BIM
[Semester] => fourth
)
Array
(
[Exam] => preboard
[Semester] => fourth
[Course Name] => Web tech-II
[College] => NCC
[Course Code] => IT 219
[Faculty] => BIM
)
17.
Session Variables are used because it is the easy and simple way to store the value in variable across the multiple pages.

 

St Xavier’s College

Group A

  • What is a cookies?

Ans: A cookies is a data which is stored in the browser (client side). When a user request to the server it will be delivered along with request. It is used to recognize the user.

  • What is cross site scripting?

Ans: A cross site scripting is a class of security vulnerability where an attacker manages to use a website and tends to deliver potentially malicious Javascript payload to the end user.

  • How validation for Integer and Float is done in PHP?

Ans: Since every input received in a php are in a string form. It must be converted to its original data type. For integer type intval() function and for float type floatval() function is used for validation.

  • What is the use of global keyword and $GLOBALS array?

Ans: Both are used to access global variable.
We can access global variable by using global keyword inside the function.
Eg.
Function sum() {
Global $add;
}
If we use $GLOBALS array then we can directly use it inside the function in $GLOBALS[varname] form.
Eg.
foreach($GLOBALS[‘hobbies’] as $hob)

  • Differentiate between include and require?

Ans:

Include Require
When an error occurs, include will only produce warning and the script will continue. When an error occurs, require will produce fatal error and the script will stop executing.
Syntax:
Include ‘filename’;
Syntax:
Require ‘filename’;

 What is $_SERVER variable?
Ans: $_SERVER variable is a super global variable which holds the information about header, path and script location.
Eg.
$_SERVER[PHP_SELF] : returns the filename of currently executing script.

  • What is epoch time stamp?

Ans: Epoch time stamp is a number of second elapsed since 1 jan 1970 midnight.

  • What does session_start() do?

Ans: Session_start() function is used to start the session. It must be at very beginning. It also helps to declear session variable.

  • What are $_GET and $_POST variables?

Ans: $_GET variable is a super global variable to access the value of a particular field of form and sends it using GET method.
$_POST variable is a super global variable to access the value of a particular field of form and sends it using POST method.

  • How can you accomplish automatic error handling using PEAR in PHP?

An: We can accomplish automatic error handling using setErrorHandling() function and passing PEAR_ERROR_DIE constant.
 
Group B

  • Write a program to manipulate multidimensional associative array in PHP?

Ans: An array within an array is known as multidimensional array. Let us assume we have a districts of Kathmandu valley. (i.e) Kathmandu, Lalitpur and Bhaktapur. Suppose the major places of Kathmandu are New Road, Darbarmarg and Baudha. Similarly Lalitpur has Patan, Jawalakhel and Sanepa and Bhaktapur has Darbar Square and Suryabinayak.

  • Explain the manipulation of array in a double quoted string with proper example.

Ans: i) If key doesn’t contain other thing than letter, number, and underscore don’t wrap key with quote.
ii) If key contains other character than number, letter and underscore, wrap elements inside array and give quotes.

Assume a database and table of your choice. Design a login form and validate the login form the database. On successful login create login session for the user.

  • Place a checkbox above submit button of Q.no 13 indicating remember me. Set a cookie if the checkbox is checked nad facilitate auto login from the next time.

(Solution of 13 and 14 together.)
Ans:
Index.php

 
Valid.php

//1. Connect to DB server and choose DB

 
/2) Perform SQL query

  • Mention all the parameters for set_cookie(__)function.

Ans: Cookies are the data that are stored in the browser. When a user request to the server it will be delivered along with request. Cookies is mostly used to recognize the user.
There are three parameter that goes inside set_cookie() function.
Field_name
In this parameter it determines where the cookie is to be set. Mostly in the forms we can see in username and password in which cookies are set.
Cookie value
This parameter determines what value is to be set in cookie.
Time
Time determines the life span of a cookie. To determine the time we use time() function.

Group C

Write a program to set default values and validation of a form consisting of following fields.

  1. Full Name
  2. Username
  3. Password
  4. Email
  5. Faculty (dropdown)
  6. Subject (multiselect dropdown)
  7. Hobbies (checkbox)

 
Upon successful validation insert data into a database table. Assume database name and table as per your convenience.
Ans:

 
//1. connect to database server and choose database