PHP Tips : How to use optimized built-it functions  ?
Tags: php, webdev, codequality, beginners
Published: 2020-04-15 15:43:26

Did you already use built-in function in PHP like `count`, `strlen` or `in_array` ? 
I think so, these are essential but do you know there are some of these functions that can be executed faster !? Thanks to OpCode optimized.

*OpCode you said ?*

Yeah OpCode, you know this is the list of instructions that are generated when your php code is compiled and easier interpreted by the computer.

For exemple this code :
```php

<?php

namespace foo;

$myString = 'This is my string !';
strlen($myString);
```
Will generate this OpCode :

```
line     #* E I O op                         fetch       ext  return  operands
--------------------------------------------------------------------------------
   5     0  E >   ASSIGN                                              !0, 'This+is+my+string+%21'
   6     1        INIT_NS_FCALL_BY_NAME                               'foo%5Cstrlen'
         2        SEND_VAR_EX                                         !0
         3        DO_FCALL                                 0          
         4      > RETURN                                              1

```

And some functions like `strlen` has is own opCode (you can see this in the next pictures at line 6)

```
line     #* E I O op            fetch          ext  return  operands
---------------------------------------------------------------------------
   5     0  E >   ASSIGN                                    !0, 'This+is+my+string+%21'
   6     1        STRLEN                            ~2      !0        
         2        FREE                                      ~2
         3      > RETURN                                    1
```
Instead of use a call to a function `INIT_NS_FCALL_BY_NAME` you can use the OpCode `STRLEN` directly that was optimized.

This OpCode is not use by default when you are in a namespace (like my exemple), if you want to use the optimized version you have to prefix your built-it function by a backslash `\` like this : `\strlen('foo bar')`. 

To compare the performance of the two versions, I make a little script which use strlen in a loop with and without backslash and this is the result :

## Not optimized strlen in a loop

```php 

<?php

namespace foo;

$bar = [];
for($i = 0; $i < 10000; $i++) {
    $bar[] = strlen('this is my string !'.$i);
}
```
![strlen not optimized](https://dev-to-uploads.s3.amazonaws.com/i/svryn0rkeimf88cf6vh1.png)

## Optimized \strlen always in a loop 

```php 
<?php

namespace foo;

$bar = [];
for($i = 0; $i < 10000; $i++) {
    $bar[] = \strlen('this is my string !'.$i);
}
```

![strlen optimized](https://dev-to-uploads.s3.amazonaws.com/i/z2f0cws8dg0o86oi23bj.png)

It's a micro optimization, but always good to take.
A list of opCode was introduced with PHP 7.0, you can see it bellow:

#### Most useful functions
- [count](https://www.php.net/manual/en/function.count.php)
- [strlen](https://www.php.net/manual/en/function.strlen)
- [in_array](https://www.php.net/manual/en/function.in-array)
- [array_key_exists](https://www.php.net/manual/en/function.array-key-exists)

#### Comparison functions
- is_null
- is_bool
- is_long
- is_int
- is_integer
- is_float
- is_double
- is_real
- is_string
- is_array
- is_object
- is_resource
- is_callable

#### Cast functions
- boolval
- intval
- floatval
- doubleval
- strval

#### Other functions
- chr
- ord
- dirname
- defined
- constant
- define
- gettype
- get_class
- get_called_class
- func_num_args
- func_get_args
- function_exists
- extension_loaded
- call_user_func_array
- call_user_func
- array_slice

If you don't like prefix your functions with a `\` you can import the function with `use function` instead and enjoy the benefits of optimized code.

```php
<?php

namespace foo;

use function strlen;

$myString = 'This is my string !';
strlen($myString);
```

You can use a tool like [PHP-CS-FIXER](https://github.com/FriendsOfPHP/PHP-CS-Fixer) to indicate in your code base which optimisations can be done and fix them, for optimized functions the option is `native_function_invocation`.

In conclusion, use the optimized functions as much as possible, it does not require much effort and can make your code more efficient.

Here is some links : 
- [when strlen was introduced](https://stackoverflow.com/questions/55854738/when-was-the-strlen-opcode-introduced-in-php)
- [Function optimized in PHP](https://github.com/php/php-src/blob/PHP-7.3.8/Zend/zend_compile.c#L3904)

Thank you for reading,
Codeur Malin