Forum Moderators: coopster

Message Too Old, No Replies

Check if PHP extension is enabled

         

andrewheiss

4:40 am on Mar 2, 2008 (gmt 0)

10+ Year Member



I'm working on a site that is hosted on two different servers with (unfortunately) different PHP configurations. I use the PHP Tidy extension on my pages to clean up the mess that ensues after echoing out a whole bunch of dynamic content. One of my servers, though, doesn't have Tidy enabled, though.

Rather than comment out all the code references to tidy on one copy of the site, I'd like to keep it consistent and code in some test to see if tidy is enabled, kind of like checking for magic quotes with get_magic_quotes_gpc()==1.

Is there an easy PHP way to see if php_tidy.dll (or any other extension in php.ini, like mhash) is enabled?

[edited by: coopster at 3:57 pm (utc) on Mar. 3, 2008]
[edit reason] no edits made [/edit]

phparion

5:17 am on Mar 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



one simple check could be

$tidy = new tidy;
if($tidy) {
echo "success";
}
else {
echo "failure";
}

andrewheiss

6:05 am on Mar 2, 2008 (gmt 0)

10+ Year Member



I tried that earlier, but it doesn't work. It calls a fatal error:
"Fatal error: Class 'tidy' not found..."

Is there a way to catch that error so the rest of the script continues and incorporate the error result in the logic? For example, something like this:


$tidy = new tidy;

/* Concept of logic, not actual code
if (error) {
no
continue script
} else {
yes
}
*/

PHP_Chimp

2:57 pm on Mar 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




if ([url=http://uk.php.net/manual/en/function.function-exists.php]function_exists[/url](tidy)) {
$tidy = new tidy;
//
}
else {
echo 'This place is not tidy';
}

You may have to change the function that you search for to something like 'tidy_repair_string', as tidy itself may not show up as a function.

[edited by: PHP_Chimp at 2:59 pm (utc) on Mar. 2, 2008]

coopster

7:21 pm on Mar 2, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can just use extension_loaded [php.net] too.

andrewheiss

1:27 am on Mar 3, 2008 (gmt 0)

10+ Year Member



Perfect! extension_loaded was what I needed.

Thanks!

phparion

5:52 am on Mar 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



extension_loaded is a nice addition to my knowledge. I wonder if I will ever be able to cram php manual :) so many functions...