Question: I am getting a image source from a URL dynamically, but sometimes the URL returns empty or the image does not exist so i created a `PHP` function to check if the image source do exist or not so i can display a default image to my users when the image is not accessible.

My concern, is my function enough to catch the data?

My current php function which works when its sure that a image do exist.

```
function get_image_by_id($ID) {

  $url = 'http://proweb/process/img/'.$ID.'.jpg'; 

  if(empty($url)){

   return 'default.jpg';

   }else{

    return $url;

  }

}

```

My index.php 

```
<img src="<?php echo get_image_by_id($ID);?>" />

```

Answer: 
Here `CURL` can also be helpful, tested it and working fine. Here we are using curl to get HTTP response code for the URL. If status code is `200` it means it exists. 

```
function get_image_by_id($ID)
{
    $url = 'http://proweb/process/img/' . $ID . '.jpg';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);
    $info = curl_getinfo($ch);
    if ($info["http_code"] != 200)
    {
        return 'default.jpg';
    }
    else
    {
        return $url;
    }
}

```
```
function get_image_by_id($ID) {
 if(empty($url)){
$url = 'http://proweb/process/img/default.jpg; 
}else{
$url = 'http://proweb/process/'.$ID.'.jpg'; 
 }
return $url
}

```