PHP_ PHP Hypertext Preprocessor
PHP_ PHP Hypertext Preprocessor
Preprocessor
CSCI 165
Introduction
Open source
<
Data Types
Array $fruits = array (apple, orange, lemon); Used to store multiple values
Arithmetic
Comparison
Logical
Values can also be requested from the user of the program, using the readline function:
When this line is executed, the terminal side will show the string statement and the user will be
able to type in a value:
Arrays Overview
string key)
Adding to an array
Deleting from an array
1. sort()Sorts an array by values in ascending order. Re-indexes the array, meaning the original keys are lost.
Example: sort($array);
2. rsort()Sorts an array by values in descending order. Re-indexes the array, so keys are lost.
Example: rsort($array);
3. asort()Sorts an array by values in ascending order while preserving the original keys.
Example: asort($array);
4. arsort()Sorts an array by values in descending order while preserving the original keys.
Example: arsort($array);
Outputting an array (print_r function)
Looping through an indexed array
Looping through an Associative array
Counting elements in an array
Use Count() function
String functions
PHP Manual - all
Array functions Built-in Functions
Math functions
Example:
§Input number between 1 and 10;
§Input second number between 11 and 20;
§Add first number to second number;
https://www.w3sch
ools.com/php/php_if
_else.asp
Selection Examples - If statement
Selection Examples - If Else statement
Selection Examples - If Elseif Else statement
More info:
https://www.w3sch
ools.com/php/php_s
witch.asp
Iteration Types
For Loop (counter-controlled loop) - executes a block of code a fixed
number of times
Code
example
Output
Iteration examples- For Each Loop to
access values AND keys
Syntax
Code
example
Output
Iteration examples- While Loop
Iteration examples- Do While Loop
User registration
Login
Contact form
Newsletter subscription
Order form
etc….
HTML Form Tags
Text Checkbox
HTML input
types: Email Submit
fopen()
● Opens a file
● 2 parameters needed (file name, and mode (e.g. r, w a)
fread()
● Reads a specified number of bytes
● File must be opened before using fread()
fclose()
● Closes the file
● Necessary so that the file doesn’t continue to use system
resources
File Editing - Reading Binary files with fread()
File Editing - Reading with fgets()
If working with text file, fgets() is more appropriate to use than
fread()
https://www.w3sc
hools.com/php/ph
p_file_open.asp
File Editing - Writing to a File
File Editing - Appending to a File
https://www.w3
schools.com/ph
p/php_file_creat
e.asp