diff --git a/PHP.md b/PHP.md
index 2f5d8f4..d43380e 100644
--- a/PHP.md
+++ b/PHP.md
@@ -5,33 +5,35 @@
1. Download [MAMP] (https://www.mamp.info/en/downloads/)
1. Double click .pkg file and follow prompts
1. Double click /Applications/MAMP/MAMP
-1. Point MAMP to working directory
+1. Tell MAMP to where your files are
- Click on Preferences
- Click on Web Server
- Click the folder icon next to "Document Root" and find a suitable directory to work out of
- Click OK
+1. In your Document Root, create `index.php`.
1. Go to
+ - If no file is specified in the URL after the port, MAMP will look for `index.php`
1. Error logs are in /Applications/MAMP/logs/
- - `tail -f php_error.log`
+ - use `tail -f php_error.log` to watch the end of the log file in case something breaks
1. MAMP stands for Mac, Apache, MySQL, PHP
- - this describes the tech stack
- - Mac
- - Your OS
- - Apache
- - A pre-build web server that serves static files
- - Is extendable with various modules that allows it to do many things easily
- - MySQL
- - Your Database
- - PHP
- - A module for Apache that allows it to server dynamic data
+ - Mac
+ - Your OS
+ - Apache
+ - A pre-build web server that serves static files
+ - It is extendable with various modules that allows it to do many things easily
+ - MySQL
+ - Your Database
+ - PHP
+ - A module for Apache that allows it to serve dynamic data
## Basics
### Tags
Because this is all run on top of Apache, the initial assumption is that we're serving static HTML files
+
- We need `` tags to show that we're writing PHP
-- Think of this as if Apache/PHP is server.js and we're just writing EJS
+- Think of this as if Apache/PHP is server.js and we're writing EJS
Instead of `<%= %>` you have `= ?>` or ``
@@ -59,41 +61,62 @@ Use a $ before a variable name to tell php it is a variable. Assignment is stan
$my_first_var; //declare
$my_first_var = 2; //assignment
$my_second_var = 3; //declare and assign
+ echo $my_first_var; //print this variable to the page
+ echo $my_second_var; //print this variable to the page
?>
```
### Data Types
+PHP has the following basic data types:
+
+Strings:
+
```php
```
+
+Integers:
+
```php
```
+
+Floats:
+
```php
```
+
+Booleans:
+
```php
```
+
+Arrays:
+
```php
```
+
+NULL:
+
```php
```
+Some kinds of string interpolation work:
+
+```php
+
+```
+
### Arithmetic Operators
```php
```
@@ -132,8 +164,11 @@ Use a `.` or `.=` to combine strings.
```php
```
@@ -142,10 +177,15 @@ Use a `.` or `.=` to combine strings.
```php
```
@@ -153,33 +193,40 @@ Use a `.` or `.=` to combine strings.
### Formats
-Format 1:
+The traditional format works great:
```php
2){
+ echo "x > 2";
}
- elseif(condition2){
+ elseif($x < 2){
+ echo "x > 2";
}
else{
+ echo "x == 2";
}
?>
```
-Format 2:
+If you have html and don't want to have lines that look like ``, you can use the following style of if/else:
```php
-
- condition1
-
- condition2
+
+ 2): ?>
+ x > 2
+
+ x < 2
- else
+ x == 2
```
### Comparison Operators
+Equality:
+
```php
@@ -228,7 +275,7 @@ print_r($cars); //prints contents of array in nicer format than var_dump
### Associative Arrays (hashes)
-These are very similar to Ruby hashes
+These are very similar to JavaScript objects, but are accessed like arrays:
```php
";
+ echo "The number is: $x
";
$x++;
}
?>
```
+
+Alternative syntax:
+
```php
-
-
-
= $x ?>
-
-
+
```
### For
@@ -265,31 +317,41 @@ while($x <= 5) {
```php
";
+ echo "The number is: $x
";
}
?>
```
+
+Alternative syntax:
+
```php
-
- The number is: = $x ?>
-
+
+
+ - The number is: = $x ?>
+
+
```
### Foreach
+This is like `for of` in JS:
+
```php
$value) {
- echo $key . ": $value
";
+ echo $key . ": $value
";
}
?>
```
+
+Alternative syntax:
+
```php
$value): ?>
- = $key ?>: =$value?>
+ = $key ?>: =$value?>
```
@@ -300,242 +362,16 @@ This works for associative arrays:
$ages = array("Peter" => 35, "Ben" => 37, "Joe" => "43");
foreach ($ages as $key => $value) {
- echo $key . ": $value
";
+ echo $key . ": $value
";
}
?>
```
+
+Alternative syntax:
+
```php
35, "Ben" => 37, "Joe" => "43"); ?>
$value): ?>
- = $key ?>: =$value?>
+ = $key ?>: =$value?>
```
-
-## Functions
-
-```php
-
-```
-
-## Convenience Methods
-
-### Strings
-
-Count a string's length
-
-```php
-
-```
-
-Count number of words in a string
-
-```php
-
-```
-
-Reverse a string
-
-```php
-
-```
-
-Find a sub string in a string
-
-```php
-
-```
-
-Replace text within a string
-
-```php
-
-```
-
-Get a substring based on character index
-
-```php
-
-```
-
-Turn a string into an array based on some delimeter
-
-```php
-
-```
-
-### Sorting Arrays
-
-Various functions for sorting arrays. Can be done arithmetically or alphabetically, depending on content
-
-- sort() - sort arrays in ascending order
-- rsort() - sort arrays in descending order
-- asort() - sort associative arrays in ascending order, according to the value
-- ksort() - sort associative arrays in ascending order, according to the key
-- arsort() - sort associative arrays in descending order, according to the value
-- krsort() - sort associative arrays in descending order, according to the key
-
-These functions affect the actual array they are called on.
-
-```php
-$cars = array("Volvo", "BMW", "Toyota");
-sort($cars);
-print_r($cars); //BMW, Toyota, Volvo
-```
-
-## Classes and Objects
-
-### Public Members
-
-```php
-wheels = 3;
-print_r($my_car);
-?>
-```
-
-### Constructors
-
-```php
-wheels = $num_wheels;
- }
-}
-$my_car = new Car(4);
-?>
-```
-
-### Protected Members
-
-```php
-wheels;
- }
- public function __construct($num_wheels){ // runs at beginning of object creation
- $this->wheels = $num_wheels;
- }
-}
-
-$my_car = new Car(4);
-echo $my_car->getWheels();
-echo $my_car->wheels; //error: cannot access protected property
-?>
-```
-
-### Statics
-
-```php
-wheels;
- }
- public function __construct($num_wheels){ // runs at beginning of object creation
- $this->wheels = $num_wheels;
- }
-}
-class CarFactory{
- static protected $cars = array();
- static public function create($num_wheels){
- $new_car = new Car($num_wheels);
- self::$cars[] = $new_car;
- return $new_car;
- }
- static public function find($index){
- return self::$cars[$index];
- }
-}
-$my_car = CarFactory::create(2);
-$my_car2 = CarFactory::create(4);
-print_r($my_car);
-print_r(CarFactory::find(1));
-print_r(CarFactory::$cars); //errors out: protected
-?>
-```
-
-### Inheritance
-
-```php
-wheels = $num_wheels;
- }
- public function getWheels(){
- return $this->wheels;
- }
- public function start(){
- $this->engine_on = true;
- }
-}
-
-class Humvee extends Car {
- protected $armour = 10;
- public function takeDamage($damage){
- $this->armour -= $damage;
- }
- public function loseWheel(){
- $this->wheels--; //can access parent $wheels member
- }
- //can extend parent's start function
- public function start(){
- parent::start(); //run parent's start function
- //parent::$engine_on = false; //errors out: cannot access private member of parent
- echo "WELCOME TO THE JUNGLE...";//then do something else
- }
-}
-
-$my_humvee = new Humvee(6);
-print_r($my_humvee);
-$my_humvee->loseWheel();
-print_r($my_humvee);
-$my_humvee->takeDamage(6);
-print_r($my_humvee);
-$my_humvee->start();
-print_r($my_humvee);
-?>
-```
diff --git a/PHP2.md b/PHP2.md
new file mode 100644
index 0000000..ad476d1
--- /dev/null
+++ b/PHP2.md
@@ -0,0 +1,261 @@
+#Intermediate PHP
+
+## Functions
+
+```php
+
+```
+
+## Convenience Methods
+
+PHP has lots of global functions that will help you out:
+
+### Strings
+
+Count a string's length
+
+```php
+
+```
+
+Count number of words in a string
+
+```php
+
+```
+
+Reverse a string
+
+```php
+
+```
+
+Find a sub string in a string
+
+```php
+
+```
+
+Replace text within a string
+
+```php
+
+```
+
+Get a substring based on character index
+
+```php
+
+```
+
+Turn a string into an array based on some delimiter:
+
+```php
+
+```
+
+### Sorting Arrays
+
+Various functions for sorting arrays. Can be done arithmetically or alphabetically, depending on content
+
+- sort() - sort arrays in ascending order
+- rsort() - sort arrays in descending order
+- asort() - sort associative arrays in ascending order, according to the value
+- ksort() - sort associative arrays in ascending order, according to the key
+- arsort() - sort associative arrays in descending order, according to the value
+- krsort() - sort associative arrays in descending order, according to the key
+
+These functions affect the actual array they are called on.
+
+```php
+$cars = array("Volvo", "BMW", "Toyota");
+sort($cars);
+print_r($cars); //BMW, Toyota, Volvo
+rsort($cars);
+print_r($cars); //Volco, Toyota, BMW
+```
+
+Now with associative arrays:
+
+```php
+$ages = array("Peter" => 35, "Ben" => 37, "Joe" => "43");
+asort($ages);
+print_r($ages);
+ksort($ages);
+print_r($ages);
+arsort($ages);
+print_r($ages);
+krsort($ages);
+print_r($ages);
+```
+
+## Classes and Objects
+
+### Public Members
+
+```php
+wheels = 3;
+print_r($my_car);
+?>
+```
+
+### Constructors
+
+```php
+wheels = $num_wheels;
+ }
+}
+$my_car = new Car(4);
+print_r($my_car);
+?>
+```
+
+### Protected Members
+
+But what if you don't want someone to modify the number of wheels of a car after it is created?
+You can set a method or member to `private` and any instance will not be able to read or write that value. It is only accessible from within the class itself
+
+```php
+wheels;
+ }
+
+ public function __construct($num_wheels){
+ $this->wheels = $num_wheels;
+ }
+}
+
+$my_car = new Car(4);
+echo $my_car->getWheels();
+echo $my_car->wheels; //error: cannot access protected property
+$my_car->wheels = 4; //error
+?>
+```
+
+### Statics
+
+You can have `static` members and methods that accessible only on the class itself, not on instances of the class.
+
+```php
+wheels;
+ }
+ public function __construct($num_wheels){
+ $this->wheels = $num_wheels;
+ }
+}
+
+class CarFactory{
+ static protected $cars = array(); // the array is protected
+ static public function create($num_wheels){
+ $new_car = new Car($num_wheels);
+ self::$cars[] = $new_car;
+ return $new_car;
+ }
+ static public function find($index){
+ return self::$cars[$index];
+ }
+}
+
+$my_car = CarFactory::create(2);
+$my_car2 = CarFactory::create(4);
+print_r($my_car);
+print_r(CarFactory::find(1));
+print_r(CarFactory::$cars); //errors out: protected
+?>
+```
+
+### Inheritance
+
+Protected members/methods can be used by inheriting classes, private members/methods cannot
+
+```php
+wheels = $num_wheels;
+ }
+ public function getWheels(){
+ return $this->wheels;
+ }
+ public function start(){
+ $this->engine_on = true;
+ }
+}
+
+class Humvee extends Car {
+ protected $armour = 10;
+ public function takeDamage($damage){
+ $this->armour -= $damage;
+ }
+ public function loseWheel(){
+ $this->wheels--; //can access parent class $wheels member because it is protected
+ }
+ //can extend parent's start function
+ public function start(){
+ parent::start(); //run parent's start function
+ //parent::$engine_on = false; //errors out: cannot access private member of parent
+ echo "WELCOME TO THE JUNGLE...";//then do something else
+ }
+}
+
+$my_humvee = new Humvee(6);
+print_r($my_humvee);
+$my_humvee->loseWheel();
+print_r($my_humvee);
+$my_humvee->takeDamage(6);
+print_r($my_humvee);
+$my_humvee->start();
+print_r($my_humvee);
+?>
+```