If your pulling in images using ajax you can test to see if the data is available prior to creating a new image element and simply skip it. That way you don't load up your page with hidden elements. Every element you add to a page uses more memory. If your loading many image tags in the page that fail and your simply hiding them the page is going to get big depending on how big this loop is. When you get a response back check the data and if you get a 404 skip it and go to the next load request and don't add it to the page. also google bots don't like 404 messages. So loading bad image tags into your page will not be valid or clean and google will drop your SEO score.
Another thing you should consider is sending a request to the server have the server respond with a list of what files it has so your only sending load requests for what is on the server rather then guessing.
[UPDATE]
<?php if(file_exists($_GET["photo"])): ?>
<div id="title_wrap">
<div id="title"><?php echo $photo->get_title(); ?></div>
</div>
<div id="time_views">
<div id="time"><?php echo $photo->get_time();?></div>
<div id="views"><?php echo $photo->get_views(); ?></div>
</div>
<?php endif; ?>
<div id="relatedImages>this is outside the if statement and will be displayed even if the if statement is false</div>
If file_exists() returns false then none of this is displayed. so you don't have to hide it with css as it will not be on the page. There is no point in displaying the divs if there is no data to display. The solution is not css or JavaScript you should simply not show the content if the file is not there.
also this part of your code is not your problem. The problem is your giving the user the option to select images that do not exist. If your only giving the user the option to select images where you have the file then file_exists() will return true every time and you will never need to deal with a 404 error.
based on your questions and how you wright your PHP, I highly recommend getting a book on PHP. You should refresh on operators and if statements. Once you have reviewed the section on if statements check out formatting of inline php statements. This will get you on track for a very successful career in web programming. (the link to the book was the first one i found when searching amazon) It will be a good start. also check out http://www.w3schools.com/php/ they have a few beginner tutorials that can help you on your way. and remember KISS(Keep It Stupid Simple).
http://www.amazon.com/PHP-Absolute-Beginners-Jason-Lengstorf/dp/1430268158/ref=sr_1_1?s=books&ie=UTF8&qid=1422575607&sr=1-1&keywords=php+absolute+beginner&pebp=1422575612138&peasin=1430268158