COMP721 Web Development - PHP Part 1 Notes
PHP Basics
- Basic PHP Scripts
- Getting started with PHP
- Understanding PHP data types and operators
- Floating-point precision issues
- Expressions, including the Euclidean Algorithm (GCD)
- Functions and control structures
Web Development Environment
- Components needed for PHP development
- A web browser (e.g., Chrome, Firefox, Edge, Safari)
- A web server (e.g., Apache with PHP support)
- A database (e.g., MySQL, MariaDB)
- Recommended IDE: VS Code
- Stack options include WAMP, MAMP, LAMP, XAMPP
PHP Overview
- PHP: Stands for PHP Hypertext Preprocessor
- Examples of other acronyms: GNU (GNU is Not Unix), PIP (PIP Install Packages)
Running PHP
- On personal devices, you can set up an AMP stack (Apache, MySQL, PHP) using WAMP, MAMP, LAMP, or XAMPP
- Online PHP editors are available for quick testing
Embedded Scripting
- PHP can be embedded in HTML documents
- Server-side scripts (e.g., PHP, C#) are executed on the server and not sent to the client
- Client-side languages (e.g., JavaScript) run in the user's browser
PHP Files
- PHP files should have a
.phpextension - PHP code is executed on the server, and the output is sent to the client
Code Blocks in PHP
- Code Declaration Blocks: Sections for PHP code
- Standard PHP script delimiters:
<?php ... ?> - Short tags:
<? ... ?>are not recommended - ASP-style delimiters:
<% ... %>
- Standard PHP script delimiters:
Functions in PHP
- A function is a self-contained block of code designed to perform a specific task.
- Functions are invoked with their name and can accept parameters:
function_name(argument1, argument2) - Example of a basic function:
php function greet($name) { echo "Hello, $name!"; } greet("John");
- Functions are invoked with their name and can accept parameters:
- The
phpinfo()function displays configuration information about PHP.
Output in PHP
- Use
echoandprintto send output back to the client:- Example:
php echo "Welcome to PHP!";
- Example:
Variables in PHP
Variables must start with a
$sign, followed by the identifier nameVariable naming guidelines:
- Can include letters, numbers, or underscores
- Cannot begin with a number or contain spaces
Example:
```php
$votingAge = 18;
echo $votingAge;
## Data Types in PHP
- **Primitive Data Types**:
- **Integers**: Whole numbers, e.g., `-1`, `0`, `5`
- **Floats**: Decimal numbers, e.g., `3.14`
- **Booleans**: `true` or `false`
- **Strings**: Sequences of characters
## Arrays
- **Indexed Arrays**: Collections of values stored with numerical indexes
- **Associative Arrays**: Arrays that use named keys instead of numeric indexes
- Example of accessing an array:
php
$fruits = array("apple", "banana", "cherry");
echo $fruits[1];
```
PHP Expressions
- An Expression is a combination of variables, literals, and operators that PHP can evaluate.
- Operators Types:
- Arithmetic Operators:
+,-,*,/,% - Assignment Operators:
=,+=,-=, etc. - Comparison Operators:
==,===,!=, etc. - Logical Operators:
&&,||,!
- Arithmetic Operators:
Control Structures
- Functions: Used to encapsulate code for reuse. Defined using
function_name(parameters) { ... } - Conditionals:
if,else, andswitchstatements control program flow - Loops: Repeats code blocks while a condition is true, includes
while,for, andforeachstructures
Summary
- These are the foundational concepts of PHP programming essential for creating dynamic web applications.