# PHP ## Setup 1. Download [MAMP] (https://www.mamp.info/en/downloads/) 1. Double click .pkg file and follow prompts 1. Double click /Applications/MAMP/MAMP 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/ - 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 - 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 writing EJS Instead of `<%= %>` you have `` or `` Instead of `<% %>` you have `` ### Comments ```php // single line comment ``` ```php /* multi line comment */ ``` ### Declaring/Assigning variables Use a $ before a variable name to tell php it is a variable. Assignment is standard. ```php ``` ### Data Types PHP has the following basic data types: Strings: ```php ``` Integers: ```php ``` Floats: ```php ``` Booleans: ```php ``` Arrays: ```php ``` NULL: ```php ``` ### String Operators Use a `.` or `.=` to combine strings. Works just like `+` and `+=` ```php ``` Some kinds of string interpolation work: ```php ``` ### Arithmetic Operators ```php ``` ### Increment/Decrement Operators ```php ``` ### Assignment Operators ```php ``` ## Conditionals ### Formats The traditional format works great: ```php 2){ echo "x > 2"; } elseif($x < 2){ echo "x < 2"; } else{ echo "x == 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 2): ?> x > 2 x < 2 x == 2 ``` ### Comparison Operators Equality: ```php ``` Arithmetic: ```php $y; //greater than $x <= $y; //less than or equal to $x >= $y; //greater than or equal to ?> ``` ### Logical Operators ```php ``` ## Arrays ### Indexed Arrays Standard array functionality ```php ``` ### Associative Arrays (hashes) These are very similar to JavaScript objects, but are accessed like arrays: ```php 35, "Ben" => 37, "Joe" => "43"); //declare $age["Bob"] = 105; //add at a new position echo "Bob is " . $age['Bob'] . " years old."; ?> ``` ## Loops ### While ```php "; $x++; } ?> ``` Alternative syntax: ```php ``` ### For ```php "; } ?> ``` Alternative syntax: ```php ``` ### Foreach This is like `for of` in JS: ```php $value) { echo $key . ": $value
"; } ?> ``` Alternative syntax: ```php $value): ?> :
``` This works for associative arrays: ```php 35, "Ben" => 37, "Joe" => "43"); foreach ($ages as $key => $value) { echo $key . ": $value
"; } ?> ``` Alternative syntax: ```php 35, "Ben" => 37, "Joe" => "43"); ?> $value): ?> :
``` ## Functions ```php ```