KEMBAR78
ElePHPant7 - Introduction to PHP7 | PDF
Assalamualaikum!
Muhammad Hafiz Hasan
Unit Government Online Services (GOS)
mhafiz@jpm.gov.my
https://my.linkedin.com/in/mhafizhasan
Agenda
✓ Introduction
✓ Why upgrade
✓ Upgrade checklist
✓ Upgrade recommendations
✓ Getting help
Introduction
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
Why Upgrade
PHP5 to PHP7
Why upgrade? PHP5 to PHP7
PHP 5
is now 13 years old
+
PHP 7
is mature enough
Why upgrade? PHP5 to PHP7
Upshift in performance with
PHPNG
(PHP Next Generation)
=
at par with
Facebook HHVM
(HipHop Virtual Machine)
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
Why upgrade? PHP5 to PHP7
Drupal 8 runs 72% faster
with PHP 7
source: http://www.zend.com/en/resources/php7_infographic
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
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
Why upgrade? PHP5 to PHP7
Significant memory saving
Why upgrade? PHP5 to PHP7
Introduction of
Scalar 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
Why upgrade? PHP5 to PHP7
Introduction of
Spaceship Operator
<=>
Why upgrade? PHP5 to PHP7
Introduction of
Null Coalesce Operator
??
$name = isset($firstname) ? $firstname : “Guest”;
$name = $firstname ?? “Guest”;
$name = $firstname ?? $username ?? $nickname “Guest”;
Why upgrade? PHP5 to PHP7
Introduction of
Anonymous Classes
class className {
// defined properties and methods
};
$object = new className( 'arguments' );
$object = new class( 'arguments' ) {
// defined properties and methods
};
Upgrade Checklist
Upgrade checklist
☑ Removed functions
☑ Removed INI directives
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
✘ 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
Removed functions : mysql_*
mysql_* < MySQLi < PDO
PHP5.5.0 PHP7
Why PHP Data Object - PDO
Vendor flexibility
Reduced learning curve
Named parameters
Why PHP Data Object - PDO
Vendor flexibility
● PostgreSQL
● SQLite
● MySQL
● Oracle
● ODBC
● MS SQLServer & Azure
● Firebird
● Informix
● IBM DB2
● Sybase
● Cubrid
● 4D
Installation
Ubuntu
sudo apt-get install php5-mysql
CentOS
yum install php-pdo php-mysql
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”);
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();
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
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
Removed INI directives : asp_tags
<%...%> <%=...%>
<script language=”php”>...</script>
<?php...?> <?=...?>
Upgrade
recommendations
TL;DR
Too Long; Didn’t Read
Framework
● Structured code and file organisation
● Utilities, libraries & helpers
● Design pattern (M-V-C)
● Security
● Rapid application development
● Community support
● Fun
Popular PHP frameworks - github
Popular PHP framework - survey
source: https://www.sitepoint.com/best-php-framework-2015-sitepoint-survey-results/
Your best friends
http://www.phptherightway.com/
Last but not least,
Thank you
Any questions?

ElePHPant7 - Introduction to PHP7

  • 2.
    Assalamualaikum! Muhammad Hafiz Hasan UnitGovernment Online Services (GOS) mhafiz@jpm.gov.my https://my.linkedin.com/in/mhafizhasan
  • 3.
    Agenda ✓ Introduction ✓ Whyupgrade ✓ Upgrade checklist ✓ Upgrade recommendations ✓ Getting help
  • 4.
  • 5.
    Evolution Timeline PHP 1.0 (1995) PersonalHome 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
  • 6.
  • 7.
    Why upgrade? PHP5to PHP7 PHP 5 is now 13 years old + PHP 7 is mature enough
  • 8.
    Why upgrade? PHP5to PHP7 Upshift in performance with PHPNG (PHP Next Generation) = at par with Facebook HHVM (HipHop Virtual Machine)
  • 9.
    Why upgrade? PHP5to 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
  • 10.
    Why upgrade? PHP5to PHP7 Drupal 8 runs 72% faster with PHP 7 source: http://www.zend.com/en/resources/php7_infographic
  • 11.
    Why upgrade? PHP5to 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
  • 12.
    Why upgrade? PHP5to PHP7 We also tested how various PHP frameworks perform under PHP 7 source: http://www.zend.com/en/resources/php7_infographic
  • 13.
    Why upgrade? PHP5to PHP7 Significant memory saving
  • 16.
    Why upgrade? PHP5to PHP7 Introduction of Scalar 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
  • 17.
    Why upgrade? PHP5to PHP7 Introduction of Spaceship Operator <=>
  • 18.
    Why upgrade? PHP5to PHP7 Introduction of Null Coalesce Operator ?? $name = isset($firstname) ? $firstname : “Guest”; $name = $firstname ?? “Guest”; $name = $firstname ?? $username ?? $nickname “Guest”;
  • 19.
    Why upgrade? PHP5to PHP7 Introduction of Anonymous Classes class className { // defined properties and methods }; $object = new className( 'arguments' ); $object = new class( 'arguments' ) { // defined properties and methods };
  • 20.
  • 21.
    Upgrade checklist ☑ Removedfunctions ☑ Removed INI directives
  • 22.
    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
  • 23.
    ✘ 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
  • 24.
    Removed functions :mysql_* mysql_* < MySQLi < PDO PHP5.5.0 PHP7
  • 25.
    Why PHP DataObject - PDO Vendor flexibility Reduced learning curve Named parameters
  • 26.
    Why PHP DataObject - PDO Vendor flexibility ● PostgreSQL ● SQLite ● MySQL ● Oracle ● ODBC ● MS SQLServer & Azure ● Firebird ● Informix ● IBM DB2 ● Sybase ● Cubrid ● 4D
  • 27.
    Installation Ubuntu sudo apt-get installphp5-mysql CentOS yum install php-pdo php-mysql
  • 28.
    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”);
  • 29.
    Example : Namedplaceholders <?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();
  • 30.
    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
  • 31.
    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
  • 32.
    Removed INI directives: asp_tags <%...%> <%=...%> <script language=”php”>...</script> <?php...?> <?=...?>
  • 35.
  • 36.
  • 37.
    Framework ● Structured codeand file organisation ● Utilities, libraries & helpers ● Design pattern (M-V-C) ● Security ● Rapid application development ● Community support ● Fun
  • 38.
  • 39.
    Popular PHP framework- survey source: https://www.sitepoint.com/best-php-framework-2015-sitepoint-survey-results/
  • 40.
  • 41.
  • 42.