{"id":1862,"date":"2022-05-21T09:51:28","date_gmt":"2022-05-21T04:21:28","guid":{"rendered":"http:\/\/phptutorialpoints.in\/?p=1862"},"modified":"2025-07-16T23:41:10","modified_gmt":"2025-07-16T18:11:10","slug":"php-8-features-and-improvements","status":"publish","type":"post","link":"https:\/\/phptutorialpoints.in\/php-8-features-and-improvements\/","title":{"rendered":"PHP 8 Features and Improvements"},"content":{"rendered":"<div id=\"bsf_rt_marker\"><\/div><p>In this article, we will see PHP 8 Features and improvements. <span style=\"color: #111111; font-family: Helvetica,Arial,sans-serif,;\">PHP 8 was released in November 2020<\/span> and it is<span style=\"color: #111111; font-family: Helvetica,Arial,sans-serif,;\"> a new major version of PHP. There are some breaking changes and lots of new PHP 8 features and performance improvements. <\/span><\/p>\n<p><span style=\"color: #111111; font-family: Helvetica,Arial,sans-serif,;\">Currently, the latest version is PHP 8.1 and which was released on November 25, 2021, and It included several improvements, such as enumerations (also called &#8220;enums&#8221;), readonly properties, and array unpacking with string keys.<\/span><\/p>\n<p><!--more--><\/p>\n<h2>PHP 8 Features<\/h2>\n<p>See below for new PHP 8 features<\/p>\n<p><strong>1). Union Types<\/strong><\/p>\n<p>Union types are a collection of two or more types that indicate that either can be used. This will also allow several types to be defined for the arguments received by a function, as well as for the value it returns.<\/p>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">Note that void can never be part of a union type since it indicates &#8220;no return value at all&#8221;. Furthermore, nullable unions can be written using |null, or by using the existing? notation.<\/p>\n<p>It is one of the good features from a list of PHP 8 features.<\/p>\n<p>See below example of PHP version 8 where how union type is used:<\/p>\n<div class=\"q-relative qu-py--tiny qu-borderRadius--small qu-bg--gray_ultralight QTextCode___StyledRelative-sc-1j2dh40-0 fvlKPw\">\n<div class=\"CssComponent-sc-1oskqb9-0 QTextCode___StyledCssComponent-sc-1j2dh40-1 mxwUy\">\n<pre class=\"highlight php\"><code><span class=\"c1\">\/\/ In PHP Version 7.4<\/span>\r\n<span class=\"kd\">class<\/span> <span class=\"nc\">Digit<\/span> <span class=\"p\">{<\/span>\r\n    <span class=\"k\">private<\/span> <span class=\"nv\">$digit<\/span><span class=\"p\">;<\/span>\r\n    <span class=\"k\">public<\/span> <span class=\"k\">function<\/span> <span class=\"n\">set<span class=\"nv\">Digit<\/span><\/span><span class=\"p\">(<\/span><span class=\"nv\">$digit<\/span><span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\r\n        <span class=\"nv\">$this<\/span><span class=\"o\">-&gt;<span class=\"nv\">digit <\/span><\/span><span class=\"o\">=<\/span> <span class=\"nv\">$digit<\/span><span class=\"p\">;<\/span>\r\n    <span class=\"p\">}<\/span>\r\n<span class=\"p\">}<\/span>\r\n\r\n<span class=\"c1\">\/\/ In PHP Version 8<\/span>\r\n<span class=\"kd\">class<\/span> <span class=\"nc\">Digit <\/span><span class=\"p\">{<\/span>\r\n    <span class=\"k\">private<\/span> <span class=\"kt\">int<\/span><span class=\"o\">|<\/span><span class=\"n\">float<\/span> <span class=\"nv\">$digit<\/span><span class=\"p\">;<\/span>\r\n    <span class=\"k\">public<\/span> <span class=\"k\">function<\/span> <span class=\"n\">setDigit<\/span><span class=\"p\">(<\/span><span class=\"kt\">int<\/span><span class=\"o\">|<\/span><span class=\"n\">float<\/span> <span class=\"nv\">$digit<\/span><span class=\"p\">):<\/span> <span class=\"kt\">void<\/span> <span class=\"p\">{<\/span>\r\n        <span class=\"nv\">$this<\/span><span class=\"o\">-&gt;<span class=\"nv\">digit <\/span><\/span><span class=\"o\">=<\/span> <span class=\"nv\">$digit<\/span><span class=\"p\">;<\/span>\r\n    <span class=\"p\">}<\/span>\r\n<span class=\"p\">}<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<p><strong>2). JIT Compiler<\/strong><\/p>\n<p>When PHP code is run, it&#8217;s usually done by compiling &amp; executed in a virtual machine. JIT will change this by compiling the code into x86 machine code and then running that code directly on the CPU.<\/p>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">PHP Opcache supports JIT. It&#8217;s disabled by default, and if enabled, <a href=\"https:\/\/php.watch\/versions\/8.0\/JIT\" target=\"_blank\" rel=\"noopener\">JIT<\/a> compiles and caches native instructions. It does not make a noticeable difference in IO-bound web applications but provides a performance boost for CPU-heavy applications.<\/p>\n<p>It is one of the new features from a list of PHP 8 features.<\/p>\n<div class=\"q-relative qu-py--tiny qu-borderRadius--small qu-bg--gray_ultralight QTextCode___StyledRelative-sc-1j2dh40-0 fvlKPw\">\n<pre class=\"CssComponent-sc-1oskqb9-0 QTextCode___StyledCssComponent-sc-1j2dh40-1 mxwUy\"># Enabling JIT in php.ini\r\nopcache.enable=1\r\nopcache.jit_buffer_size=100M\r\nopcache.jit=tracing<\/pre>\n<\/div>\n<p><strong>3). Nullsafe Operator<\/strong><\/p>\n<p>Instead of writing classic !== null you can use the \u201c?\u201d operator to write just reduce multiple lines of code to a single line.<\/p>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">The Nullsafe operator is similar to the ternary operator but will behave like an isset on the left-hand operand instead of just using its boolean value. This makes this operator especially useful for arrays and assigning defaults when a variable is not set.<\/p>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">It is not fully reliable as it doesn&#8217;t work on method calls. Instead, you need intermediate checks, or rely on optional helpers provided by some frameworks:<\/p>\n<p>It is one of the good features from a list of PHP 8 features.<\/p>\n<div class=\"q-relative qu-py--tiny qu-borderRadius--small qu-bg--gray_ultralight QTextCode___StyledRelative-sc-1j2dh40-0 fvlKPw\">\n<div class=\"CssComponent-sc-1oskqb9-0 QTextCode___StyledCssComponent-sc-1j2dh40-1 mxwUy\">\n<pre>$startDate = $booking-&gt;getStartDate();<\/pre>\n<\/div>\n<div class=\"CssComponent-sc-1oskqb9-0 QTextCode___StyledCssComponent-sc-1j2dh40-1 mxwUy\">\n<pre>$dateAsString = $startDate ? $startDate-&gt;asDateTimeString() : null;<\/pre>\n<\/div>\n<\/div>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">With the addition of the null safe operator, we can now have null coalescing-like behaviour on methods.<\/p>\n<div class=\"q-relative qu-py--tiny qu-borderRadius--small qu-bg--gray_ultralight QTextCode___StyledRelative-sc-1j2dh40-0 fvlKPw\">\n<div class=\"CssComponent-sc-1oskqb9-0 QTextCode___StyledCssComponent-sc-1j2dh40-1 mxwUy\">\n<pre>$dateAsString = $booking-&gt;getStartDate()?-&gt;asDateTimeString();<\/pre>\n<\/div>\n<\/div>\n<p><strong>4). Named Arguments<\/strong><\/p>\n<p>Named arguments allow you to pass in values to a function, by specifying the value name, so you don&#8217;t need to consider their order, also you can also skip optional parameters!<\/p>\n<p>It is one of the good features from a list of PHP 8 features.<\/p>\n<div class=\"CssComponent-sc-1oskqb9-0 QTextCode___StyledCssComponent-sc-1j2dh40-1 mxwUy\">\n<pre>function foo(string $a, string $b, ?string $c = null, ?string $d = null){ \/* \u2026 *\/ }<\/pre>\n<\/div>\n<pre>foo(\r\nb: 'value b',\r\na: 'value a',\r\nd: 'value d',\r\n);<\/pre>\n<p><strong>5). Attributes\u00a0<\/strong><\/p>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">Attributes allow us to declare meta-data for our functions, classes, properties, and parameters. It map to PHP class names (declared with an Attribute itself), and they can be fetched programmatically with <a href=\"https:\/\/phptutorialpoints.in\/category\/core-php\/\">PHP<\/a> Reflection API.<\/p>\n<p>It is one of the good features from a list of PHP 8 features.<\/p>\n<p>Attributes syntax is very simple, attributes which are also called annotations are specially formatted text enclosed with \u201c&lt;&lt;\u201d and \u201c&gt;&gt;.\u201d<\/p>\n<div class=\"q-relative qu-py--tiny qu-borderRadius--small qu-bg--gray_ultralight QTextCode___StyledRelative-sc-1j2dh40-0 fvlKPw\">\n<pre class=\"CssComponent-sc-1oskqb9-0 QTextCode___StyledCssComponent-sc-1j2dh40-1 mxwUy\">#[CustomAttribute]\r\nclass Foo {\r\n#[AnotherAttribute(42)]\r\npublic function bar(): void {}\r\n}<\/pre>\n<\/div>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">This allows us to easily declare attributes\/annotations which previously required storing them in doc block elements and parsing the string to infer them.<\/p>\n<p><strong>6). Inheritance with private methods<\/strong><\/p>\n<p>Earlier, PHP used public, protected, and private methods to apply the same <a href=\"https:\/\/phptutorialpoints.in\/object-oriented-php\/\">inheritance<\/a> checks. In other words, the same method signature rules as the protected and public methods should be followed by private methods. Because of this, children\u2019s classes would not be able to use private methods.<\/p>\n<p>It is one of the good features from a list of PHP 8 features.<\/p>\n<p>This updated PHP 8 feature has modified the behavior, so these inheritance checks are no longer performed on private methods. In addition, it did not make sense to use the final private feature, so doing so would now cause a warning:<\/p>\n<p><strong>Warning:<\/strong>\u00a0Private methods cannot be final as other classes never override them<\/p>\n<p><strong>7). Match Expression<\/strong><\/p>\n<p>The new\u00a0<a href=\"https:\/\/www.php.net\/manual\/en\/control-structures.match.php\" target=\"_blank\" rel=\"noopener\"><code>match<\/code>\u00a0expression<\/a>\u00a0is a terser safer alternative to the well-known\u00a0<code>switch<\/code>. It does not require the use of <code>case<\/code>\u00a0and\u00a0<code>break<\/code> statements support combined conditions and return a value instead of entering a new code block.<\/p>\n<p>It is one of the good features from a list of PHP 8 features.<\/p>\n<p>Here\u2019s a typical PHP 7\u00a0<code>switch<\/code>:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"language-php\" tabindex=\"0\"><code class=\"language-php\"><span class=\"token keyword\">switch<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n\t<span class=\"token keyword\">case<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">:<\/span>\r\n\t\t<span class=\"token variable\">$result<\/span> <span class=\"token operator\">=<\/span> <span class=\"token string single-quoted-string\">'Foo'<\/span><span class=\"token punctuation\">;<\/span>\r\n\t\t<span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span>\r\n\t<span class=\"token keyword\">case<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">:<\/span>\r\n\t\t<span class=\"token variable\">$result<\/span> <span class=\"token operator\">=<\/span> <span class=\"token string single-quoted-string\">'Bar'<\/span><span class=\"token punctuation\">;<\/span>\r\n\t\t<span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span>\r\n\t<span class=\"token keyword\">case<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">:<\/span>\r\n\t\t<span class=\"token variable\">$result<\/span> <span class=\"token operator\">=<\/span> <span class=\"token string single-quoted-string\">'Baz'<\/span><span class=\"token punctuation\">;<\/span>\r\n\t\t<span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token punctuation\">}<\/span>\r\n \r\n<span class=\"token keyword\">echo<\/span> <span class=\"token variable\">$result<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token comment\">\/\/&gt; Bar<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>And, here\u2019s how the same code could look with PHP 8:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"language-php\" tabindex=\"0\"><code class=\"language-php\"><span class=\"token keyword\">echo<\/span> <span class=\"token keyword\">match<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n\t<span class=\"token number\">0<\/span> <span class=\"token operator\">=&gt;<\/span> <span class=\"token string single-quoted-string\">'Foo'<\/span><span class=\"token punctuation\">,<\/span>\r\n\t<span class=\"token number\">1<\/span> <span class=\"token operator\">=&gt;<\/span> <span class=\"token string single-quoted-string\">'Bar'<\/span><span class=\"token punctuation\">,<\/span>\r\n\t<span class=\"token number\">2<\/span> <span class=\"token operator\">=&gt;<\/span> <span class=\"token string single-quoted-string\">'Baz'<\/span><span class=\"token punctuation\">,<\/span>\r\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token comment\">\/\/&gt; Bar<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><strong>8). Weak Maps<\/strong><\/p>\n<p>A weak map is a collection of data (objects) in which keys are weakly referenced, meaning that they are not prevented from being garbage collected.<\/p>\n<p>It is one of the good features from a list of PHP 8 features.<\/p>\n<p><strong>WeakMaps and WeakRefs<\/strong>\u00a0can be used to delete objects when only the cache references the entity classes of the objects. This leads to resource-saving handling of the objects.<\/p>\n<p>See the below example:<\/p>\n<pre class=\" language-php\"><code class=\" language-php\"><span class=\"token variable\">$map<\/span> <span class=\"token operator\">=<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">WeakMap<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token variable\">$obj<\/span> <span class=\"token operator\">=<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">stdClass<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token variable\">$map<\/span><span class=\"token punctuation\">[<\/span><span class=\"token variable\">$obj<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">=<\/span> <span class=\"token number\">42<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token function\">var_dump<\/span><span class=\"token punctuation\">(<\/span><span class=\"token variable\">$map<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/code><\/pre>\n<p>In long-run processes, this can prevent memory leaks, which eventually improve performance.<\/p>\n<p><strong>9). Constructor Property Promotion<\/strong><\/p>\n<p>Instead of specifying class properties and a constructor for them, PHP can now combine them into one.<\/p>\n<pre class=\"highlight php\"><code><span class=\"c1\">\/\/ In php 7 or later minor version<\/span>\r\n<span class=\"kd\">class<\/span> Shape <span class=\"p\">{<\/span>\r\n    <span class=\"k\">protected<\/span> <span class=\"kt\">float<\/span> <span class=\"nv\">$a<\/span><span class=\"p\">;<\/span>\r\n    <span class=\"k\">protected<\/span> <span class=\"kt\">float<\/span> <span class=\"nv\">$b<\/span><span class=\"p\">;<\/span>\r\n    <span class=\"k\">protected<\/span> <span class=\"kt\">float<\/span> <span class=\"nv\">$c<\/span><span class=\"p\">;<\/span>\r\n\r\n    <span class=\"k\">public<\/span> <span class=\"k\">function<\/span> <span class=\"n\">__construct<\/span><span class=\"p\">(<\/span>\r\n        <span class=\"kt\">float<\/span> <span class=\"nv\">$a<\/span> <span class=\"o\">=<\/span> <span class=\"mf\">0.0<\/span><span class=\"p\">,<\/span>\r\n        <span class=\"kt\">float<\/span> <span class=\"nv\">$b<\/span> <span class=\"o\">=<\/span> <span class=\"mf\">0.0<\/span><span class=\"p\">,<\/span>\r\n        <span class=\"kt\">float<\/span> <span class=\"nv\">$c<\/span> <span class=\"o\">=<\/span> <span class=\"mf\">0.0<\/span><span class=\"p\">,<\/span>\r\n    <span class=\"p\">)<\/span> <span class=\"p\">{<\/span>\r\n        <span class=\"nv\">$this<\/span><span class=\"o\">-&gt;<\/span><span class=\"n\">a<\/span> <span class=\"o\">=<\/span> <span class=\"nv\">$a<\/span><span class=\"p\">;<\/span>\r\n        <span class=\"nv\">$this<\/span><span class=\"o\">-&gt;<\/span><span class=\"n\">b<\/span> <span class=\"o\">=<\/span> <span class=\"nv\">$b<\/span><span class=\"p\">;<\/span>\r\n        <span class=\"nv\">$this<\/span><span class=\"o\">-&gt;<\/span><span class=\"n\">c<\/span> <span class=\"o\">=<\/span> <span class=\"nv\">$c<\/span><span class=\"p\">;<\/span>\r\n    <span class=\"p\">}<\/span>\r\n<span class=\"p\">}<\/span>\r\n\r\n<span class=\"c1\">\/\/ In PHP 8 version<\/span>\r\n<span class=\"kd\">class<\/span> Shape <span class=\"p\">{<\/span>\r\n    <span class=\"k\">public<\/span> <span class=\"k\">function<\/span> <span class=\"n\">__construct<\/span><span class=\"p\">(<\/span>\r\n        <span class=\"kt\">protected<\/span> <span class=\"n\">float<\/span> <span class=\"nv\">$a<\/span> <span class=\"o\">=<\/span> <span class=\"mf\">0.0<\/span><span class=\"p\">,<\/span>\r\n        <span class=\"kt\">protected<\/span> <span class=\"n\">float<\/span> <span class=\"nv\">$b<\/span> <span class=\"o\">=<\/span> <span class=\"mf\">0.0<\/span><span class=\"p\">,<\/span>\r\n        <span class=\"kt\">protected<\/span> <span class=\"n\">float<\/span> <span class=\"nv\">$c<\/span> <span class=\"o\">=<\/span> <span class=\"mf\">0.0<\/span><span class=\"p\">,<\/span>\r\n    <span class=\"p\">)<\/span> <span class=\"p\">{}<\/span>\r\n<span class=\"p\">}<\/span><\/code><\/pre>\n<p><strong>10). Consistent type error for internal functions<\/strong><\/p>\n<p>PHP will already throw\u00a0<code>TypeError<\/code>\u00a0in User-defined functions, \u00a0but internal functions rather emitted warnings and returned\u00a0<code>null<\/code>. As of PHP 8, the behavior of internal functions has been made consistent.<\/p>\n<p>&nbsp;<\/p>\n<p>Also, there are some new functions introduced in PHP 8. See below<\/p>\n<p><strong>1). str_contains() function<\/strong><\/p>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">PHP 8 introduces str_contains(), which returns a simple boolean indicating if the needle is present in the haystack. It helps you to determine whether the given sub-string is present inside the string.<\/p>\n<p>It is one of the new features from a list of PHP 8 features.<\/p>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">So instead of doing this:<\/p>\n<div class=\"q-relative qu-py--tiny qu-borderRadius--small qu-bg--gray_ultralight QTextCode___StyledRelative-sc-1j2dh40-0 fvlKPw\">\n<div class=\"CssComponent-sc-1oskqb9-0 QTextCode___StyledCssComponent-sc-1j2dh40-1 mxwUy\">\n<pre>if (strpos('string with lots of words', 'words') !== false) { \/* \u2026 *\/ }<\/pre>\n<\/div>\n<\/div>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">You would now do this:<\/p>\n<div class=\"q-relative qu-py--tiny qu-borderRadius--small qu-bg--gray_ultralight QTextCode___StyledRelative-sc-1j2dh40-0 fvlKPw\">\n<div class=\"CssComponent-sc-1oskqb9-0 QTextCode___StyledCssComponent-sc-1j2dh40-1 mxwUy\">\n<pre>if (str_contains('string with lots of words', 'words')) { \/* \u2026 *\/ }<\/pre>\n<\/div>\n<\/div>\n<p><strong>2). str_starts_with() and str_ends_with() functions<\/strong><\/p>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">These functions are now incorporated into the core:<\/p>\n<div class=\"q-relative qu-py--tiny qu-borderRadius--small qu-bg--gray_ultralight QTextCode___StyledRelative-sc-1j2dh40-0 fvlKPw\">\n<div class=\"CssComponent-sc-1oskqb9-0 QTextCode___StyledCssComponent-sc-1j2dh40-1 mxwUy\">\n<pre>str_starts_with('haystack', 'hay'); \/\/ true<\/pre>\n<\/div>\n<div class=\"CssComponent-sc-1oskqb9-0 QTextCode___StyledCssComponent-sc-1j2dh40-1 mxwUy\">\n<pre>str_ends_with('haystack', 'stack'); \/\/ true<\/pre>\n<\/div>\n<\/div>\n<p><strong>3). fdiv() function<\/strong><\/p>\n<p>The new fdiv() function does something similar as the fmod() and intdiv() functions, which allows for division by 0. Instead of errors, you&#8217;ll get INF, -INF or NAN, depending on the cas<\/p>\n<p><strong>4). get_debug_type() function<\/strong><\/p>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">The function get_debug_type() returns the type of a variable. get_debug_type() returns more useful output for arrays, strings, anonymous classes and objects. Sure it sounds like gettype() but there are benefits of the later.<\/p>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">For example: calling gettype() on a class \\Foo\\Bar would return object. Using get_debug_type() will return the class name.<\/p>\n<p><strong>5). get_resource_id()<\/strong><\/p>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">Resources are special variables in PHP, referring to external resources. One example is a MySQL connection, and another one is a filehandle.<\/p>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">Each one of those resources gets assigned an ID, though previously the only way to know that id was to cast the resource to int:<\/p>\n<div class=\"q-relative qu-py--tiny qu-borderRadius--small qu-bg--gray_ultralight QTextCode___StyledRelative-sc-1j2dh40-0 fvlKPw\">\n<div class=\"CssComponent-sc-1oskqb9-0 QTextCode___StyledCssComponent-sc-1j2dh40-1 mxwUy\">\n<pre>$resourceId = (int) $resource;<\/pre>\n<\/div>\n<\/div>\n<p class=\"q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start\">PHP 8 adds the get_resource_id() functions, making this operation more obvious and type-safe:<\/p>\n<div class=\"q-relative qu-py--tiny qu-borderRadius--small qu-bg--gray_ultralight QTextCode___StyledRelative-sc-1j2dh40-0 fvlKPw\">\n<div class=\"CssComponent-sc-1oskqb9-0 QTextCode___StyledCssComponent-sc-1j2dh40-1 mxwUy\">\n<pre>$resourceId = get_resource_id($resource);<\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>PHP 8 features introduces a powerful lineup of features that elevate the development experience, making it easier to write cleaner, more efficient, and more expressive code. From the performance boost of JIT compilation to the improved error handling and the flexibility of union types and named arguments, PHP 8 empowers developers to create robust and future-proof web applications.<\/p>\n<p>By embracing these keywords &#8211; JIT compilation, union types, match expressions, named arguments, constructor property promotion, error handling improvements, and attributes &#8211; developers can harness the full potential of PHP 8 and stay at the forefront of modern web development. Upgrade to PHP 8 and unlock a new level of efficiency, readability, and performance in your code. The future of PHP development starts with PHP 8!<\/p>\n<p>I hope this article helps you to know PHP 8 features!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will see PHP 8 Features and improvements. PHP 8 was released in November 2020 and it is a new major version of PHP. There are some breaking changes and lots of new PHP 8 features and performance improvements. Currently, the latest version is PHP 8.1 and which was released on November [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6145,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2],"tags":[559,560,561],"class_list":["post-1862","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-php","tag-php-8-features","tag-php-8-functions","tag-php-8-improvements"],"acf":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/phptutorialpoints.in\/wp-content\/uploads\/2025\/07\/PHP-8-Highlights.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/phptutorialpoints.in\/wp-json\/wp\/v2\/posts\/1862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phptutorialpoints.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phptutorialpoints.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phptutorialpoints.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phptutorialpoints.in\/wp-json\/wp\/v2\/comments?post=1862"}],"version-history":[{"count":19,"href":"https:\/\/phptutorialpoints.in\/wp-json\/wp\/v2\/posts\/1862\/revisions"}],"predecessor-version":[{"id":4143,"href":"https:\/\/phptutorialpoints.in\/wp-json\/wp\/v2\/posts\/1862\/revisions\/4143"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/phptutorialpoints.in\/wp-json\/wp\/v2\/media\/6145"}],"wp:attachment":[{"href":"https:\/\/phptutorialpoints.in\/wp-json\/wp\/v2\/media?parent=1862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phptutorialpoints.in\/wp-json\/wp\/v2\/categories?post=1862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phptutorialpoints.in\/wp-json\/wp\/v2\/tags?post=1862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}