How to find out which extensions are installed in my PHP?

Asked

Viewed 5,403 times

6

Is there any way to find out which extensions are installed in my PHP?

I already know I can do it with phpinfo, but I find the visualization confusing.

There is a simpler way, ie only the lists of extensions installed?

  • 2

    I do not understand the reason for downvote, I find a valid question and that can generate different types of answers +1... I am formulating one at the moment I think it may be a better alternative ;)

  • 1

    @Guilhermenascimento must be someone who already knows how to list PHP modules and thinks it’s bullshit to share knowledge with others (ironic)

  • 1

    It can be, I can not say but by the arguments I’ve read, because incredible as it seems there are users who comment on Soen and "Portuguese" (different from here), the argument is always something like, question without previous research. Usually if the question is a doubt in the use, for them it is OK, but if it is a question that you do not know where to start but there is documentation then "take negative", for the simple fact to them that you should have researched. The funny thing is that Help itself encourages us to create questions to add good content

2 answers

6


Terminal

Apart from the quoted terminal command in the other answer:

$ php -m

Using php

There is still the option to do it in PHP, which is nice to display maybe on a Dashboard or if you have created a Wizard installer that checks what is active and necessary. There are 2 types of extension, the "normal extensions" the "zend extensions", in case you can use the function get_loaded_extensions

array get_loaded_extensions ([ bool $zend_extensions = FALSE ] )
  • List normal extensions:

    print_r(get_loaded_extensions());
    
  • List zend extensions:

    print_r(get_loaded_extensions(true));
    

Check functionality ("Feature detection")

The examples mentioned above are good for installation or configuration time, however for a production software it is recommended to check if the functionality is supported with functions such as:

  • class_exists:

    bool class_exists ( string $class_name [, bool $autoload = true ] )
    

    An example:

    <?php
    if (class_exists('PDO', false)) {
         //Usar PDO
    } elseif (class_exists('mysqli', false)) {
         //Usar mysqli
    }
    
  • function_exists:

    bool function_exists ( string $function_name )
    

    Another example with function_exists and class_exists:

    <?php
    if (function_exists('imagecreatetruecolor')) {
         //Usar $image = imagecreatetruecolor();
         //...
    } elseif (class_exists('Imagick', false)) {
         //Usar $image = new Imagick();
         //...
    }
    
  • extension_loaded:

    bool extension_loaded ( string $name )
    

    We also have the extension_loaded, however you can also use, an example to check if you have GD or Imagick:

    <?php
    if(extension_loaded('gd')) {
        print_r(gd_info());
    } elseif (extension_loaded('imagick')) {
        $imagick = new Imagick();
        print_r($imagick->queryFormats());
    } else {
        echo 'Nenhuma extensão de imagem disponível';
        exit;
    }
    

    It has no problem at all, the problem that occurs is if some function changes name, somewhat rare to happen, but so you will catch an error of undefined which can cause a number of problems in the sequence since the extension works but the function names no longer exist or changed format (from procedural to class for example). See this example of a possible problem:

  • 2

    This last example I’ve seen being using in Gregwar\Image. It is a library to work with images. + 1 for example :)

  • @Wallacemaxters In their source code? I believe the latter is good, but the function_exists and class_exists be better

  • I think I made a mistake. I saw this in the source code of the Laravel. There is an excerpt that checks if mcrypt is activated. He uses extension_loaded

3

In the terminal, you can use the command $ php -m

Browser other questions tagged

You are not signed in. Login or sign up in order to post.