Learn How to Use Changed functions, new syntax and Global Constants in PHP7

0
1356
Learn-How-to-Use-Changed-functions,-new-syntax-and-Global-Constants-in-PHP7

Learn-How-to-Use-Changed-functions,-new-syntax-and-Global-Constants-in-PHP7
In the last chapter, we discussed the new features of Generator Return Expressions and Generator Delegation, which are added to PHP 7 on the existing generator feature of PHP 4 and 5.x. In this chapter, we are going to discuss the changed functions, new Unicode codepoint escape syntax and new Global Constants, which are added to PHP 7 along with practical examples.

Changed functions
Following are the changed functions in PHP 7 as compared to the previous PHP versions.

  • debug_zval_dump () à This PHP function dumps a string representation of an internal Zend value to output. In PHP 7, it now prints “int” instead of “long” as data type, and “float” instead of “double” data type.
  • dirname () à This PHP function returns a parent directory’s path. In PHP 7, it now has an option for taking a second parameter i.e. depth that is used to get the name of the directory depth levels up from the current directory.
  • getrusage () à This PHP function is used to get the current resource usages. In PHP 7, it is now supported on Windows.
  • mktime () and gmmktime () functions à In PHP 7, these functions are no longer accepting is_dst as a parameter. The former function is used to get UNIX timestamp for a date and the latter function is used to get UNIX timestamp for a GMT date.
  • preg_replace () function à this function is used to perform a regular expression search and replace. In PHP 7, this function no longer supports “\e” (PREG_REPLACE_EVAL). Instead preg_replace_callback () function should be used.
  • setlocale () function à This function is used to Set locale information. In PHP 7, this function is no longer accepting the category passed as string. Instead LC_* constants must be used.
  • exec (), system () and passthru () functions à These PHP functions have NULL byte protection now.
  • substr () à In PHP 7, this function now returns an empty string, if string is equal to start characters long.

Example of New Global Constants
In the following example, we are printing the value returned by the changed functions in PHP 7 that we have discussed just now.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<html>

<head>

<title><?php echo “Welcome to PHP-7 Tutorials”;?></title>

</head>

<body>

<?php

print(“<br>debug_zval_dump(): “);

print(debug_zval_dump(“PHP”));

print(“<br>dirname(): “);

print(dirname(“/php7-demo-examples”));

print(“<br>setlocale () : “);

print(setlocale(LC_ALL,0));

print(“<br>substr(): “);

print(substr(“PHP”,2,3));

?>

</body>

</html>

Output
Following is the output, when we execute above PHP 7 program. We can observe the values which are returned by the changed functions in PHP 7.
Output1
New Unicode codepoint escape syntax
This is the new feature in PHP 7 that takes a Unicode codepoint in hexadecimal form as input and outputs that codepoint in UTF-8 format to a double-quoted string or a heredoc as shown in the below example. It accepts all valid code points. If we input any codepoint with the number of leading 0’s, it will behave in the same manner if there are no zeros. Therefore, giving leading zero to hexadecimal form is optional.

Example of New Unicode codepoint escape syntax
In the following example, we are printing the value New Unicode codepoint escape syntax in PHP 7 that we have discussed just now.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<html>

<head>

<title><?php echo “Welcome to PHP-7 Tutorials”;?></title>

</head>

<body>

<?php

echo “\u{ab}”;

print(“<br> This will be same as if it is without leading zeros.<br>”);

echo “\u{0000ab}”;

print(“<br>”);

echo “\u{9999}”;

?>

</body>

</html>

Output2
Output
Following is the output when we will execute above PHP 7 program. We can observe the values which are returned by the New Unicode codepoint escape syntax in PHP 7.

Note: The addition of new Unicode codepoint escape syntax in PHP 7 has an impact on the strings that contain a literal \u {followed by an invalid sequence, it will cause a fatal error. In order avoid this fatal error, the leading backslash should be escaped.

New Global Constants
Global constants in PHP are those which are available to get used in the PHP program globally and the value of these constants cannot be altered. Following are the new global constants that are added to PHP 7.

Core Predefined Constants

  • PHP_INT_MIN

LibXML

  • LIBXML_BIGLINES

PCRE

  • PREG_JIT_STACKLIMIT_ERROR

POSIX

  • POSIX_RLIMIT_AS
  • POSIX_RLIMIT_CORE
  • POSIX_RLIMIT_CPU
  • POSIX_RLIMIT_DATA
  • POSIX_RLIMIT_FSIZE
  • POSIX_RLIMIT_LOCKS
  • POSIX_RLIMIT_MEMLOCK
  • POSIX_RLIMIT_MSGQUEUE
  • POSIX_RLIMIT_NICE
  • POSIX_RLIMIT_NOFILE
  • POSIX_RLIMIT_NPROC
  • POSIX_RLIMIT_RSS
  • POSIX_RLIMIT_RTPRIO
  • POSIX_RLIMIT_RTTIME
  • POSIX_RLIMIT_SIGPENDING
  • POSIX_RLIMIT_STACK
  • POSIX_RLIMIT_INFINITY

Zlib

  • ZLIB_BLOCK
  • ZLIB_FINISH
  • ZLIB_FULL_FLUSH
  • ZLIB_NO_FLUSH
  • ZLIB_PARTIAL_FLUSH
  • ZLIB_SYNC_FLUSH

Example of New Global Constants
In the following example, we are printing the values of some of the newly added global constants which are added to PHP 7.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<html>

<head>

<title><?php echo “Welcome to PHP-7 Tutorials”;?></title>

</head>

<body>

<?php

print(“<br>PHP_INT_MIN: “);

print(PHP_INT_MIN);

print(“<br>LIBXML_BIGLINES: “);

print(LIBXML_BIGLINES);

print(“<br>PREG_JIT_STACKLIMIT_ERROR: “);

print(PREG_JIT_STACKLIMIT_ERROR);

print(“<br>ZLIB_BLOCK: “);

print(ZLIB_BLOCK);

?>

</body>

</html>

Output3
Output
Following is the output of executing the above PHP 7 program. We can observe the values which these global constants hold.
Source Code for Global Constant in PHP 7

Conclusion
In this chapter, we have discussed the changed functions, new Unicode codepoint escape syntax and new global constants which are added to PHP 7. In the coming chapter of this tutorial, we are going to discuss about preg_replace_callback_array (), ArrayAccess and Other Changes which are added to PHP 7.

LEAVE A REPLY

Please enter your comment!
Please enter your name here