39

Is there a PHP command I can use to determine if PDO is enabled or disabled?

I know I an manually run phpinfo() and eyeball it, but I have a script I run various web servers that display selected PHP configuration settings for the server.

So I am looking to see if there is a command I can use.

7 Answers 7

63

The proper way of determining that will be using the extension_loaded function:-

if ( extension_loaded('pdo') ) { ....... } 

And you might also want to check for the database-specific PDO driver using:-

if ( extension_loaded('pdo_<database type here>') ) { // e.g., pdo_mysql ....... } 
Sign up to request clarification or add additional context in comments.

1 Comment

Like pdo_firebird, pdo_mysql, pdo_oci, pdo_odbc, pdo_pgsql, pdo_sqlite, pdo_sqlite_external
42

Check if the class exists:

if (class_exists('PDO')) 

I appreciate the support and all the upvotes I still get, but please check Salman Abbas's answer for the proper way to do this.

4 Comments

Was going to suggest defined() but PDO switched to class constants.
I tested it and this way works perfectly (both on a server that had it enabled and on a server that did not). Thanks @AJ!
@AJ: wouldn't it be better to add false as 2nd param? If the OP only purely wants to check for existance, but not to autoload the class at the same time, I mean.
Unless some insane library developer decided to name his poly-device operator class PDO.
14

Just run the command as php -m from the command prompt which will display list of modules installed for PHP

Comments

7

You have two options:

if (extension_loaded('pdo')) { /* ... */ } 

Or (this one is not 100% reliable since it can be implemented in user-land classes):

if (class_exists('PDO', false)) { /* ... */ } 

Personally, I prefer the first option.

Comments

3
if (!defined('PDO::ATTR_DRIVER_NAME')) { echo 'PDO unavailable'; } elseif (defined('PDO::ATTR_DRIVER_NAME')) { echo 'PDO available'; } 

I hope this works

Comments

1

How about

if (in_array('pdo', get_loaded_extensions())) { ... pdo is there ... } 

2 Comments

@Dr. DOT: it only seems. Plus, AJ didn't write that answer. GolezTrol did.
I know...not sure how or why I picked up "AJ" -- sorry @GolezTrol
0

For checking it on linux terminal level using above command

php -m

which would give output related to modules installed from your php.inienter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.