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)
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
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
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.
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>
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!
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
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 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’;
of characters. • Useful when you need to identify the character at a given index • $myString = “string”; $myChar = $myString[0]; // “s” $myChar = $myString[5]; // “g”
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 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.
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;
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;
• $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;
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
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.
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’
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’
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’);
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?