Things you should know when upgrading to PHP 7

If you are PHP developer then you know that PHP 7 is available and you can use it in your projects. PHP 7 comes with a lot of nice new features, but a lot of people are having headache because of backward compatibility. In this post, I will write about things that are not compatible with PHP 7 and you should be careful about.

Probably you are asking yourself, what happened with PHP 6, why PHP jumped from PHP 5 to PHP 7 (they tried to copy Microsoft who jumped from Windows 8 to Windows 10)?

If you didn’t know, PHP 6 existed as a real project which was developed between 2005 and 2010, but it was never officially released. Some features from PHP 6 were later on introduced in PHP 5.3 like closures and namespaces, but PHP internals decided that they should immediately jump to PHP 7.

Timelines

Now when PHP 7 is released, the question is what will happen with PHP 5.5 and 5.6. We will see the end of life for PHP 5.5 on July 10, 2016. When we are talking about PHP 5.6 then only security fixes are going to be available until August 2016 and its end of life is scheduled to take place a year later.

All versions prior to 5.5 has already reached their end of life, without receive even security fixes.

Action: If you are working with PHP < 5.5 you should think about installing PHP 5.6 or even PHP 7 immediately.

Deprecated features

There are a lot of functions that were marked as deprecated in PHP 5.x. If you want to upgrade to PHP 7, you should know that these deprecated functions are removed and not supported anymore. A feature is marked as deprecated to warn developers that it will be removed in some future version of a language. If you are seeing E_DEPRECATED error across you project, then you are using of the deprecated features (functions).

Alternative PHP tags

You probably know about standard PHP tags like <?php or <?= but there are other tags that you may not even be aware of. Before, you could enclose your PHP code in <script> tags like it is demonstrated below:

An old PHP opening tags

PHP

1

2

3

<script language="php">

echo"Hello Code Epicenter";

</script>

If you have worked in ASP then you know that you could enclose your PHP code inside ASP Script tags:

ASP Opening tags

PHP

1

2

<%$message="Hello Code Epicenter";%>

<%=$message%>

If you are using any of these, you will have to change them.

Multiple default cases in switch statement

If you didn’t know, PHP language specification had a bug! You could define multiple default: cases in a single switch statement, but only last default: case would execute which could lead to bugs. For example:

Multiple default statements in switch statement

PHP

1

2

3

4

5

6

7

8

switch($variable){

default:

echo"Hello Code Epicenter 1";

break;

default:

echo"Hello Code Epicenter 2";

break;

}

Only the last default: case would execute. This is removed in PHP 7 and you will get a fatal error if you try to write similar code.

The Original MySQL extension is removed

The original ext/mysql extension is removed and not supported in PHP 7, which means that all mysql_ functions are not supported anymore. You realize now that this is the largest change to your existing code. If you want to work with PHP 7, then you have to change all your mysql_ functions to procedural mysqli_ functions or to an object oriented PDO.

Migration to mysqli

Fortunately myslq_ and mysqli_ functions are very similar. The only difference is in letter ‘i’ that you should append to your mysql_ functions. In most cases you will just have to change function name. Here is the list of the incompatible functions between mysql_ and mysqli_:

List of incompatible functions mysql

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

mysql_client_encoding()

mysql_list_dbs()(useSHOW DATABASES query)

mysql_db_name()

mysql_list_fields()

mysql_db_query()

mysql_list_processes()(useSHOWPROCESSLIST query)

mysql_dbname()

mysql_list_tables()(useSHOWTABLES query)

mysql_field_flags()

mysql_listdbs()(useSHOW DATABASES query)

mysql_field_len()

mysql_listfields()

mysql_field_name()

mysql_listtables()(useSHOWTABLES query)

mysql_field_table()

mysql_numfields()

mysql_field_type()

mysql_numrows()(usemysqli_num_rows()instead)

mysql_fieldflags()

mysql_pconnect()(appendp:tothehostname passed tomysqli_connect())

mysql_fieldlen()

mysql_result()

mysql_fieldname()

mysql_selectdb()(usemysqli_select_db()instead)

mysql_fieldtable()

mysql_table_name()

mysql_fieldtype()

mysql_tablename()

mysql_freeresult()(usemysqli_free_result()instead)

mysql_field_name()

mysql_listtables()(useSHOWTABLES query)

mysql_field_table()

mysql_numfields()

mysql_field_type()

mysql_numrows()(usemysqli_num_rows()instead)

mysql_fieldflags()

mysql_pconnect()(appendp:tothehostname passed tomysqli_connect())

mysql_fieldlen()

mysql_result()

mysql_fieldname()

mysql_selectdb()(usemysqli_select_db()instead)

mysql_fieldtable()

mysql_table_name()

mysql_fieldtype()

mysql_tablename()

mysql_freeresult()(usemysqli_free_result()instead)

mysql_unbuffered_query()

ext/mysqli supports both functional and object oriented approach API. Which one to use is your decision, but I suggest you to use object oriented API.

PDO API

Instead of ext/mysqli you can use ext/pdo which stands for PHP Data Objects. PDO provides object oriented approach and gives you some additional functionalities likes prepared statements and security.

PDO helps you to connect to variety of databases using consistent API, and you only have to learn one API to work with many databases. Here you can find more information about PDO.

Here is a simple example:

PDO example PHP

PHP

1

2

3

4

5

6

7

8

9

10

11

12

<?php

try{

$dbh=newPDO('mysql:host=localhost;dbname=test',$user,$pass);

foreach($dbh->query('SELECT * from FOO')as$row){

print_r($row);

}

$dbh=null;

}catch(PDOException$e){

print"Error!: ".$e->getMessage()."<br/>";

die();

}

?>

http://code-epicenter.com/things-you-should-know-when-upgrading-to-php-7/Things you should know when upgrading to PHP 7http://code-epicenter.com/wp-content/uploads/2016/01/Upgrading-to-php-7.pnghttp://code-epicenter.com/wp-content/uploads/2016/01/Upgrading-to-php-7-150x150.png2016-01-09T13:48:13+00:00Amir DuranPHPProgrammingPHP,PHP 7If you are PHP developer then you know that PHP 7 is available and you can use it in your projects. PHP 7 comes with a lot of nice new features, but a lot of people are having headache because of backward compatibility. In this post, I will write about things...Amir DuranAmirDuranamir.duran@gmail.comAdministratorAmir Duran is software engineer who currently lives and works in Germany. He obtained Masters degree diploma on Faculty of Electrical Engineering in Sarajevo, department Computer science. With good educational background he is specialized in designing and implementing a full-stack web based applications.Code Epicenter

Related Posts

About Amir Duran

Amir Duran is software engineer who currently lives and works in Germany. He obtained Masters degree diploma on Faculty of Electrical Engineering in Sarajevo, department Computer science. With good educational background he is specialized in designing and implementing a full-stack web based applications.