Week 2

Notes 2 - 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) { ... }

Developer Mode

define('DEVELOPER', false);
if(DEVELOPER) {
    error_reporting(E_ALL);
    ini_set("display_errors",1);
    ini_set("error_log",'myLogFile.log');
}



Top