ElePHPant7 - Introduction to PHP7

42

Transcript of ElePHPant7 - Introduction to PHP7

Page 1: ElePHPant7 - Introduction to PHP7
Page 2: ElePHPant7 - Introduction to PHP7

Assalamualaikum!Muhammad Hafiz HasanUnit Government Online Services (GOS)[email protected]://my.linkedin.com/in/mhafizhasan

Page 3: ElePHPant7 - Introduction to PHP7

Agenda

✓ Introduction✓ Why upgrade✓ Upgrade checklist✓ Upgrade recommendations✓ Getting help

Page 4: ElePHPant7 - Introduction to PHP7

Introduction

Page 5: ElePHPant7 - Introduction to PHP7

Evolution Timeline

PHP 1.0(1995)

Personal Home Page Tools

(PHP)

PHP 2.0(1997)

PHP 3.0(1998)

PHP 4.0(2000)

Introduce Zend Engine

PHP 5.0(2004)

Zend Engine II

PHP 5.1(2005)

PHP Data Objects (PDO)

PHP 5.3(2009)

Namespace support

PHP 5.4(2012)

Trait support

PHP 7.0(2015)

Zend Engine III

source: https://en.wikipedia.org/wiki/PHP

Page 6: ElePHPant7 - Introduction to PHP7

Why UpgradePHP5 to PHP7

Page 7: ElePHPant7 - Introduction to PHP7

Why upgrade? PHP5 to PHP7

PHP 5is now 13 years old

+ PHP 7

is mature enough

Page 8: ElePHPant7 - Introduction to PHP7

Why upgrade? PHP5 to PHP7

Upshift in performance withPHPNG

(PHP Next Generation)

=at par with

Facebook HHVM(HipHop Virtual Machine)

Page 9: ElePHPant7 - Introduction to PHP7

Why upgrade? PHP5 to PHP7

Run up to 3x Magento transactions on the same hardware

With execution time more than twice as fast compared to PHP 5.6 and 30% lower memory consumption - servers running PHP 7 will be able to serve up to 3x as many requests as those running PHP 5.6.

source: http://www.zend.com/en/resources/php7_infographic

Page 10: ElePHPant7 - Introduction to PHP7

Why upgrade? PHP5 to PHP7

Drupal 8 runs 72% faster with PHP 7

source: http://www.zend.com/en/resources/php7_infographic

Page 11: ElePHPant7 - Introduction to PHP7

Why upgrade? PHP5 to PHP7

WordPress Screams on PHP 7. You’ll need less

servers to serve the same amount of users!

One WordPress request on PHP 5.6 executes just under

100M CPU instructions, while PHP 7 only executes 25M to

do the same job.

source: http://www.zend.com/en/resources/php7_infographic

Page 12: ElePHPant7 - Introduction to PHP7

Why upgrade? PHP5 to PHP7

We also tested how various PHP frameworks perform

under PHP 7

source: http://www.zend.com/en/resources/php7_infographic

Page 13: ElePHPant7 - Introduction to PHP7

Why upgrade? PHP5 to PHP7

Significant memory saving

Page 14: ElePHPant7 - Introduction to PHP7
Page 15: ElePHPant7 - Introduction to PHP7
Page 16: ElePHPant7 - Introduction to PHP7

Why upgrade? PHP5 to PHP7

Introduction ofScalar Type Hints & Return Types

(string, int, float, bool)

declare(strict_types=1);

function add(int $a, int $b) {

return $a + $b;}

var_dump(add(1,2));

declare(strict_types=1);

function add(int $a, int $b) : int {

return (string)($a + $b);}

var_dump(add(1,2));

Scalar type hints Return type declaration

Page 17: ElePHPant7 - Introduction to PHP7

Why upgrade? PHP5 to PHP7

Introduction ofSpaceship Operator

<=>

Page 18: ElePHPant7 - Introduction to PHP7

Why upgrade? PHP5 to PHP7

Introduction ofNull Coalesce Operator

??

$name = isset($firstname) ? $firstname : “Guest”;

$name = $firstname ?? “Guest”;

$name = $firstname ?? $username ?? $nickname “Guest”;

Page 19: ElePHPant7 - Introduction to PHP7

Why upgrade? PHP5 to PHP7

Introduction ofAnonymous Classes

class className {

// defined properties and methods

};

$object = new className( 'arguments' );

$object = new class( 'arguments' ) {

// defined properties and methods

};

Page 20: ElePHPant7 - Introduction to PHP7

Upgrade Checklist

Page 21: ElePHPant7 - Introduction to PHP7

Upgrade checklist

☑ Removed functions☑ Removed INI directives

Page 22: ElePHPant7 - Introduction to PHP7

Removed functions : ereg*

ereg* replace with PCRE (Perl Compatible Regular Expression)

✓ preg_filter✓ preg_grep✓ preg_last_error✓ preg_match_all✓ preg_match✓ preg_quote✓ preg_replace_callback_array✓ preg_replace_callback✓ preg_replace✓ preg_split

✘ ereg_replace

✘ ereg

✘ eregi_replace

✘ eregi

✘ split

✘ spliti

✘ sql_regcase

Page 23: ElePHPant7 - Introduction to PHP7

✘ mysql_affected_rows

✘ mysql_client_encoding

✘ mysql_close

✘ mysql_connect

✘ mysql_create_db

✘ mysql_data_seek

✘ mysql_db_name

✘ mysql_db_query

✘ mysql_drop_db

✘ mysql_errno

✘ mysql_error

✘ mysql_escape_string

✘ mysql_fetch_array

✘ mysql_fetch_assoc

✘ mysql_fetch_field

✘ mysql_fetch_lengths

✘ mysql_fetch_object

✘ mysql_fetch_row

Removed functions : mysql_*✘ mysql_field_flags

✘ mysql_field_len

✘ mysql_field_name

✘ mysql_field_seek

✘ mysql_field_table

✘ mysql_field_type

✘ mysql_free_result

✘ mysql_get_client_info

✘ mysql_get_host_info

✘ mysql_get_proto_info

✘ mysql_get_server_info

✘ mysql_info

✘ mysql_insert_id

✘ mysql_list_dbs

✘ mysql_list_fields

✘ mysql_list_processes

✘ mysql_list_tables

✘ mysql_num_fields

✘ mysql_num_rows

✘ mysql_pconnect

✘ mysql_ping

✘ mysql_query

✘ mysql_real_escape_string

✘ mysql_result

✘ mysql_select_db

✘ mysql_set_charset

✘ mysql_stat

✘ mysql_tablename

✘ mysql_thread_id

✘ mysql_unbuffered_query

Page 24: ElePHPant7 - Introduction to PHP7

Removed functions : mysql_*

mysql_* < MySQLi < PDO

PHP5.5.0 PHP7

Page 25: ElePHPant7 - Introduction to PHP7

Why PHP Data Object - PDO

Vendor flexibilityReduced learning curve

Named parameters

Page 26: ElePHPant7 - Introduction to PHP7

Why PHP Data Object - PDO

Vendor flexibility

● PostgreSQL

● SQLite

● MySQL

● Oracle

● ODBC

● MS SQLServer & Azure

● Firebird

● Informix

● IBM DB2

● Sybase

● Cubrid

● 4D

Page 27: ElePHPant7 - Introduction to PHP7

Installation

Ubuntusudo apt-get install php5-mysql

CentOSyum install php-pdo php-mysql

Page 28: ElePHPant7 - Introduction to PHP7

Example : Connection

<?php

// MySQL$dbh = new PDO(“mysql:host=$host;dbname=$dbname, $user, $pass”);

// MSSQL$dbh = new PDO(“mssql:host=$host;dbname=$dbname, $user, $pass”);

// Sybase$dbh = new PDO(“sybase:host=$host;dbname=$dbname, $user, $pass”);

// SQLite$dbh = new PDO(“sqlite:my/database/path/database.db”);

Page 29: ElePHPant7 - Introduction to PHP7

Example : Named placeholders

<?php

// No placeholders$sth = $dbh->prepare(“INSERT INTO users (name, email) VALUES ($name, $email)”);

// Unnamed placeholders$sth = $dbh->prepare(“INSERT INTO users (name, email) VALUES (?, ?)”);$sth->bindParam(1, $name);$sth->bindParam(2, $email);

// Named placeholders$sth = $dbh->prepare(“INSERT INTO users (name, email) VALUES (:name, :email)”);$sth->bindParam(‘:name’, $name);$sth->bindParam(‘:email’, $email);

// Execute$sth->execute();

Page 30: ElePHPant7 - Introduction to PHP7

Removed functions : mssql_*

✘ mssql_bind

✘ mssql_close

✘ mssql_connect

✘ mssql_data_seek

✘ mssql_execute

✘ mssql_fetch_array

✘ mssql_fetch_assoc

✘ mssql_fetch_batch

✘ mssql_fetch_field

✘ mssql_fetch_object

✘ mssql_fetch_row

✘ mssql_field_length

✘ mssql_field_name

✘ mssql_field_seek

✘ mssql_field_type

✘ mssql_free_result

✘ mssql_free_statement

✘ mssql_free_statement

✘ mssql_get_last_message

✘ mssql_guid_string

✘ mssql_init

✘ mssql_min_error_severity

✘ mssql_min_message_severity

✘ mssql_next_result

✘ mssql_num_fields

✘ mssql_num_rows

✘ mssql_pconnect

✘ mssql_query

✘ mssql_result

✘ mssql_rows_affected

✘ mssql_select_db

Page 31: ElePHPant7 - Introduction to PHP7

Removed INI directives : always_populate_raw_post_data

$username = $_POST[‘username’];

echo $username;

$post = file_get_contents(“php://input”);

echo $post[‘username’];

$HTTP_RAW_POST_DATA php://input

Page 32: ElePHPant7 - Introduction to PHP7

Removed INI directives : asp_tags

<%...%> <%=...%>

<script language=”php”>...</script>

<?php...?> <?=...?>

Page 33: ElePHPant7 - Introduction to PHP7
Page 34: ElePHPant7 - Introduction to PHP7
Page 35: ElePHPant7 - Introduction to PHP7

Upgrade recommendations

Page 36: ElePHPant7 - Introduction to PHP7

TL;DRToo Long; Didn’t Read

Page 37: ElePHPant7 - Introduction to PHP7

Framework● Structured code and file organisation● Utilities, libraries & helpers● Design pattern (M-V-C)● Security● Rapid application development● Community support● Fun

Page 38: ElePHPant7 - Introduction to PHP7

Popular PHP frameworks - github

Page 39: ElePHPant7 - Introduction to PHP7

Popular PHP framework - survey

source: https://www.sitepoint.com/best-php-framework-2015-sitepoint-survey-results/

Page 40: ElePHPant7 - Introduction to PHP7

Your best friends

http://www.phptherightway.com/

Page 41: ElePHPant7 - Introduction to PHP7

Last but not least,

Page 42: ElePHPant7 - Introduction to PHP7

Thank youAny questions?