Scripting Language Exam Questions
Scripting Language Exam Questions
The MVC (Model-View-Controller) framework in PHP development serves to separate the application logic, user interface, and control flow, providing organized code that is easier to manage and maintain . The Model represents the data and business logic of the application, interacting with the database to manage data. The View is responsible for displaying data to the user, and the Controller handles user input, processes it as required by the Model, and updates the View . This separation allows developers to work on individual components concurrently and enhances the extendability and scalability of the application .
The XMLHttpRequest object enables asynchronous communication to a server, sending requests and retrieving server responses without reloading the web page, thus making applications faster and more dynamic . A simple example to display a phone number could involve creating an instance of XMLHttpRequest, defining a callback function, and configuring a 'GET' request to a URL endpoint. For example: `var xhr = new XMLHttpRequest(); xhr.open('GET', '/api/getPhoneNumber', true); xhr.onload = function() { if (xhr.status === 200) { document.getElementById('phoneNumber').innerText = xhr.responseText; } }; xhr.send();` This code snippet assumes an endpoint `/api/getPhoneNumber` which returns a phone number from the database .
The ALTER clause in SQL is utilized to make modifications to an existing database table's structure. This includes renaming, adding, or deleting columns, as well as changing column or constraint definitions . For instance, to add a new column called `email` to an existing table `users`, the SQL query would be `ALTER TABLE users ADD email VARCHAR(255);` . To modify an existing field's data type, the command could be `ALTER TABLE users MODIFY COLUMN username VARCHAR(150);` which adjusts the `username` field's data type and length . These modifications help in keeping the database schema aligned with application needs.
Arrays in JavaScript are ordered collections of data elements, typically indexed by numbers, and are used for storing list-like objects. They come with built-in methods to manipulate the elements such as push, pop, and slice . Objects, on the other hand, are collections of key-value pairs where keys can be strings or symbols, making it perfect for representing more complex entities like a person with name and age properties . A custom object can be created using a constructor function or the `Object.create()` method, defining attributes directly within the object. For example: `function Person(name, age) { this.name = name; this.age = age; } var student = new Person('John', 25);` .
Client-side scripting enhances user interaction and can provide immediate feedback and validate inputs before sending data to the server, thereby reducing server load and improving user experience . Form validation should be implemented on both the client and server sides for security and efficiency. Client-side validation with JavaScript helps catch errors early using regular expressions or HTML5 attributes like 'required', 'pattern'. A simple example is `<input type='email' required>` which checks for valid email inputs immediately . On the other hand, server-side validation is crucial for security as client-side validation can be bypassed; it checks the data again once it reaches the server to prevent malicious input . Combining both provides robust protection against erroneous and malicious inputs.
Cookies in PHP are managed using the `setcookie()` function to define a cookie and its parameters like name, value, expiration, and path, and to delete a cookie, you set its expiration date to the past . For example, setting a cookie can be done with `setcookie('user', 'John Doe', time() + (86400 * 30), '/');` to set a cookie named 'user' with the value 'John Doe' that expires in 30 days . To destroy this cookie, use `setcookie('user', '', time() - 3600, '/');` which functions by just setting a past expiry date . These cookies are stored client-side and sent back to the server with every HTTP request to track or identify returning users.
Exception handling in PHP is a mechanism to manage errors gracefully without crashing the program. It involves using 'try', 'catch', and 'finally' blocks to intercept exceptions and manage them appropriately. For example, a 'try' block executes code that might generate an exception, while 'catch' handles the exception . The four major principles of OOP are encapsulation, abstraction, inheritance, and polymorphism. Encapsulation restricts access to certain components, e.g., protecting variables with getter and setter methods. Abstraction simplifies complex reality by modeling classes based on the essential properties. Inheritance allows a class to inherit properties from a parent class, exemplified in PHP using the 'extends' keyword, while polymorphism lets methods do different things based on the object it is acting upon, often achieved using method overriding .
A JavaScript object is a standalone entity, consisting of key-value pairs, where keys are strings or symbols, and values can be of any data type, allowing for the representation of complex entities . DOM (Document Object Model) objects contain properties that represent elements in an HTML document, such as `document.body` which refers to the `<body>` of the document. Key methods include `getElementById()` for accessing elements by their ID, `createElement()` for creating new elements, and `appendChild()` which appends a node as the last child of a specified parent node . These methods enable dynamic interaction with web page elements, allowing for real-time updates without requiring a page reload .
Super global variables in PHP are built-in variables that are always accessible, regardless of scope, and include arrays like $_GET, $_POST, $_SERVER, and $_SESSION, which store information from forms, server environment, and session details . The GET method appends data to the URL, thus exposing it, and is suitable for retrieving data, while the POST method sends data in the HTTP request body, which is not visible in the URL, making it more secure for sensitive data transmission . GET is limited by URL length, whereas POST is not, hence it's preferred for submitting large amounts of data .
A Content Management System (CMS) offers several advantages, such as simplifying the process of content creation and management without requiring extensive technical knowledge, thus allowing users with limited technical skills to update and manage websites efficiently . It provides themes and plugins for customization and functionality extension, and enables content collaboration among multiple users with varied roles . On the downside, CMS platforms can be vulnerable to security exploits, often requiring regular updates and maintenance . They might also have limitations in flexibility compared to custom-built sites, as you have to work within the framework and restrictions of the CMS .