Question: I'm developing a browser extension (content script) that scans and highlights certain words on a page, and uses AJAX and PHP to echo back content into a tooltip that appears when a user hover over said words. One thing that gets echoed back from the PHP file is an image, but my problem is that I don't have an image for every keyword - what I want is the PHP to only echo back an image when one exists at the given URL. As of now, certain words show images, others show the "image not found" icon. What I want is for **no** "image not found" icon if the image doesn't exist. 

I have the AJAX send the variable ($data) to the PHP files hosted on my website. Maybe have it check the HTTP header of the url before echoing? 

Here is my code:

> 
> $data = $\_POST['id'];
> 
> 
> echo "http://extension.nicholasrub.in/headshots/" . $data . ".png'>";
> 
> 
>

Answer: 
Why don't you just check it like this?

```
$data = $_POST['id'];
if($data !== "") {
    echo "http://extension.nicholasrub.in/headshots/" . $data . ".png'>";
}

else {
    echo "http://extension.nicholasrub.in/headshots/notFound.png'>";
}

```

EDIT:

Use `file_exists()`:

```
$data = $_POST['id'];
$imagePath = "/path/images/" . $data . ".png";

if (file_exists($imagePath)) {
    echo "http://extension.nicholasrub.in/headshots/" . $data . ".png'>";
}

else {
    echo "http://extension.nicholasrub.in/headshots/notFound.png'>";
}

```
You can check if a file exists using this function
<http://php.net/manual/en/function.file-exists.php>