now stands for PHP: Hypertext Preprocessor - which is a recursive backronym - Used on ~40% of all websites - E.g., large part of Facebook’s front-end is written in PHP
request HTTP response file’s extension must be .php # Web server knows from the extension # that it has to use a PHP interpreter: LoadModule php5_module "c:/php/php5apache2.dll" AddType application/x-httpd-php .php Internet
use an online try-it editor for today’s exercises - E.g., http://phptester.net/ Both the client and the server run on the same machine. No internet connection needed. Client Server HTTP request HTTP response Your machine http://localhost refers to the web server running on your local machine
?> - File must have .php extension! - In general, mixing HTML and PHP code is bad practice — use templating engines (coming later) <html> […] Today is: <?php echo date("Y-m-d H:i:s"); ?> […] </html>
executed } - Note: function names are not case-sensitive - Arguments separated with a comma function functionName($arg1, $arg2) { - Arguments can have default values function functionName($arg1, $arg2=50) {
for declaring a variable - Start with $ - Variable is created the first time it’s used $name = "John"; $age = 28; - Type is determined by the interpreter - Variable names are case-sensitive
- declared within a function => local scope - global keyword is used to access a global variable within a function function myTest() { global $x; […] }
Constants are automatically global across the entire script - Convention is to use UPPERCASE for constants define("DB_SERVER", "localhost"); define("DB_USERNAME", "balog");
everywhere - Methods declared without explicit visibility are public - Class constants are always public (cannot be made private or protected) - private — can only be accessed by the class that declared it - By convention, variables/methods are prefixed with _ - For example: private $_value; private function _dosomething($param) { … } - protected — an be accessed only within the class itself and by inherited classes
or method being referenced is declared as const or static, then you must use the :: operator, otherwise use -> - If you are accessing a const or static variable or method from within a class, then you must use the self-reference self - If you are accessing a variable or method from within a class that is not const or static, then you must use the self- referencing variable $this
string - If you don’t specify a timestamp, the current date and time will be used - Use the mktime() function to get the Unix timestamp for a date - See - http://php.net/manual/en/function.date.php - http://www.w3schools.com/php/php_date.asp $d = mktime(11, 14, 54, 8, 12, 2014); echo date("Y-m-d h:i:s", $d); // 2014-08-12 11:14:54
time - You can do math on them, e.g., calculate difference - See http://php.net/manual/en/class.datetime.php - Calendar functions - Day of week, number of days in a month, etc. - See http://php.net/manual/en/ref.calendar.php
the URL - E.g., http://localhost/myscript.php?name=John&age=12 - Works both with links (<a href=…>) and forms - POST request - Variables are passed in the request body - Works only with forms
$_REQUEST contain the input variables as associative arrays - $_GET is when HTTP method GET is used - $_POST is when HTTP method POST is used - $_REQUEST is for both GET and POST methods - Avoid using $_REQUEST unless you know what you’re doing!
if $_GET['xxx'] (or $_POST['xxx']) is set $page = ""; if (isset($_GET['page'])) { $page = $_GET['page']; } echo $page; - A more compact solution: $page = isset($_GET['page']) ? $_GET['page'] : "";
?> - Convention to use .inc.php extensions - Generally means that a file is to be included and does not make a standalone script in itself include vs. require - Included file is executed immediately!
upon failure: - include will only produce a warning (E_WARNING) and the script will continue - require will produce a fatal error (E_COMPILE_ERROR) and stop the script - require_once will check if the file has already been included and, if so, will not require it again - Functions cannot be redeclared - To avoid errors on subsequent inclusion
shows - show all errors in dev environment Constant Description E_ERROR Fatal run-time errors, cannot be recovered. Execution of the script is halted. E_WARNING Run-time warnings (non-fatal errors). Execution of the script is not halted. E_PARSE Compile-time parse errors. E_NOTICE Run-time notices. The script encountered something that could be an error. E_ALL All errors and warnings
- show all errors in dev environment ini_set("display_errors", 1); error_reporting(E_ALL); - don’t show but log errors in production env. ini_set("display_errors", 0); ini_set("log_errors", 1); // Define where do you want the log to go ini_set("error_log", "syslog");
- If the form has been submitted - Check input values - If error - Display form with input values remembered - Else - Process form (confirmation page, save to database, etc.) - Else - Display form for the first time (i.e., empty form)
get_value_post("name"); […] // check if the form has been submitted if ($submitted) { // check for errors $errors = array(); […] if (count($errors) > 0) { display_form($name, $email, $year, $month, $day, $sex, $terms, $errors); } else { confirm($name, $email, $year, $month, $day, $sex); } } else { // display form for the first time display_form(); }