Want to learn PHP? This beginner level session will introduce you to PHP and its syntax. Learn how to make a 'Hello World' PHP page, create a class, build a method, declare and use variables, and write control structures (if, else, foreach, switch, etc.). Get excited to start developing in PHP and leave with a foundation to go and build amazing PHP applications on.
3. Starting Out With PHP
• PHP is a useless language to learn
• PHP is hated by all developers
• PHP is insecure, slow, and not modern
• PHP is bad
4. Starting Out With PHP
• Lets developers make mistakes
- $password = md5(‘password123’);
- eval($userInput);
5. Starting Out With PHP
• Lets developers make mistakes
- echo $$variable . $$$variable;
- if (‘true’ == false) {…}
6. Starting Out With PHP
• Lets developers make mistakes
- $sql = “DELETE FROM users WHERE id =
{$_GET[‘id’]}”;
- “INSERT INTO some_data VALUES (NULL,
‘{$_POST[‘name’]}’);
7. Starting Out With PHP
• Lets developers make mistakes
- file_get_contents($randomUrl);
- $GLOBALS[‘everything’] = $everything;
8. Starting Out With PHP
• Lets developers make mistakes
- $oneCase + $two_case + $ThreeCase_More
- if (empty($allTheThings)) {…}
9. Starting Out With PHP
• Lets developers make mistakes
- if ($a < $b) {
if ($test == true) {
if ($number < 100) {
if (strpos($string, ‘abc’) != 0) {
if ($nestMore == ‘Yes’) {
echo ‘Nesting if statements is great!’;
}
}
}
}
}
10. Starting Out With PHP
• Lets developers make mistakes
- Works on my box!
11.
12. Starting Out With PHP
• PHP runs over 83% of the web
• PHP has an active community
• PHP is secure, fast, and constantly being updated
• PHP is good
13. About Mark Niebergall
• PHP since 2005
• Masters degree in MIS
• Senior Software Engineer
• Drug screening project
• UPHPU President
• CSSLP, SSCP Certified and SME
• Drones, fishing, skiing, father of 5 boys
14. Starting Out With PHP
• What is PHP
• Basic Syntax
• Language Features
• PHP Ecosystem
18. What is PHP
<?php
class HelloWorld
{
public function sayHello($name)
{
echo ‘Hello ‘ . $name;
}
}
$helloWorld = new HelloWorld;
$helloWorld->sayHello(‘OpenWest Attendees’);
19. What is PHP
• Personal Home Page
• Rasmus Lerdorf in 1994
• PHP: Hypertext Preprocessor
20. What is PHP
• Programming language designed for the Internet
• Server-side scripting language
• General-purpose programming language
21. What is PHP
• Facebook
• Slack
• Tumblr
• Wikipedia
• News sites
• Blogs
22. What is PHP
• Open source
- Free to use
- Anyone can contribute
- Source is public
40. Basic Syntax
<?php
class Cat {
protected $name;
public function __construct($name) {
$this->name = $name;
}
public function pet() {
return ‘purr’;
}
}
41. Basic Syntax
<?php
class Cat
{
// Anything can use public
public function pet() {…}
// Cat class and anything inheriting can use protected
protected function speak() {…}
/* only Cat class can use private */
private function _ignoreHuman() {…}
}
42. Basic Syntax
<?php
abstract class Pet {
protected $name;
abstract public function feed(Food $food);
}
class Cat extend Pet {
public function feed(Food $food) {
return $this->stare($food);
}
protected function stare($thing) {}
}
43. Basic Syntax
<?php
namespace AnimalPet;
use AnimalHuman;
class Cat
{
public function pet() {…}
protected function speak() {…}
private function _ignoreHuman(Human $human) {…}
}
55. Language Features
• Class
- class Cat extends Pet implements HousePet {
use MouseCatcherTrait;
const FAMILY = ‘Feline’;
protected $name;
public function __construct($dependencies) {…}
public static function independentAction() {…}
protected function eatFood(Food $food) : Food {…}
private function _controlHuman(Human $human) {…}
}
56. Language Features
• Abstract
- Class can extend one abstract at a time
- Can have parent, grandparent, etc.
- Mixed functions filled in and others not
- abstract class Pet {
abstract protected function eatFood(Food $food) :
Food {};
}
57. Language Features
• Interfaces
- Contract
- Class can implement many interfaces
- interface Port {
public function connect(Connection $connection);
}
58. Language Features
• Trait
- Common characteristics
- trait SomeCharacteristic {
public function doSomething() {…}
}
- class Thing {
use SomeCharacteristic;
}
59. Language Features
• Exceptions
- try {
…
throw new Exception(‘Error message’);
…
throw new CustomException(‘Failed’);
} catch (Exception | CustomException $e) {
echo $e->getMessage();
}
60. Language Features
• Magic methods
- Called automatically
- __construct when instantiated
- __set, __get
- __destruct, __call, __callStatic, __isset, __unset,
__sleep, __wakup, __toString, __invoke, __set_state,
__clone, __debugInfo
61. Language Features
• Namespaces
- namespace AnimalMammalFeline;
use AnimalFish;
class Cat
{
public function eat(Fish $fish) {…}
}
…
new AnimalMammalFelineCat;
62. Language Features
• Libsodium
- First programming language with modern security
library built-in
- Hash
- Encrypt
- Keys
- Random