Академический Документы
Профессиональный Документы
Культура Документы
@denis_ristic
INTRODUCTION TO
PHP
INTRODUCTION TO PHP 2
PHP SYNTAX
▸ A PHP script starts with <?php and ends with ?> (not always)
▸ Comments in PHP
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple lines
*/
▸ In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are
NOT case-sensitive
PHP VARIABLES
▸ In PHP, a variable starts with the $ sign, followed by the name of the variable
▸ A variable starts with the $ sign, followed by the name of the variable
▸ Variable names are case-sensitive ($age and $AGE are two different
variables)
INTRODUCTION TO PHP 4
PHP VARIABLES
▸ PHP is a Loosely Typed Language
▸ The scope of a variable is the part of the script where the variable can be
referenced/used.
▸ A variable declared outside a function has a global scope and can only be
accessed outside a function
▸ A variable declared within a function has a local scope and can only be accessed
within that function
▸ The global keyword is used to access a global variable from within a function
▸ In PHP there are two basic ways to get output: echo and
print
▸ echo and print are more or less the same, they are both
used to output data to the screen
PHP EXAMPLE
<?php
PHP EXAMPLE
<?php
function myTest() {
$x = 5; // local scope
echo "Variable x inside function is: $x";
}
myTest();
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
INTRODUCTION TO PHP 8
▸ String
▸ Integer
▸ Boolean
▸ Array
▸ Object
▸ NULL
▸ Resource
INTRODUCTION TO PHP 9
$x = 'Hello world!';
echo $x;
$x = 5985;
var_dump($x);
$x = 10.365;
var_dump($x);
$x = true;
var_dump($x);
class Car {
public $model;
function __construct($model) {
$this->model = $model;
}
}
$x = null;
var_dump($x);
INTRODUCTION TO PHP 10
PHP CONSTANTS
CONSTANTS EXAMPLE
<?php
$x = 10;
$y = 5;
$x += $y; // equal to $x = $x + $y
$x -= $y; // equal to $x = $x - $y
$x *= $y; // equal to $x = $x * $y
$x /= $y; // equal to $x = $x / $y
$x %= $y; // equal to $x = $x % $y
$x **= $y; // equal to $x = $x ** $y
INTRODUCTION TO PHP 13
COMPARISON OPERATORS
<?php
$x = 10;
$y = 5;
$x == $y; // equal
$x === $y; // identical (equal + same type)
$x != $y; // not equal
$x !== $y; // not identical
$x <> $y; // not equal
STRING OPERATORS
<?php
CONDITIONALS
<?php
$i = 11;
if ($i < 10) {
echo "Number is smaller than 10!";
} else if ($i < 20) {
echo "Number is smaller than 20!";
} else {
echo "Number is greater or equal to 20!";
}
$favoritecolor = "red";
switch ($favoritecolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
LOOPS
<?php
$x = 1;
while ($x <= 5) {
echo "The number is: $x";
$x++;
}
$x = 6;
do {
echo "The number is: $x";
$x++;
} while ($x <= 5); // this evaluates at the end, so it will always print once
FUNCTIONS
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg(); // call the function
ARRAYS
<?php
$cars_length = count($cars);
for ($x = 0; $x < $cars_length; $x++) {
echo $cars[$x];
}
$cars = [
"BMW" => "350d",
"Mercedes" => [
"E" => [220, 250, 300],
"S" => "500"
]
"Audi" => ["A8", "A6", "A4"]
];
echo "I like " . $cars['BMW'] . ".";
STRINGS
<?php
var_dump(str_split("Hello world!")); // outputs ["H", "e", "l", "l", "o", " ", "w",
"o", "r", "l", "d", “!"]
$number = 123;
printf("With 2 decimals: %1\$.2f, With no decimals: %1\$u", $number);
INTRODUCTION TO PHP 21
SUPERGLOBALS
▸ Several predefined variables in PHP are "superglobals", which means that they are always
accessible, regardless of scope - and you can access them from any function, class or file
without having to do anything special.
$d = strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $d);
$d = strtotime("next Saturday");
echo date("Y-m-d h:i:sa", $d);
$d = strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $d);
FILE HANDLING
<?php
echo readfile("some.txt");
$lines = file('some.txt');
foreach ($lines as $line_num => $line) {
echo "Line #{$line_num} : " . $line . "\n";
}
COOKIES
<?php
if (!isset($_COOKIE["username"])) {
echo "Cookie named username is not set!";
} else {
echo "Cookie username is set and has value " . $_COOKIE["username"];
}
// modify cookie
setcookie("username", "Jane Doe", time() + (86400 * 30), "/");
// delete cookie
setcookie("username", "", time() - 3600);
INTRODUCTION TO PHP 25
SESSIONS
<?php
// Start the session
session_start();
// modify session
$_SESSION["username"] = "Jane Doe";
ERROR HANDLING
<?php
if (!file_exists("welcome.txt")) {
die("File not found");
} else {
$file = fopen(“welcome.txt", "r");
}
$test = 2;
if ($test >= 1) {
trigger_error("Value must be 1 or below");
}
EXCEPTIONS
<?php
function checkNum($number) {
if ($number>1) {
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception
checkNum(2);
try {
checkNum(2);
//If the exception is thrown, this text will not be shown
echo 'If you see this, the number is 1 or below';
} catch (Exception $e) {
echo 'Message: ' .$e->getMessage();
}
function myException($exception) {
echo "<b>Exception:</b> " . $exception->getMessage();
}
set_exception_handler('myException');
INCLUDES
<?php
include 'header.php';
//include will only produce a warning (E_WARNING) and the script will continue
require 'body.php';
// require will produce a fatal error (E_COMPILE_ERROR) and stop the script
require_once 'footer.php';
// the require_once statement is identical to require except PHP will check if the file
has already been included, and if so, not include (require) it again.
INTRODUCTION TO PHP 29
PHP REFERENCES
▸ PHP Manual
▸ http://php.net/manual/en/
▸ https://devzone.zend.com/6/php-101-php-for-the-absolute-
beginner/
▸ http://www.phptherightway.com/
▸ Awesome PHP
▸ https://github.com/ziadoz/awesome-php
INTRODUCTION TO PHP 30
PHP REFERENCES
▸ PHP Best Practices
▸ https://phpbestpractices.org/
▸ PHP FIG
▸ http://www.php-fig.org/
▸ PHP Security
▸ http://phpsecurity.readthedocs.io/en/latest/index.html
▸ http://phpbench.com/
▸ http://sandbox.onlinephpfunctions.com/