-6

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.

user3747660
  • 55
  • 1
  • 9
  • 3
    Are you looking for, `$int = $number`? – Anthony Forloney Jan 22 '15 at 01:55
  • This is too abstract to tell, but you might be looking for a boring old array map. – mario Jan 22 '15 at 01:56
  • Not necessarily. I just need to convert the many 'else if' statements into a more concise loop. I just don't know which loop to use. – user3747660 Jan 22 '15 at 01:57
  • It's unclear why a loop would be necessary, unless you are _actually_ doing something more complex than `$int = ` If you are just assigning it, and the possible range is 1..MAXINT the simple assignment is sufficient. – Michael Berkowski Jan 22 '15 at 01:57
  • Well, what if I don't know what $number equals. Let's say number equals 20345, how many 'else if' statements would I need? Of course a loop is necessary. Or else I would need 20345 'else if' statements, manually written by hand. – user3747660 Jan 22 '15 at 01:58
  • 1
    Why do you need ANY `if/else if` statements? What's wrong with just assigning one variable to another? – Barmar Jan 22 '15 at 01:59
  • @user3747660 If you are ultimately just assigning it to `$int`, do you _need to know_ what `$number` 's value is? – Michael Berkowski Jan 22 '15 at 01:59
  • Let me edit the question, so you can actually see what I am trying to do. Hold on for 1 minute. – user3747660 Jan 22 '15 at 02:00
  • @user3747660 You wouldn't need any. `$int = $number` is enough, judging by your example. If you want anything more you'll have to post a better example. –  Jan 22 '15 at 02:00
  • I have edited the question. Basically, I do not know what $number equals. So I need to loop through all possible numbers from 1 to unlimited, until it finds what $number equals, then it echos it to the screen. – user3747660 Jan 22 '15 at 02:02
  • I don't need the conversion of a number to a string. Darn. Forget all strings and integers, I need the concept changed from an 'else if' statement to a loop in PHP. – user3747660 Jan 22 '15 at 02:05
  • my guess would be use a array or a switch but if using an array you could just do `$array[$number]`. It would be helpful to tell us what kind of data is being looked up/searched for. – Class Jan 22 '15 at 02:14
  • @user3747660 Without truly knowing what you're intending on doing with sample output, we cannot speak on what approach you should take. – Anthony Forloney Jan 22 '15 at 02:15
  • I showed above what I need to do. To convert the 'if else' statements into a loop in PHP. If $number = 10000, I can't write 10000 'if else' statements, until it finds out that $number = 10000. Is there another way of doing it with a loop? That is all I'm asking. Every single time I ask a question on stackoverflow, no one seems to understand the "CONCEPT" of what needs to be achieved. Forget strings, integers, etc. Concept. – user3747660 Jan 22 '15 at 02:20
  • Did just just asking "where I can read looping in php manual"? You can try read this mate http://www.w3schools.com/php/php_looping_for.asp – Jun Rikson Jan 22 '15 at 02:30
  • lol. I know how to perform a for loop in PHP. If it's so easy to achieve what I need, why not show me how it's done? Because it's not. The problem in the for loop is the middle of it, I do not know the number it needs to find. So I can't put ", $x <= 10, " because $x could be any number. – user3747660 Jan 22 '15 at 02:33
  • Can you tell me, could you answer your own question without knowing what you already know? – Class Jan 22 '15 at 02:47
  • I don't understand your question. I can't use a for loop, because for loops require me to input when the loop should stop (x<=10). But if I don't know what x equals, I can't use a for loop. – user3747660 Jan 22 '15 at 02:49

5 Answers5

1

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

$i = 1;
while ($i !== $number) {
    $i++;
}
$int = $i;
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

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.

  • Switching is very similar to 'else if' statements. If $number was equal to 10000, I can't manually write out 10000 switch cases, until it finds out that $number = 10000. – user3747660 Jan 22 '15 at 02:39
  • This should do it then: http://stackoverflow.com/questions/277569/is-there-an-easy-way-to-convert-a-number-to-a-word-in-php – Tiago Goddard Jan 22 '15 at 02:43
  • Like I replied to about 10 other people, I do not need number to string or string to number conversion. Did I ask that in the question? No. So why is everyone giving me conversion links? – user3747660 Jan 22 '15 at 02:45
  • 1
    Well, if there isn't a logic in what you want you can't iterate in a loop with it, but you can define it previously, be it with an array map, a switch or an enumerator class like this http://stackoverflow.com/a/254543 – Tiago Goddard Jan 22 '15 at 02:50
0

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" }
James Anderson
  • 27,109
  • 7
  • 50
  • 78
  • $filetwo is a real variable, an actual file that will get displayed. It's not just text. Like I said, the concept is what I need, forget strings and integers. I'm talking about showing a variable, not a string or integer. – user3747660 Jan 22 '15 at 02:29
  • @user3747660 -- variables contain strings and integers. I think you need to backtrack and learn the very basics of programming. – James Anderson Jan 22 '15 at 02:53
  • You are associating $number with a string, and then you echo it. That is not what I need, and is bad programming. You can't echo out $number, and then append on it what you have. If I rename the variable (like I have done in my question), your answer will be completely inaccurate. I have learned the basics of programming. If it's so easy, why not help out someone in need? – user3747660 Jan 22 '15 at 02:56
  • I will post another question, with the actual code I'm using, which is part of an array. It will make it easier for everyone. – user3747660 Jan 22 '15 at 03:29
0

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.

Class
  • 3,149
  • 3
  • 22
  • 31
0

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; }
}
Jun Rikson
  • 1,964
  • 1
  • 22
  • 43
  • Logic here: while ($i !== $number). I mentioned before that $number is not a static number. It could be any number between 1 and infinity. It just needs to loop through all possible numbers, then when a match is found, it outputs a variable. – user3747660 Jan 22 '15 at 03:11
  • did you know what is `while()` function is? It will looping `99999999999999` times if you put `99999999999999` in `$number`. – Jun Rikson Jan 22 '15 at 03:13
  • That's right. So I need to loop through $number, until it finds out what $number is equal to. The while() function will only do that, if it doesn't find what $number is equal to. The problem is, I don't know how to achieve this with the while loop, with $number constantly changing. – user3747660 Jan 22 '15 at 03:14
  • @user3747660 If you already have a value stored in `$number`, why do you feel you need a loop to determine what it is equal to? What would happen once `$i` reached the value of `$number` in your scenario? You'd now have two variables with the same value, so what do you intend to do with them at that point? – eldarerathis Jan 22 '15 at 03:17
  • Because $number is generated via PHP based on a lot of code, it is something that I do NOT know what it will be. – user3747660 Jan 22 '15 at 03:21
  • yes as @eldarerathis said, until you have something really specific for your case, you just need to define it directly. Or you just really need `while($i <= $number) { if($i === $number) { \\process something } else { \\process something }}` – Jun Rikson Jan 22 '15 at 03:21
  • That way, looping will stop until $i reach the $number – Jun Rikson Jan 22 '15 at 03:22
  • You're going back to if and else, something that I need to avoid. What if $number is 10000? That means 10000 else if statements. What if $number is generated randomly, and I don't know what the number is? That is why I need to find it. – user3747660 Jan 22 '15 at 03:22
  • @user3747660 Okay. But once that value is generated and stored in the variable `$number`, you do know what it is: it is `$number`. Is there a reason you can't just read its value directly? Is it not actually a value being stored in a variable? – eldarerathis Jan 22 '15 at 03:24
  • $number is generated via a MySQL database query, as an array. So I wouldn't directly know what $number will be. That is why I need to find it, without those 'else if' statements. – user3747660 Jan 22 '15 at 03:25
  • @user3747660 : run the last code in my answer and get the logic. Tell me what you are trying to achieve now from the result? – Jun Rikson Jan 22 '15 at 03:27
  • This isn't working. Wait 1 minute until I will post the actual code I am using, as an array. I will repost another question in a few seconds. – user3747660 Jan 22 '15 at 03:29
  • 1
    Do not re-post with new question, just edit your question above. – Jun Rikson Jan 22 '15 at 03:31