COMP721 Web Development Week 3: PHP Part 2
Advanced Arrays
- Searching and Sorting Arrays
- Sorting methods:
sort(),rsort(), andusort()for indexed arrays. - Associative array sorting functions:
asort(),arsort(),uasort()(by value)ksort(),krsort(),uksort()(by key)
- Sorting methods:
PHP Superglobals
- Definition: Predefined global arrays accessible from anywhere in your code.
- Functions and Uses:
_POST, etc. for handling form data and client information.
PHP Strings and Associated Functions
String Basics:
- Strings can be made using either single or double quotation marks.
- Variables can be embedded directly in strings.
String Manipulation:
- Concatenation using
.operator. - Various functions to manipulate strings:
strlen(),str_word_count(): counting characters and words.strpos(),strrpos(): searching for the position of a character.substr(): extracting substrings.
- Concatenation using
Functions for Array Manipulation
- Common Array Functions:
array_shift(): Removes first element;array_push(): Adds elements to the end;array_merge(): Combines arrays, handling duplicates differently based on method used.array_diff(),array_intersect(): Comparing arrays.
Binary Search
- Description: Efficient way to find an element in a sorted array by repeated dividing the search interval in half.
- Function:
- Code implementation requires understanding of recursion:
function binarySearch($arr, $start, $end, $x) {
if($end < $start) return false;
// (implementation continues)
}
Quicksort Algorithm
- Concept: Recursive sorting algorithm where you choose a pivot and sort lesser or greater values around it.
- Complexity: Average time complexity is .
- Implementation:
function quicksort(&$arr, $lo, $hi) {
// (full implementation)
}
Working with HTML Forms
- Input Types:
- Different basic and new HTML5 input types: text, password, file, etc.
- Form Handling:
- Understanding the difference between
GETandPOSTmethods (security, visibility in URL, etc.).
- Understanding the difference between
Form Data Validation
- Functions:
empty(): Check if variables are undefined or empty.isset(): Check if variable is declared.- Numeric Validation: Using
is_numeric().
Further String Functions
- Manipulating Strings:
str_replace(),str_ireplace(): For search and replace.- Tokenizing strings using
strtok()andexplode()to break strings into arrays.
Conclusion
- OOP in PHP: Short overview on creating classes and objects in PHP.