Notes 14 - Intro to PHP
Lecture Topics:
- php syntax
- running a php script via console
- accessing php script via browser HTTP request
- data types
- variables/identifiers
- scope
- operators
- constants
- conditional statments
- control structures
- looping
- embedding php in html
- understanding php output
- developer mode and errors
- logging
- print_r($var) and var_dump($var)
:~$php myScript.php
<?php // php opens here and closes here?>
<h1>HTML and PHP</h1>
<?php // php opens here again and closes here?>
$myVariable; // $[A..Za..z_][A..Za..z0..9_]+ case sensitive
DataTypes
- integer
- float
- string
- boolean
- array
- object
- resource
string gettype(var); bool settype(var,'type');
is_int(), is_float(), is_string(), is_bool(), is_array(), is_object,
is_null(), is_resource
bool isset(var [, var[,...]]); void unset(var [, var[,...]]);
bool empty(var);
Scope
- Superglobals: global scope
- Constants: global scope
- Global Variables: global scope of script, and func **if declared
- Static Variables: local to function definition, retians value
- Function Variables: local to function, D.N.E(xists) after func
Superglobals: $_GET, $_POST, $GLOBALS, $_COOKIE, $_SESSION, $_FILE, $_ENV,
$_REQUEST, $_SERVER
Operators
- Assignment: =
- Arithmetic: + - * / %
- Arithmetic Assignment: += -= *= /= %=
- String ops: . .=
- ++increment decrement-- pre and post
- Comparison: < > <= >= == === != !== <>
- Logic: && AND, || OR, ! NOT, xor
- Ternary/Conditional: ?:
Control Structures
- if(expression) { ... }
- if(expression) { ... } else { ... }
- if(expression) { ... } elseif( expression) { ... } else { ... }
- switch($var) { case <int, float, char, string>: ... break;
default: ... }
Looping
- while(expression) { ... }
- do { ... }while(expression);
- for(init; test; update) { ... }
- foreach($arr as $key => $val) { ... }
Top