88

Possible Duplicate:
Writing a function in php

I'm using the following code

echo 'Curl: ', function_exists('curl_version') ? 'Enabled' : 'Disabled';

this can get it enabled or disabled

but I would like to make as function say function name is _iscurl

then I can call it as following any where in my website code

if (_iscurl()){
  echo "this is enabled"; // will do an action
}else{
  echo "this is disabled"; // will do another action
}

almost same as my previous question check if allow_url_fopen is enabled or not

Cœur
  • 37,241
  • 25
  • 195
  • 267
Reham Fahmy
  • 4,937
  • 15
  • 50
  • 71
  • 7
    Why not use `function_exists('curl_version') ` as your `_iscurl()` function? – Anirudh Ramanathan Nov 17 '12 at 19:23
  • 1
    See the manual on writing a [`function`](http://php.net/function), pack your one-liner into there, replace the `echo` with `return` and cut the ternary out. – mario Nov 17 '12 at 19:24

7 Answers7

145

Just return your existing check from a function.

function _isCurl(){
    return function_exists('curl_version');
}
John V.
  • 4,652
  • 4
  • 26
  • 26
  • 6
    This works, and is accepted, but the below answers are what i would consider less hacky, and more clear to someone else reading the code. If i read this, i might think you are specifically checking to see if you can find the curl version, and not to see if curl is loaded. `extension_loaded('curl')` is much more direct. – deweydb Jul 24 '17 at 21:59
65
<?php

// Script to test if the CURL extension is installed on this server

// Define function to test
function _is_curl_installed() {
    if  (in_array  ('curl', get_loaded_extensions())) {
        return true;
    }
    else {
        return false;
    }
}

// Ouput text to user based on test
if (_is_curl_installed()) {
  echo "cURL is <span style=\"color:blue\">installed</span> on this server";
} else {
  echo "cURL is NOT <span style=\"color:red\">installed</span> on this server";
}
?>

or a simple one -

<?
phpinfo();
?>

Just search for curl

source - http://www.mattsbits.co.uk/item-164.html

da5id
  • 9,100
  • 9
  • 39
  • 53
Amit Pandey
  • 1,436
  • 2
  • 24
  • 34
52
var_dump(extension_loaded('curl'));
mpen
  • 272,448
  • 266
  • 850
  • 1,236
Alex S
  • 719
  • 6
  • 8
10

Hope this helps.

<?php
    function _iscurl() {
        return function_exists('curl_version');
    }
?>
Angel Politis
  • 10,955
  • 14
  • 48
  • 66
bikash shrestha
  • 429
  • 2
  • 5
  • 12
    [`function_exists`](http://php.net/function_exists) itself returns `true` or `false`. You can just return it's return value. There is no need for 4 extra lines of code, for this *one liner*. Also, your function doesn't have end `}`! – Prasanth Nov 17 '12 at 19:30
  • 1
    agree on the overhead in code but the function does have an end } as one line if else statements don't need curly brackets. But perhaps the bad indentation made you make that mistake. – Sam Vloeberghs Dec 22 '13 at 17:26
8

you can check by putting these code in php file.

<?php
if(in_array  ('curl', get_loaded_extensions())) {
    echo "CURL is available on your web server";
}
else{
    echo "CURL is not available on your web server";
}

OR

var_dump(extension_loaded('curl'));
manish1706
  • 1,571
  • 24
  • 22
5

You can always create a new page and use phpinfo(). Scroll down to the curl section and see if it is enabled.

Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
5

Its always better to go for a generic reusable function in your project which returns whether the extension loaded. You can use the following function to check -

function isExtensionLoaded($extension_name){
    return extension_loaded($extension_name);
}

Usage

echo isExtensionLoaded('curl');
echo isExtensionLoaded('gd');
Channaveer Hakari
  • 2,769
  • 3
  • 34
  • 45
  • 1
    Assume you working in a big project and want to check the above condition more than 30 to 40 times. Then you need to write manually 30 to 40 times and all of sudden you got requirement that you need to check with some condition before at that time you need to search and put condition in all 30 - 40 places in your project. Instead if you had reusable function you can put that condition inside the function and can avoid the overhead of searching and replacing or adding code. – Channaveer Hakari Mar 21 '17 at 13:11
  • 2
    That's what refactoring tools are for. But on the point: Introducing code of which you don't know for sure that it is necessary I would consider a flaw. So the reason you name is a reason not to do so in my opinion. – hakre May 25 '17 at 11:13
  • Great idea. I have started wrapping all native php functions. I collected them in a library. Anyone interested? – rosell.dk Jun 23 '17 at 09:54