Web Technology · Unit 6 · 8 hrs
Server Side Scripting using PHP
Exam-focused notes for Server Side Scripting using PHP (Web Technology, CSC329): what the TU syllabus asks and how it has actually been tested, with 16 solved past questions from this unit.
What this unit covers
- PHP Syntax
- Variables
- Data Types
- Strings
- Constants
- Operators
- Control structure
- Functions
- Array
- Creating Class and Objects
- PHP Forms
- Accessing Form Elements
- Form Validation
- Events
- Cookies and Sessions
- Working with PHP and MySQL
- Connecting to Database
- Creating, Selecting, Deleting, Updating Records in a table
- Inserting Multiple Data
- Introduction to CodeIgniter, Laravel, Wordpress etc.
Creating Class and Objects
Write a PHP program to demonstrate the concept of inheritance. [5]
Inheritance is an OOP concept where a child class (derived class) acquires the properties and methods of a parent class (base class) using the extends keyword. This promotes code reusability. --- --- --- Concept Description ------ extends Used to inherit pa...
Full solved answer →How can you define class and objects in PHP? Write a PHP script to create class and its object. [5]
A class is a "blueprint" for an object, used as a code template to generate objects. It contains the code for properties (variables) and methods (functions). A class is defined using the class keyword, followed by the class name and a pair of curly braces {...
Full solved answer →Inserting Multiple Data
Write a PHP program to show insertion of multiple data into a database table. [5]
The program follows the standard MySQLi procedural approach as described in the curriculum notes: 1. Establish a database connection using mysqliconnect() 2. Select the target database 3. Insert multiple rows using multiple INSERT SQL queries (or a single m...
Full solved answer →Form Validation
Create a PHP form with form elements. Write a program to perform form validation. Use your own assumptions for the validation. [5]
- Username: required, minimum 5 characters - Password: required, must start with a digit and end with $ - Gender (radio button): must be selected - Hobbies (checkbox): at least one must be checked --- --- Element Validation Rule ------ username Required, le...
Full solved answer →PHP Forms
Write a PHP program to create a form. Create an insertdb() method to insert the form values into a database table and display() to display the database table value.[10]
--- Before running the program, create the table in MySQL: --- Part Description ------ mysqliconnect() Creates connection to MySQL using procedural approach insertdb($conn, ...) Takes form values as parameters and runs INSERT query display($conn) Runs SELEC...
Full solved answer →Functions
Write a PHP function add that takes two arguments as an input and return their sum. [5]
Based on the PHP Functions and Function Arguments concepts A PHP function is declared using the function keyword, and arguments are passed inside the parentheses separated by commas. --- --- --- Part Description ------ function add(...) Declares a user-defi...
Full solved answer →Write a PHP program that includes a function that will take two strings as input argument and print concatenation of two strings. [5]
A user-defined function declaration starts with the word function. Arguments are specified after the function name, inside the parentheses, and as many arguments as needed can be added, separated by commas. --- --- --- Step Description ------------------- f...
Full solved answer →How can you define function in PHP? Create a function that will take two integers as argument and will return product of them. [5]
A function is a block of statements that can be used repeatedly in a program. A function will not execute immediately when a page loads; it will be executed only when it is called. Besides built-in PHP functions, we can create our own user-defined functions...
Full solved answer →Cookies and Sessions
Define cookie. Write a PHP program to create and retrieve a cookie. [5]
A cookie is a variable that is stored on the visitor's computer. Each time the same computer requests a page with a browser, it will send the cookie too. Cookies are used to identify users and store information between page requests. Examples of cookies inc...
Full solved answer →How can you handle session in PHP? [5]
A session is a way to store information (in variables) to be used across multiple pages. Unlike cookies, session data is stored on the server side. Each user gets a unique session ID which is used to identify them across requests. --- Before using session v...
Full solved answer →Working with PHP and MySQL
Write a server side script in PHP to illustrate inserting and selecting values to and from database table. Create required connection using your own assumptions.[10]
- Server: localhost - Username: root - Password: (empty) - Database: webTech - Table: students with columns id, name, email, phone --- --- Step Description ------------------- 1 Set server credentials (localhost, root, empty password, webTech DB) 2 Use mysq...
Full solved answer →Create web form for book search catalog. The form should contain a dropdown defining search type, a text box for search keyword, a radio button for download type true or false, now write PHP script to store data from the form into database table and also retrive the results from stored table in a new page.[10]
This solution has three files: - form.html - The search form - store.php - Stores form data into the database - retrieve.php - Retrieves and displays stored results --- --- --- --- --- Step File Action -------------------- 1 form.html User selects a search ...
Full solved answer →Connecting to Database
How database connection is created in PHP? Write a PHP program to create a form and insert values in to the database table. The form should contain at least two input fields.[10]
There are three ways of working with MySQL and PHP: 1. MySQLi (object-oriented) 2. MySQLi (procedural) 3. PDO The following three lines are always written when using localhost: Note: mysqliconnect() takes server name, username, password, and optionally the ...
Full solved answer →Array
How can you define array in PHP? Write a PHP function to create an array of type integer and print its elements. [5]
An array is a data structure that stores multiple values in a single variable. In PHP, an array can hold values of different data types (string, integer, float, etc.) under one variable name, and each element is accessed using an index (starting from 0). Sy...
Full solved answer →How do you define an Array in PHP? write a PHP script to create a multidimensional array name Product that will contain pcode, pname, and price. Initialize the array with at least three instances. Also, Write an HTML script to display the initialized values of the array in an HTML Table.[10]
As stated in the course notes: "An array is a special variable, which can hold more than one value at a time. An array can hold many values under a single name, and we can access the values by referring to an index number." --- According to the notes, PHP s...
Full solved answer →Variables
How can you define variables in PHP? Define any two variable of string types and display their results after concatenation. [5]
In PHP, a variable starts with the $ sign, followed by the name of the variable. Variables are like containers for storing data. - A variable starts with the $ sign, followed by the name of the variable. - A variable name must start with a letter or the und...
Full solved answer →Make Unit 6 stick
Practice CSC329 with flashcards & quizzes