ITEW3 PRELIM.pdf
Document Details
Uploaded by Deleted User
Tags
Full Transcript
1. What does server-side scripting refer to? Scripts that run in the user's browser Scripts that run on a web server Scripts that are executed on the client side Scripts that are embedded in HTML 2. Which of the following is NOT a feature of PHP? Can be embedded within...
1. What does server-side scripting refer to? Scripts that run in the user's browser Scripts that run on a web server Scripts that are executed on the client side Scripts that are embedded in HTML 2. Which of the following is NOT a feature of PHP? Can be embedded within HTML Supports multiple platforms Executes on the client side Provides built-in support for session management 3. What is the main role of PHP in web development? To manage database transactions only To create static web pages To generate dynamic web content on the server To handle web content 4. Which PHP version introduced object-oriented programming support? PHP 2.0 PHP 3.0 PHP 4.0 PHP 5.0 5. What is the latest stable version of PHP as of the given text? PHP 8.0 PHP 8.1 PHP 8.2 PHP 8.3 6. Which of the following is a feature of the PHP 8.3 version? JavaScript integration Deep-cloning of readonly properties Support for new database systems Static typing of variables 7. What is a common use of PHP? Creating desktop applications Managing dynamic content and databases Designing graphics and images Running background processes 8. What does the PHP tag 10 5 25 50 13. Which PHP loop guarantees that the loop’s body will execute at least once? for while do...while foreach 14. What does the break keyword do in a loop? Continues to the next iteration Exits the loop immediately Skips the current iteration Restarts the loop from the beginning 15. Which of the following data types is NOT supported by PHP? Integer Float Boolean Character 16. How is an associative array different from an indexed array in PHP? Uses numeric indexes Uses named keys Cannot be looped through Stores values in a element 17. What does the echo statement do in PHP? Read value from the terminal Outputs text to the browser Declares a output Defines a echo function 18. Which of the following is a method to handle form data in PHP? $_GET $_SESSION $_COOKIE $_SERVER 19. What is the purpose of the return keyword in a PHP function? To output a value To terminate the function To specify the type of variable To return a value from the function 20. Which PHP function is used to read data from a file? file_put_contents() file_get_contents() readfile() file_write() INTRO DUCTION TO SERVER-SIDE SCRIPTING Lear ning Outcome ▪ To explain the concept of server -side scripting and its distinction from client-side scripting. ▪ To introduce PHP as a powerful tool for web developm ent and understand its applications. ▪ To enable learners to set up their own local development environment for PHP scripting. 1. W HAT IS SERVER-SI DE SCRI PTING? Server-side scripting refers to scripts that run on a web server to generate dynamic web content. Unlike client-side scripts (which run in the user's browser), server -side scripts are executed on the server before the content is sent to the user's browser. This allows for more complex operations, like accessing databases, processing form data, and customizing content based on user preferences or actions. Key Points: Execution: Happens on the server, not the client's browser. Security: Data processing is more secure since the code isn't exposed to the user. Exam ples: PHP, Node.js, Python (Django/Flask), Ruby on Rails. 2. OVERVIEW OF PHP AND ITS RO LE IN W EB DEVELO PM ENT PHP (Hypertext Preprocessor) is a widely used open-source server-side scripting language. It is specifically designed for web developm ent and can be embedded directly into HTML. PHP is a widely used open source and general-purpose server-side scripting language and popular for creating dynamic and interactive websites from e-comm erce websites such as Shopee, Lazada, Amazon to CRM system s like HubSpot and Salesforce due to its ease of use and extensive support for databases. PHP is executed on the server, generating HTM L that is sent to the client's browser. Key Features: Integration: Can be embedded within HTML and works well with databases like MySQL. Cross-Platform: Runs on various platforms such as Windows, Linux, and macO S. Session Management: It provides built-in support for session management, allowing developers to maintain user state across different pages. File Handling: PHP can read and write files on the server, enabling functionalities like file uploads and downloads. Extensive Libraries: A wide range of built-in functions and libraries are available for tasks such as image processing, data encryption, and more. Wide Adoption: Powers popular platforms like WordPress, Drupal, and Joomla. Com m unity: A large com munity provides extensive libraries, frameworks (like Laravel , CodeIgniter), and tools. 1|P a g e e.g. Dangal Greetings Instead of lots of com mands to output HTML, PHP pages contain HTM L with embedded code. The PHP code is enclosed in special start and end processing instructions that allow you to jump into and out of "PHP mode." History of PHP PHP was created in 1994 by Rasmus Lerdorf. The very first incarnation was a simple set of Comm on Gateway Interface (CGI) binaries written in C program ming language. In 1995, Lerdorf released the first version officially called "Personal Home Page Tools (PH P Tools)". In 1997, PHP/FI 2.0 was released, which combined the Personal Hom e Page tools with a Form Interpreter and added mSQL support. By late 1996, PHP/FI was estimated to be in use on at least 15,000 websites. In 1997, Zeev Suraski and Andi Gutmans rewrote the parser from scratch, forming the basis for PHP 3.0. PHP 3.0, released in 1998, was the first version that closely resembled PHP as it exists today. It introduced features like object -oriented programming support and a more powerful and consistent language syntax. By the winter of 1998, Suraski and Gutmans began working on a rewrite of PHP's core, which they dubbed the "Zend Engine". PHP 4.0, based on this engine, was released in 2000 and included features like support for many more web servers, HTTP sessions, and o utput buffering. PHP 5, released in 2004, was mainly driven by the Zend Engine 2.0 with a new object model and dozens of other new features. It included improved support for object -oriented programming and the PHP Data Objects (PDO) extension for database abstraction. Latest Version of PHP The latest stable version of PHP is 8.3.0, released on Novem ber 23, 2023. It is supported until December 31, 2027. Some notable features of PHP 8.3 include: ▪ Explicit typing of class constants ▪ Deep-cloning of readonly properties ▪ Additions to the randomness functionality ▪ The #[\Override] attribute to ensure that overriding a parent method is intentional ▪ The json_validate() function to efficiently check if a string is syntactically valid JSO N. 2|P a g e The next major version, PHP 8.4, is scheduled for release on November 21, 2024 The term PHP stands for PHP Hypertext Preprocessor. Originally, the “PHP” within the acronym stood for Personal Home Page. But, as the language evolved and caught on, it ended up being used for more than just personal hom e pages. So, that acronym, in turn, became just the “P” within PHP. PHP Features: PHP is used to manage dynamic content, databases, session tracking, even build entire e - com merce sites. It is integrated with a num ber of popular databases, including MySQ L, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server. PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the Unix side. The MySQL server, once started, executes even very complex queries with huge result sets in record-setting time. PHP supports a large number of major protocols such as PO P3, IM AP, and LDAP. PHP4 added support for Java and distributed object architectures (CO M and CO RBA), making n -tier developm ent a possibility for the first time. PHP is forgiving: PHP language tries to be as forgiving as possible. This means to accom modate developers by allowing for flexibility in coding practices PHP Syntax is C-Like. Com m on Uses of PHP PHP performs system functions, from files on a system it can create, open, read, write, and close them. The other uses of PHP are: PHP can handle forms, i.e. gather data from files, save data to a file, thru email you can send data, return data to the user. You add, delete, and modify elem ents within your database thru PHP. Access cookies variables and set cookies. Using PHP, you can restrict users to access some pages of your website. It can encrypt data. Characteristic of PHP Sim plicity Efficiency Security Flexibility Familiarity W hat Kind of Language is PHP? Program ming languages are divided into groups depending on their characteristics. interpreted/com piled, strongly/loosely typed, dynamically/statically typed. PHP is often called a “scripting language” and it’s an interpreted language. If you’ve used compiled languages like C or C# or Go, the main difference is that you don’t need to com pile a PHP program before you run it. 3|P a g e Those languages are compiled, and the compiler generates an executable program that you then run. It’s a 2-steps process. The PHP interpreter is responsible for interpreting the instructions written in a PHP program when it’s executed. It’s just one step. You tell the interpreter to run the program. It's a completely different workflow. PHP is a dynamically typed language. The types of variables are checked at runtime, rather than before the code is executed as happens for statically typed languages. PHP is also loosely (weakly) typed. Compared to strongly typed languages like C#, Go, C or Java, you don’t need to declare the types of your variables. Being interpreted and loosely/dynamically typed will make bugs harder to find before they happen at runtime. In compiled languages, you can often catch errors at compile time, something that does not happen in interpreted languages. But on the other hand, an interpreted language has more flexibility. PHP is written internally in C, a com piled and statically typed language. PHP is similar to JavaScript, another dynamically typed, loosely typed, and interpreted language. PHP supports object -oriented programming, and functional programming. 3. SETTING UP A LO CAL DEVELOPM ENT ENVIRO NM ENT To start developing PHP, you need a local environment where you can write and run PHP scripts. Here's how to set it up: Steps: Install a Local Server: You can use software packages like XAM PP, W AM P, or M AM P, which bundle Apache (a web server), PHP, and MySQL (a database) together. Editor/IDE: Choose a code editor like VS Code, Sublim e Text, or an IDE like PhpStorm. Configuration: Once installed, you can place your PHP files in the "htdocs" directory (XAM PP) or "www" directory (W AM P). Access your scripts by navigating to http://localhost/your -script.php in your browser. Com m on Tools: XAM PP: Cross-platform, includes Apache, MySQL, PHP, and Perl. W AMP: Windows-specific, similar to XAM PP but tailored for Windows users. M AM P: Mac-specific, includes Apache, MySQL, and PHP. 4. W RITING AND RUNNING A BASIC PHP SCRIPT Once your environment is set up, you can start writing PHP code. e.g. 4|P a g e Steps to Run: 1. Save the Script: Save the above code as hello.php in your local server's directory (e.g., htdocs for XAM PP). 2. Access the Script: Open your web browser and navigate to http://localhost/hello.php. 3. Result: You should see " Dangal Greetings!" displayed on the page. 5|P a g e PHP SYNTAX AND BASI C CO NST RUCT Lear ning Outcome ▪ Discuss the fundam entals of PHP syntax, including variables, data types, operators, control structures and loop structures. ▪ Introduce how to embed PHP within HTML to create dynamic web content. ▪ Explain how to work with functions and arrays to build basic dynamic web applications. 1. PHP SYNTAX AND EM BEDDING PHP IN HTM L PHP (Hypertext Preprocessor) is a server-side scripting language embedded within HTML to create dynamic web pages. It is executed on the server, and the result is sent to the client's browser as plain HTM L. PHP scripts are executed on the server, and the result is returned to the browser as plain HTML. Basic constructs include PHP tags, comm ents, and simple output functions like echo. Php Syntax: Em bedding PHP in HTML: Em bedding PHP in HTML allows developers to create dynamic web pages where the content can change based on user interaction or other factors. You can write HTML and em bed PHP code within it to dynamically generate content. PHP can be placed within HTML tags or as standalone scripts within an HTM L docum ent. e.g. Greetings 2. VARIABLES, DAT A TYPES, AND OPERATO RS Variables: Variables in PHP start with a $ sign followed by the name of the variable. Variable nam es must start with a letter or an underscore and can contain letters, numbers, and underscores. e.g. Data Types: String: A sequence of characters. Example: " Dangal Greetings!" Integer: Whole numbers. Exam ple: 42 Float: Decimal num bers. Example: 3.14 Boolean: true or false Array: An ordered map of values. Example: array(1, 2, 3) Object: An instance of a class. NULL: A special type that represents a variable with no value. e.g. Operators: Arithm etic Operators: +, -, *, /, % Assignm ent Operators: =, +=, -=, *=, /=, %= Comparison Operators: ==, ===, !=, !==, >, =, 3. CO NT RO L ST RUCTURES: IF, ELSE, SWITCH Control structures in PHP allow you to control the flow of your program based on conditions. The most com mon control structures are if, else, and switch. 7|P a g e a. If statement The if statement is used to execute a block of code only if a specified condition is true. Syntax: e.g. ▪ The condition $age >= 18 is checked. ▪ If the condition is true (e.g., $age is 20), the code within the if block is executed, outputting "You are eligible to vote." b. If-else Statem ent The if-else statement allows you to execute one block of code if a condition is true and another block if the condition is false. Syntax: if (condition) { // Code to be executed if condition is true } else { // Code to be executed if condition is false } Example ▪ If the condition $age >= 18 is false (e.g., $age is 16), the code within the else block is executed, outputting "You are not eligible to vote." c. ELSEIF Statement The elseif statem ent allows you to check multiple conditions. 8|P a g e Syntax: if (condition1) { // Code to be executed if condition1 is true } elseif (condition2) { // Code to be executed if condition2 is true } else { // Code to be executed if all conditions are false } Example ▪ The code checks multiple conditions to determine the grade based on the score. ▪ If the score is 75, "Grade: C" is output. d. Switch Statement The switch statement is an alternative to using multiple if-else statements when com paring the sam e variable to different values. Syntax: switch (variable) { case value1: // Code to be executed if variable equals value1 break; case value2: // Code to be executed if variable equals value2 break; // You can have any number of cases default: // Code to be executed if variable does not match any case } Example: : Displays error messages next to the form fields. 18 | P a g e