PHP - 如何更快,更快地编写IF和ELSE语句

This code I wrote along time ago. It checks an external server for an image, if it exists, it will echo the image within a list. I have it up to 13 records to check. I thought an array would be best here but not sure how they work.

    $a1="".$cdnurl."assets/".$pid."/1/medium.jpg";
              $a2="".$cdnurl."assets/".$pid."/2/medium.jpg";
              $a3="".$cdnurl."assets/".$pid."/3/medium.jpg";
              $a4="".$cdnurl."assets/".$pid."/4/medium.jpg";
              $a5="".$cdnurl."assets/".$pid."/5/medium.jpg";
              $a6="".$cdnurl."assets/".$pid."/6/medium.jpg";
              $a7="".$cdnurl."assets/".$pid."/7/medium.jpg";
              $a8="".$cdnurl."assets/".$pid."/8/medium.jpg";
              $a9="".$cdnurl."assets/".$pid."/9/medium.jpg";
              $a10="".$cdnurl."assets/".$pid."/10/medium.jpg";
              $a11="".$cdnurl."assets/".$pid."/11/medium.jpg";
              $a12="".$cdnurl."assets/".$pid."/12/medium.jpg";
              $a13="".$cdnurl."assets/".$pid."/13/medium.jpg";
              if(@fopen($a1,"r")){
                  echo '<li class="royalSlide"><img src="'.$a1.'" alt="" /></li>';
            }
if(@fopen($a2,"r")){echo '<li class="royalSlide"><img src="'.$a2.'" alt="" /></li>';
}
if(@fopen($a3,"r")){echo '<li class="royalSlide"><img src="'.$a3.'" alt="" /></li>';
}
if(@fopen($a4,"r")){echo '<li class="royalSlide"><img src="'.$a4.'" alt="" /></li>';
}
if(@fopen($a5,"r")){echo '<li class="royalSlide"><img src="'.$a5.'" alt="" /></li>';
}
if(@fopen($a6,"r")){echo '<li class="royalSlide"><img src="'.$a6.'" alt="" /></li>';
}
if(@fopen($a7,"r")){echo '<li class="royalSlide"><img src="'.$a7.'" alt="" /></li>';
}
if(@fopen($a8,"r")){echo '<li class="royalSlide"><img src="'.$a8.'" alt="" /></li>';
}
if(@fopen($a9,"r")){echo '<li class="royalSlide"><img src="'.$a9.'" alt="" /></li>';
}
if(@fopen($a10,"r")){echo '<li class="royalSlide"><img src="'.$a10.'" alt="" /></li>';
}
if(@fopen($a11,"r")){echo '<li class="royalSlide"><img src="'.$a11.'" alt="" /></li>';
}
if(@fopen($a12,"r")){echo '<li class="royalSlide"><img src="'.$a12.'" alt="" /></li>';
}
for ($i = 1; $i < 14; $i++)
{
   $elem = $cdnurl . "assets/" . $pid . "/$i/medium.jpg";
   if(is_readable($elem))
   {
      echo '<li class="royalSlide"><img src="'.$elem.'" alt="" /></li>';
   }
}

Using a foreach-loop does not make it faster, but will make it look cleaner.

Your design is wrong, you shouldn't have a need to check for the existence of the assets each time the page loads. Make the script generate the HTML and load that HTML instead of connecting to the CDN each time.

You're putting twice as much load on the CDN now. If you need to hide certain <li> elements if the image is missing, use Javascript to remove the element if the image fails to load.

A code that assumes an URL like "... number ...":

<?php
foreach (range(0, 13) as $number) {
    $url = $cdnurl . "assets/$pid/$number/medium.jpg";
    echo '<li class="royalSlide">
        <img src="' . $url . '" onerror="parentNode.parentNode.removeChild(parentNode)" alt="" />
    </li>';
}
?>

This code is an enhancement of the code from your question:

<?php
foreach (range(0, 13) as $number) {
    $url = $cdnurl . "assets/$pid/$number/medium.jpg";
    // skip URLs which cannot be found
    if (!file_exists($url)) continue;

    echo '<li class="royalSlide">
        <img src="' . $url . '" alt="" />
    </li>';
}
?>
for($i=1;i<=13;i++){
  $item =$cdnurl.'assets/'.$pid.'/'.$i.'/'.medium.jpg;
  echo (@fopen($item,'r')?'<li class="royalSlide"><img src="'.$item.'" alt="" /></li>':'');
}

would do the trick assuming that the files are indeed named by number

Yes you can use a list.

A good idea would be to create it with a for :

$a = array();
for($i = 0; î < 14; ++$i)
{
    $a[] = $cdnurl."assets/".$pid."/".$i."/medium.jpg";
}

Then you can read it :

foreach($a as $tmpA) 
{
...
}

Inside this you have to test if this is an image you have on your server, and display it. But you shouldn't use fopen. For a lot of resae.

At first you use @ because you d'ont want to have any Wrning on this, if you have warning that's you are doing wring, and php is trying to change that for you. Taht's your work to have clean code. But more important, fopen create a ressource based on the file open. Then in this page you create 14 ressources ! And they will be destroy at the end of the page becouse you never close it.

There is a php function which is : file_exitsts. Taht's the right one for you :

foreach($a as $tmpA) 
{
    if (file_exists($filename)) 
    {
        echo '<li class="royalSlide"><img src="'.$tmpA.'" alt="" /></li>';
    }
}

Although you can make this code cleaner, you cannot make it faster. Checking images from the outside servers is a terrible idea by itself and it ALWAYS be slow. Just because it's external server.

I am wondering if there is any reason to check for every image existence. Are they so fast in change?