0
for($i = 0; $i <= 7; $i++)
    if(!empty($room_ID.($i+1)) 
     && !empty($date_from.($i+1)) 
     && !empty($date_to.($i+1)) )

I have 8x3 integers.. Id like to know if there is a way to loop thru all of them with a for loop like giving $i to the end of the integer names like so:

room_ID1
room_ID2
date_from1
date_from2
etc..

Best answer would be in php, but i could use anything!

2 Answers2

1

It looks like you're trying to interpolate the variable names at runtime. You can accomplish this like so:

for($i = 0; $i <= 7; $i++)
    if(!empty(${'room_ID'.($i+1)}) 
         && !empty(${'date_from'.($i+1)}) 
         && !empty(${'date_to'.($i+1))} )

In general, the recipe is: ${'base_name' . (derived computation)}

See also this question

Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • Not sure if i get it why to put the variables between '', your first answer worked too, like this: ${room_ID.($i+1)} –  Jan 30 '14 at 18:54
  • by default, bare strings that are not defined constants are converted to strings (yet will also issue a warning). It is not recommended to rely on this behavior. e.g. `test` will be interpreted as a string (`'test'`) if the constant `test` has not been defined – Jeff Lambert Jan 30 '14 at 19:22
0

You need to enclose the variable variables in braces. Try this:

<?php
$room_ID1 = 123;
$date_from1 = "2014-01-01";
$date_to1 = "2014-01-30";

for($i = 0; $i <= 7; $i++)
    if(!empty(${'room_ID'.($i+1)}) 
     && !empty(${'date_from'.($i+1)}) 
     && !empty(${'date_to'.($i+1)}) )
{
    echo ${'room_ID'.($i+1)};
}

More info: http://php.net/manual/de/language.variables.variable.php

Steyx
  • 636
  • 5
  • 6