# COM431: Server Side Development ## Week 2 Overview - **Course Title**: Server Side Development - **Instructor**: Peter Nicholl - **University**: Simon Fraser University ## Recap of Week 1 - **PHP**: Hypertext Pre-processor, a server-side scripting language. - Used for tasks such as form handling and database access. - Open source and free. - Popular with over 80% usage among professionals. ## Week 1 Learning Outcomes - Introduced PHP fundamentals. - Overview of module aims and objectives. - Assessment structure and requirements. - Lab Class utilized **Laravel Herd** for practical application. - Planned mini challenge for upcoming demonstration. ## Advantages of PHP - User-friendly with HTML integration. - Cost-effective as it is open source. - Extensive built-in libraries for various functionalities: - Email sending - Session management - PDF generation - JSON and XML parsing - DOM interaction - Strong database integration and scalability: - Scalable systems efficiently manage increased workloads. - Support for object-oriented programming. - Performance enhancements in PHP 8. ## Homework from Previous Class - Implement more components for navigation. - Utilize HTML element attributes effectively. - Suggested resource: [30 Days to Learn Laravel](https://laracasts.com/series/30-days-to-learn-laravel-11/episodes/4) ## Customizing the Editor - Recommended software: **Visual Studio Code** - Extensions to improve PHP programming experience: - **Composer**: Integration and management tasks. - **PHP Intelephense**: PHP code intelligence. - **Prettier**: Code formatting. ## PHP Variable Types and Use Cases - **Strings**: - Used for text, data manipulation, and user input processing. - **Integers**: - Whole numbers for indexing and control structure applications. - **Floats**: - Represents decimal numbers for mathematical operations. - **Arrays**: - Collections and associative data storage. - **Functions**: - Encapsulate mathematical and string operations, database interactions, etc. - **Constants**: - Store configuration parameters. - **Objects**: - Data structures for entities like shopping carts. ## Summary of PHP Data Types - **Basic Data Types**: - Integer (e.g., -4, -3, ... 4) - Float (e.g., 3.14, 1.1) - String (e.g., "COM549") - Boolean (true/false) - Array (for multiple data items) - Object (instances of classes) - **Special Types**: - NULL: for unset variables. - Resource: external connections (e.g., databases). - Callable: functions passed to other functions. ## Superglobals in PHP - Global Variables: - `$GLOBALS`, `$_SERVER`, `$_GET`, `$_POST`, `$_COOKIE`, `$_FILES`, `$_ENV`, `$_REQUEST`, `$_SESSION`. ## Common Syntax Issues - Ensure proper use of quotes, parentheses, and brackets. - Each PHP command should end with a semicolon. - Variables are case-sensitive. - PHP disregards blank spaces. ## PHP Iterative Statements - Loops enable code repetition until a condition is met. - **Loop Structures**: - `while`: Executes as long as the condition is true. - `do...while`: Executes at least once; checks condition afterwards. - `for`: Executes a fixed number of iterations. - `foreach`: Iterates through elements in an array. ## Examples of PHP Loops ### While Loop ```php while (condition) { // Code to be executed } ``` - Example: Loop from 1 to 3. ### Do...While Loop ```php do { // Code to be executed } while (condition); ``` - Example: Increasing `$i` and printing it until 3. ### For Loop ```php for (initialization; condition; increment) { // Code to be executed } ``` - Example: Loop from 1 to 3. ### Foreach Loop ```php foreach ($array as $value) { // Code to be executed } ``` - Example: Iterating over an array of colors and superheroes. ## Integrating PHP in HTML ### Example 1 ```html
Name:
Age:
Email:
``` ### Example 2 ```html "Product 1", "item2" => "Product 2", "item3" => "Product 3"); ?>
$product) { echo "
$item: $product
"; } ?>
``` ## Arrays in PHP - **Advantages**: - Single access point for related data. - Useful for implementation of various data structures. - **Disadvantages**: - Static size, requires pre-knowledge of size, potential memory issues. ## Array Types and Access ### Numerically Indexed Arrays ```php $products1 = array('Tires', 'Oil', 'Spark Plugs'); $products2 = ['Tires', 'Oil', 'Spark Plugs']; ``` ### Non-numerically Indexed Arrays ```php $prices = array('Tires' => 100, 'Oil' => 10, 'Spark Plugs' => 4); ``` ### Using Loops to Access Arrays - For accessing indexed arrays and non-indexed ones using `foreach` and loops. ## Array Operators - Various operators such as + (union), == (equality), === (identity), != (inequality), and their functions and implications on comparisons. ## Multidimensional Arrays - Arrays containing another array. Can exhibit different structures to hold more complex data. ```php $products = array( array('TIR', 'Tires', 100), array('OIL', 'Oil', 10), array('SPK', 'Spark Plugs', 4) ); ``` ## Challenge - Review array functions (`var_dump`, `array_push`, `array_shift`) and implement them into a Laravel route to view an array in the browser. ## PHP Conditional Statements - **Common Statements**: - `if`, `if...else`, `if...elseif`, `switch...case` - Example of if-else structure demonstrating outcomes based on conditions. ## Using Laravel Components - Guide on creating and utilizing components with Laravel in order to structure code better. - Examples show syntax and integration. ## Challenges and Lab Tasks - Set up Laravel project, integrate concepts from the course, and prepare for practical demonstrations in the next classes.