No way to do that in php, that's what javascript is for.
document.getElementById("myimg").src = "newimage.jpg";
i reply to the comment:
$('img').each(function(){
var source = $(this).parent().find('span').first().val(); //if spans contain picture names
$(this).attr("src",source);
});
if you can use id's
document.getElementById("first").src = "15.jpg";
document.getElementById("second").src = "10.jpg";
document.getElementById("third").src = "21.jpg";
...
another reply to the comments. To invoke javascript on php:
if ($invoke_javascript): ?>
<script>
alert('javascript goes here');
</script>
<?php endif;