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
- ďŹle_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 CertiďŹed and SME
⢠Drones, ďŹshing, 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) {}â¨
}
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 ďŹlled 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 $ďŹsh) {âŚ}â¨
}â¨
âŚâ¨
new AnimalMammalFelineCat;