PHP - robbiehume/CS-Notes GitHub Wiki
Links
General
- PHP is processed server side; need a server to run it
- Browsers interact with the web server (Apache), which then runs the needed .php file by sending it to the PHP processor
- The processor will run the .php file and make any necessary database calls and then return data to Apache
- Then Apache sends it back to the browser
- PHP is what's called a preprocessor: it processes on the server side before it sends it back
- Most of the time PHP code is used to write out or output some kind of HTML, CSS, or JS code
Misc. Notes
var_dump()
Characteristics
- Loosely typed
PHP tag:
- When adding it inside HTML:
<?php *code* ?> - When creating pure php file (no HTML just PHP):
<?php *code*(no closing tag)
Including HTML:
<?php include “header.html” ?>- Helps make website more modulized
Syntax:
- Must add semicolon at end (;)
- Escaping PHP from HTML: separating PHP and HTML from each other to create cleaner code
-
<p>This is a paragraph</p> <?php echo "This is also a paragraph"; ?> <p>This is another paragraph</p>
-
- Comments: // and /* */
User input form (@1:20:00) / URL parameters (@1:29:00):
$_GET["<name of HTML tag>"]- When an HTML form is submitted, it goes to the specified URL and passes the form data parameters
POST vs GET:
- When GET is used for the HTML form method, it shows the values in the URL
- When POST is used, it doesn’t show it in the URL
- POST should be used when you have sensitive info that you’re using like a password or credit card number
- In PHP, need to use $_POST instead of $_GET to retrieve the value
Data types:
10 primitive data types broken into 3 categories:
- Scalar: string ('' or ""), integer, float, boolean (true or false)
- Compound: array, object, callable, iterable
- Special: resource, NULL
Variables:
- Variables names start with $
$myVar = "var";- Same with referencing them:
echo $myVarorecho "value: $myVar"
- Constants:
CONST PI = 3.14;
String functions:
$str_var = “String”strtoupper($str_var)// STRING;strtolower($str_var)// stringstrlen($str_var)// 6$str_var[1]// “t”$str_var[5]= “G” // StrinGstr_replace(“St”, “sT”, $str_var)// sTringsubstr($str_var, 3, 2)// in- String concatenation:
$a = “hello ”$b = $a . “world”// “hello world”$a .= “world”// “world world”
Arrays
$arr = array(“hi”, “hello”, 1, false)$arr[4] = “yo”// $arr: “hi”, “hello”, 1, false, “yo”echo $arr[4]// “yo”echo count($arr)// 5
Using checkboxes:
- Storing checkbox values in an array (@1:50:00)
Associative arrays (map / dictionary):
- Can also store key/value pairs
$grades = array(“Jim”=>”A+”, “Pam”=>”B-“, “Oscar”=>”C+”)echo $grades[“Jim”]// “A+”$grades[“Jim”] = “F”echo count($grades)// 3
Operators:
Equality:
==(!=) : implicitly cast and check if same value1 == "1"// true
===(!==) : check if they are the same type and value1 === "1"// false
- Spaceship (
<=>):- $x <=> $y returns:
- 1 if left ($x) is greater than right ($y)
- 0 if equal
- -1 if right ($y) is greater than left ($x)
- $x <=> $y returns:
Logical:
and/&&or/||xor: true only if one of the expressions is true, false otherwise (both true/false)
String operator (.):
- Combine strings:
-
$a = "My name is "; $b = $a . "is Robbie";
-
Control structures: