Share
Embed
1. Introduction and Overview
A brief introduction to the basics of PHP and building applications for the dynamic web.
Ben Major
March 07, 2016
Transcript
35 slides
-
About Me • Currently working for WeddingVenues.com Receives around 1,200,000
unique visits per month > 35,000 pages • Working with PHP professionally for 10+ years • 29,000+ reputation on StackOverflow (@benm) • Open Source advocate; find me on GitHub (benmajor88) -
Course Overview 1. PHP Overview & Basics 2. Functions 3.
Loops 4. MySQL and data-rich websites 5. Security and Good Practice 6. Working with JSON, XML and APIs 7. Object-Oriented PHP Development 8. Conclusion -
Course Overview 1. PHP Overview & Basics 2. Functions 3.
Loops 4. MySQL and data-rich websites 5. Security and Good Practice 6. Working with JSON, XML and APIs 7. Object-Oriented PHP Development 8. Conclusion -
The “Stack” • Building blocks of the dynamic web •
LAMP: • Linux - Server Operating System • Apache - Server Software • MySQL: Database Engine • PHP: Scripting Language -
MAMP • Windows and Mac friendly • Includes Apache and
MySQL • Bundled with phpMyAdmin • Much faster for development • Download online: https://www.mamp.info/en/ -
Komodo Edit IDE • Syntax highlighting • Autocomplete • Code
hinting • Supports OOP PHP and jQuery -
So, what is PHP? • Recursive acronym — PHP: Hypertext
Preprocessor. • HTML-embedded scripting language, not a programming language! • Multi-paradigm: procedural and object-oriented. • Loosely-typed, highly coupled. • Syntax largely borrowed and adapted from C, Perl and Java. • Current stable version: 5.6.19. -
Basic Syntax • Code must be wrapped inside an opening
and closing tag: <?php and ?> respectively. • Files must have a .php extension. • Lines should be terminated with the ; literal (except in some case). • Can be wrapped inside of HTML: <p>My paragraph here!</p> <?php /* Some PHP code here. */ ?> <p>Another paragraph!</p> -
Data Types $foo = ‘bar’;
// String $foo = 1; // Integer $foo = 1.2; // Float (floating point number) $foo = true; // Boolean $foo = array(1,2); // Array $foo = new stdClass(); // Object $foo = null; // Null -
Variables • Store values persistently during the script’s execution. •
Defined using the $ literal, for example: $var, $myVar… • Cannot begin with a numerical character, or contain special punctuation marks (bangs, colons or other reserved chars), for example: $123 Bad! $var123 OK! $!myVar Bad! $myVar! Bad! $?myVar Bad! $_myVar OK! -
Comments • Comments help to add context and explain what
your code does. • Ignored by the PHP parser. • We can define comments in three ways: • // Using two forward slashes (limited to only one line) • /* A forward slash and an asterisk Can be multiline. Have to be terminated with an asterisk and a forward slash at the end */ • # Using a pound # Sometimes cleaner, but limited to a single line -
Outputting Data • As well as controlling variables, we need
to display and output them, too. • Achieved using language structs: • echo ‘Hello, World!’; • print ‘Hello, World!’; • Or functions: • echo( ‘Hello, World!’ ); • print( ‘Hello, World!’ ); -
Strings 101 • Strings are wrapped in single (‘string’) or
double (“string”) quotes • Double quotes can be used to concatenate a string with another variable, for example: $myString = “string $i”; (will append $i to the string) $myString = 'string $i’; ($myString will be string $i) • We must escape quote literals of the same type within a string using a backslash (\), for example: $myString = “string \”$i\”. Some more.”; -
Strings 102 • We can concatenate a string from other
strings or variables, use the . or , literal: • $myString = “string ”.”appended”; • $myString = “string ”,“appended”; • We can also append characters to a string using the .= syntax: • $myString = “string”; $myString.= ‘ appended’; -
Strings 103 • In PHP, all strings are also arrays
of characters. • Useful when you need to identify the character at a given index • $myString = “string”; $myChar = $myString[0]; // “s” $myChar = $myString[5]; // “g” -
Strings 104 • String variables can be used as variable
names! • Called ‘variable variables’. • Defined using the $$ syntax: • $myString = ‘foo’; $$myString = ‘bar’; // Create $foo variable echo $foo // Will output ‘bar’ -
Useful String Functions • strtoupper( $string ) Converts all chars
of $string to uppercase. • strtolower( $string ) Converts all chars of $string to lowercase. • ucwords( $string ) Will detect individual words in $string and covert the first character of each to uppercase. • substr( $string, INDEX, LENGTH ) Extracts another string from $string at the given INDEX with a length of LENGTH -
Integers 101 • Integers represent whole numbers only. • Signed
integers only (e.g. 1 and -‐1). • They have a minimum and maximum value, which is dependent upon the platform and hardware: • A 32-bit machine which uses signed integers has a max of ~ 2,000,000,000,000 (and a minimum of -2b) • A 64-bit machine which uses signed integers has a max of ~ 9e18 • Windows installations (prior to PHP7) always used 32-bit signed ints. -
Integers 102 • By default, all integers in PHP are
decimal (or base-10). • We can also work with other bases: • $i = 123; // Decimal • $i = -‐123; // Negative • $i = 0123; // Octal (== 83) • $i = 0x1A; // Hexadecimal (== 26) • $i = 0b11111111; // Binary (== 255) -
Useful Integer Functions • min( $int1, $int2, $int3, … )
Returns the minimum integer in the given collection. • max( $int1, $int2, $int3, … ) Returns the maximum integer in the given collection. • intval( $var ) Returns the integer value of $var. • abs( $var ) Returns the absolute value of $var: • $abs = abs( -‐1 ); // $abs = 1; -
Floats • Floating point integers allow us to define values
with decimal places. • They are always real numbers. • Numerous syntax for defining floats: • $a = 1.234; // 1.234 • $b = 1.2e3; // 1200 • $c = 7E-‐10; // Same as writing: 7-‐10 : 0.0000000007 -
Useful Float Functions • round( $float ) Rounds $float to
the nearest integer value. • floor( $float ) Rounds $float down to the nearest integer value. • ceil( $float ) Rounds $float up to the nearest integer value. • floatval( $var ) Returns the floating point value of $var: • $float = floatval( ‘1.2345the’ ); // $float = 1.2345; -
Booleans • Boolean values are either true or false:
• $truthy = true; • $falsey = false; • Integers can sometimes be treated as booleans; be careful! • $int = 1; // This is the same as $int = true; • $int = 0; // This is the same as $int = false; -
Performing Operations • We can perform operations on variables in
PHP. • Mathematical operations (assume that $i is set to 2): • $i = $i + 2; // $i now = 4 $i+= 2; // $i now = 4 • $i = $i -‐ 1; // $i now = 1 $i-‐= 1; // $i now = 1 • $i = $i * 2; // $i now = 4 $i*= 2; // $i now = 4 • $i = $i / 2; // $i now = 1 $i/= 2; // $i now = 1 • $i = $i % 2; // $i now = 0 $i%= 2; // $i now = 0 -
Type Juggling • Allows us to modify the variable type
‘on the fly’. • PHP parser will automatically handle the type. • $str = 0.’ string’; // Converts 0 into a string: ‘0 string’ • $total = ‘2’ + 1; // Total = 3 • $total = ‘2.1’ + ‘2.3’; // Total = 5.4 • $myVar = ‘1’; $total+= $myVar; // Also valid, increments $total by 1 -
Type Casting • Useful if we need to explicitly set
the data type. • We can tell PHP to cast one type to another: • $integer = (int) ‘1’; // $integer is an integer with value 1 • $string = (string) 123; // $string is a string with value ‘123’ • $absInt = (int) ‘1.23’; // $absInt is an integer with value 1 -
Arrays 101 • Hold a collection of values, which can
be of mismatched types. • Can be 0-indexed, or associative: • Array values are accessed using the square bracket notation ([]). • May also contain other arrays, some called a multi-dimensional array. • Useful for storing options or other persistent data that needs to be stored in a structured manner. -
Arrays 102 0-indexed arrays: • When we don’t specify keys
for the array, the values are accessed using their index within it. • The index is always 0-based (start counting from 0). • $arr = array( ‘foo’, ‘bar’, ‘hello’, ‘world’ ); • $foo = $arr[0]; // $foo = ‘foo’ $bar = $arr[1]; // $bar = ‘bar’ $helloWorld = $arr[2].$arr[3]; // $helloWorld = ‘helloworld’ -
Arrays 103 Associative arrays: • We can specify array keys
to add meaning to the values stored within it. • Values are then accessed by their respective keys: • $arr = array( ‘foo’ => ‘bar’, ‘hello’ => ‘world’ ); • $foo = $arr[‘foo’]; // $foo = ‘bar’ $hello = $arr[‘hello’]; // $hello = ‘world’ -
Arrays 104 Multi-dimensional arrays: • $arr = array(
array( ‘foo’ => ‘bar’, ‘foo2’ => ‘bar2’ ), array( ‘foo’ => ‘bar’, ‘foo2’ => ‘bar2’ ) ); -
Modifying Arrays • We can also modify and add to
arrays using square bracket notation. • $myArray = array(); $myArray[] = ‘foo’; $myArray[] = ‘bar’; // $myArray now looks like: array(‘foo’, ‘bar’) -
Useful Array Functions • count( $array ) Returns the number
of elements inside of $array. • array_push( $array, $toAdd ) Pushes $toAdd to the end of $array. • array_pop( $array ) Removes the last element from $array and returns it. Modifies $array by reference: • $myArr = array( ‘foo’, ‘bar’ ); $foo = array_pop( $myArr ); // $foo = ‘bar’ // $myArr = array(‘foo’); -
• Code samples are available on GitHub at the following
URL: https://github.com/benmajor/php-lessons • Slideshows are available on SpeakerDeck at the following URL: https://speakerdeck.com/benmajor • If you need any help, email me: [email protected] Questions?