Getting cover images from children if they exist

I try to get
the cover image or if it doesnt exist
the first image or if it doesnt exist
the first subpage its cover image or if it doesnt exist
thefirst subpage its first image or if it doesnt exist
(nothing)

I try tis but it doesnt work. I get an error when there is no coverimage on the first subpage

  <?php if($image = $page->cover()->toFile()): ?>
		

		<!-- Load MAIN IMAGE -->
        <img class="placeholderimg" src="<?= $image->thumb('xs')->url() ?>">
		<img data-src="<?= $image->url() ?>" class="lazyload" srcset="<?= $image->srcset([300, 500, 1000, 1500, 1800, 2000]) ?>">   
		<?php  else: ?>  
        
            <?php if($image = $page->image()): ?>
           <img class="placeholderimg" src="<?= $image->thumb('xs')->url() ?>">
	       <img data-src="<?= $image->url() ?>" class="lazyload" srcset="<?= $image->srcset([300, 500, 1000, 1500, 1800, 2000]) ?>">   
           
            <?php  else: ?> 
    			
                <?php if($image = $page->children()->first()->cover()->toFile()): ?>
                <p> childrens cover</p>
        	    <img class="placeholderimg lazyload" src="<?= $image->url() ?>" srcset="<?= $image->srcset([1]) ?>">
	    	    <img data-src="<?= $image->url() ?>" class="lazyimage lazyload" srcset="<?= $image->srcset([300, 500, 1000]) ?>">   
            
                <?php // no image in selection
                else: ?>  
        
        
                    <?php if($image = $page->children()->first()->images()->first()->toFile()): ?>
                    <p>   childrens  first image</p>
             	    <img class="placeholderimg lazyload" src="<?= $image->url() ?>" srcset="<?= $image->srcset([1]) ?>">
	           	    <img data-src="<?= $image->url() ?>" class="lazyimage lazyload" srcset="<?= $image->srcset([300, 500, 1000]) ?>">   

                    <?php endif ?>
        
        
                <?php endif ?>
            
    
            <?php endif ?>
        <?php endif ?>

First, I’d suggest you simplify your code, using if - elseif, instead of nesting your stuff.
Secondly, I’d try to be less repetitive, no idea why you use different srcsets here, and different classes. If that can be avoided, it would help to make the code more readable as well.

This should work, but try to clean up:

<?php if ($image = $page->cover()->toFile()) : ?>


  <!-- Load MAIN IMAGE -->
  <img class="placeholderimg" src="<?= $image->thumb('xs')->url() ?>">
  <img data-src="<?= $image->url() ?>" class="lazyload" srcset="<?= $image->srcset([300, 500, 1000, 1500, 1800, 2000]) ?>">


<?php elseif ($image = $page->image()) : ?>
  <img class="placeholderimg" src="<?= $image->thumb('xs')->url() ?>">
  <img data-src="<?= $image->url() ?>" class="lazyload" srcset="<?= $image->srcset([300, 500, 1000, 1500, 1800, 2000]) ?>">


<!-- Here you can't be sure you have a child, so: -->
<?php elseif ( ($p = $page->children()->first() ) && $image = $p->cover()->toFile()) : ?>
  <p> childrens cover</p>
  <img class="placeholderimg lazyload" src="<?= $image->url() ?>" srcset="<?= $image->srcset([1]) ?>">
  <img data-src="<?= $image->url() ?>" class="lazyimage lazyload" srcset="<?= $image->srcset([300, 500, 1000]) ?>">

<!-- converting the first image to file doesn't make sense, so: -->
<?php elseif ( ($p = $page->children()->first() ) && $image = $p->images()->first()) : ?>
  <p> childrens first image</p>
  <img class="placeholderimg lazyload" src="<?= $image->url() ?>" srcset="<?= $image->srcset([1]) ?>">
  <img data-src="<?= $image->url() ?>" class="lazyimage lazyload" srcset="<?= $image->srcset([300, 500, 1000]) ?>">

<?php endif ?>
<?php 
$placeholderImage = $page->cover()->toFile() ?? $page->image();
if (! $placeholderImage) {
  if ( $p = $page->children()->first()) {
    $fallbackImage = $p->cover()->toFile() ?? $p->images()->first();
  }
}
?>
<?php if ($placeholderImage) : ?>

  <!-- Load MAIN IMAGE -->
  <img class="placeholderimg" src="<?= $image->thumb('xs')->url() ?>">
  <img data-src="<?= $image->url() ?>" class="lazyload" srcset="<?= $image->srcset([300, 500, 1000, 1500, 1800, 2000]) ?>">

<?php elseif ($fallbackImage) : ?>
  <img class="placeholderimg lazyload" src="<?= $image->url() ?>" srcset="<?= $image->srcset([1]) ?>">
  <img data-src="<?= $image->url() ?>" class="lazyimage lazyload" srcset="<?= $image->srcset([300, 500, 1000]) ?>">

<?php endif ?>

I simplified the first version but I still get an error on one spot: when the child doesnt have an image at all:

<?php else: ?>    
        <!-- Is there an image? -->
        <?php if ($image = $page->cover()->toFile()) : ?>
        <?php elseif ($image = $page->image()) : ?>
        <!-- Here you can't be sure you have a child, so: -->
        <?php elseif ( ($p = $page->children()->first() ) && $image = $p->cover()->toFile()) : ?>
        <!-- converting the first image to file doesn't make sense, so: -->
        <?php elseif ( ($p = $page->children()->first() ) && $image = $p->images()->first()) : ?>
        <?php endif ?>
 
        <img class="placeholderimg" src="<?= $image->thumb('xs')->url() ?>">
        <img data-src="<?= $image->url() ?>" class="lazyload" srcset="<?= $image->srcset([300, 500, 1000, 1500, 1800, 2000]) ?>">
    
<?php endif ?>

Please wrap code blocks within three backticks on a separate line before and after the code block.

You have two endifs, which doesn’t make sense.

The code otherwise shouldn’t throw an error once that is removed. If it still does, please indicate the line where the error is thrown.

The endif is part of a higher level condition.

It works now like this:

<?php if ($image = $thumb->cover()->toFile()) : ?>
     
    <img class="placeholderimg" src="<?= $image->url() ?>" srcset="<?= $image->srcset([1]) ?>">
    <img data-src="<?= $image->url() ?>" class="lazyload" srcset="<?= $image->srcset([300, 500, 1000, 1500, 1800, 2000]) ?>">


    <?php elseif ($image = $thumb->image()) : ?>  

    <img class="placeholderimg" src="<?= $image->url() ?>" srcset="<?= $image->srcset([1]) ?>">
    <img data-src="<?= $image->url() ?>" class="lazyload" srcset="<?= $image->srcset([300, 500, 1000, 1500, 1800, 2000]) ?>">


    <?php elseif ( ($p = $thumb->children()->first() ) && $image = $p->cover()->toFile()) : ?> 

    <img class="placeholderimg" src="<?= $image->url() ?>" srcset="<?= $image->srcset([1]) ?>">
    <img data-src="<?= $image->url() ?>" class="lazyload" srcset="<?= $image->srcset([300, 500, 1000, 1500, 1800, 2000]) ?>">


    <?php elseif ( ($p = $thumb->children()->first() ) && $image = $p->images()->first()) : ?>
    
    <img class="placeholderimg" src="<?= $image->url() ?>" srcset="<?= $image->srcset([1]) ?>">
    <img data-src="<?= $image->url() ?>" class="lazyload" srcset="<?= $image->srcset([300, 500, 1000, 1500, 1800, 2000]) ?>">


    <?php endif ?>

But now your html is completely the same and you repeat it 4 times while you only need it once!

Then where should it be placed?

Use the logic to define the image, as outlined above, but now you need only one image definition.

Then one if statement to print your html.