将'if else'语句转换为PHP中的循环?

How can I convert this into a loop in PHP?

$number = 4; // This number is unknown and can be any number.

if ($number === 1) {
    echo $red;
} else if ($number === 2) {
    echo $yellow;
} else if ($number === 3) {
    echo $orange;
} else if ($number === 4) {
    echo $black;
} else if ($number === 5) {
    echo $green;
} else if ($number === 6) {
    echo $grey;
} else if ($number === 7) {
    echo $brown;
} else if ($number === 8) {
    echo $blue;
} else if ($number === 9) {
    echo $silver;
} else if ($number === 10) {
    echo $gold;
} else if ($number === 11) {
    echo $white;
} 

etc...

Right now the $number can be any number, so I would have to somehow loop through the numbers from 1 to unlimited, until it finds what $number is equal to. In this case, number is equal to 4.

I'm not sure why you want to do it like this, but:

$i = 1;
while ($i !== $number) {
    $i++;
}
$int = $i;

To do what you want, you should use an array map, but an switch might do the trick too.

PHP don't actually know 1 = one, so you can't iterate through it with a loop. You have to provide this mapping to it, be it by switching instead of loads of ifs, or creating an array map.

Loop is completly the wrong construct for your problem.

Use an array like so:

<?php
$number = 2;
$english = array("zero","one","two","three","four");
echo "file" . $english[$number];
?>

displays filetwo

After OP edit all becomes clear -- try:-

 <?php
    $number = 2;
    $red = "crimson";
    $clear = "glass";
    $yellows = array("gold","lemon","acid","ochre");
    $orange = "tangerine";
    $black = "charcoal";
    $objects = array(&$clear,&$red,&$yellows,&$orange,&$black);

    var_dump( $objects[$number] );

?>

outputs:

array(4) { [0]=> string(4) "gold" [1]=> string(5) "lemon" [2]=> string(4) "acid" [3]=> string(5) "ochre" }

Why not just create a array of items that are the 'lookup table' in the order need like:

$items = ['item_1', 'item_2', 'item_3', … 'item_n'];

or have a string of items like:

$items = explode('|', 'item_1|item_2|item_3|item_n');
//| being the delemiter or what ever might not occur inside any items

then have your $number (might need to minus 1 to $number if items start with 1 and so on to get correct item) used in the array to get the specific item without a loop like:

//$number = 3
$number--;
echo $items[$number];//echos 'item3'

but it almost seems like you then want something like:

//$number = 3
$items = explode('|', 'red|pink|gold|invisible');
echo ${$items[$number - 1]};
//translates into `echo $gold;`
//echos whatever is in $gold

Reading some of your comments it seems like you might be best off to rethink your logic/variables and use arrays and such instead of possibly hundreds of differently named variables that you have to check every time which can't be converted into a simple loop like you wish it could be. The only other possibly is using variables variables like many, including me, have mentioned but you seem to shoot them down. I wouldn't recommend variables variables personally but it would possibly speed up time without needing to do any looping. To effectively minimize code without the need of many if/elses you need to have logic that is consistent.

I think this is what you mean :

echo ${"file" . $number};

But that is showing variable for $filen, for example : $file1, file2, etc. If you want to make it as number, you can do like this :

echo ${"file" . intToEnglish($number)};

You can make intToEnglish(int $number) yourself or need us for help?

I'm not quite sure why you need to loop this, but if you really need to loop this, you can try this :

$i = 0;
while ($i !== $number) {
    $i++;
} 
echo ${"file" . intToEnglish($i)};

But, make sure that echo ${"file" . intToEnglish($number)}; is exist or you will go through infinite looping.

-- EDIT --

This is the case with your edited colour variable case. If you want to make it simple (looping), you muse change how your variable assingned. Example :

$colour = array("green","yellow","red","blue","black");
$i = 0;
while ($i !== $number) {
    $i++;
} 
echo $colour[$i];

Actually that looping is not really necessary until you have process inside it. You can just define it directly like this :

echo $colour[number];

-- EDIT 2 --- This is with checking things :

while($i <= $number) { 
    if($i === $number) { echo $colour[number]; } 
    else { echo 'Checking'. $i; }
}