Current location: Hot Scripts Forums » Programming Languages » PHP » Make a loop a variable


Make a loop a variable

Reply
  #1 (permalink)  
Old 06-29-07, 11:25 AM
Deansatch Deansatch is offline
Coding Addict
 
Join Date: Jul 2006
Location: Northumberland
Posts: 375
Thanks: 0
Thanked 0 Times in 0 Posts
Make a loop a variable

Is it possible to put a [B]for[B] loop in to a variable or in to an if?

i.e.
PHP Code:

$r= for($t=0;$t<=7;$t++){echo $array[t$] ); 

or
PHP Code:

 if(for($t=0;$t<=7;$t++){echo $array[t$] );    == $something){ etc...} 

Obviously I know these don't work but is there a way of achieving this sort of structure?
__________________
Aye!
Reply With Quote
  #2 (permalink)  
Old 06-29-07, 11:35 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
i don't think it's possible, but you can always give it a try

is there any specific reason why you need this? i've never had to use something like that in my life . I'm pretty sure you can find a workaround, or even better, solution to your problem
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 06-29-07, 11:40 AM
Deansatch Deansatch is offline
Coding Addict
 
Join Date: Jul 2006
Location: Northumberland
Posts: 375
Thanks: 0
Thanked 0 Times in 0 Posts
Still trying to work around my earlier calendar post problem. I need to compare my array results in an if statement that is already inside a foreach loop.
__________________
Aye!
Reply With Quote
  #4 (permalink)  
Old 06-29-07, 11:55 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
How about:
PHP Code:

$r '';

for(
$t=0;$t<=7;$t++)
{
    
$r .= $array[$t];

Reply With Quote
  #5 (permalink)  
Old 06-29-07, 12:09 PM
Deansatch Deansatch is offline
Coding Addict
 
Join Date: Jul 2006
Location: Northumberland
Posts: 375
Thanks: 0
Thanked 0 Times in 0 Posts
that doesn't seem to work and when I echo the array on it's own it gives the first array then the first and second, then the first, second and third etc...

I basically want to compare each array with my variable in my if statement.

i.e. if($array == $variable){ etc... for each of my array values then close the for and then echo.
__________________
Aye!
Reply With Quote
  #6 (permalink)  
Old 06-29-07, 02:07 PM
ben.periton ben.periton is offline
Wannabe Coder
 
Join Date: Oct 2004
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
So...

PHP Code:

for ($i=0$i<sizeof($array); $i++) {
    if (
$array[$i] == 'this') {
        print 
'here we go!';
    }

Kinda thing?
__________________
Ben Periton
http://ben.periton.co.uk
Reply With Quote
  #7 (permalink)  
Old 06-29-07, 02:38 PM
clickzilla clickzilla is offline
Newbie Coder
 
Join Date: Jun 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
or
PHP Code:

if( in_array($variable$array) ) { 
         echo 
"Yeey! :) "; }
else {
         echo 
":(";

check the in_array() function.
The definition is like this :
bool in_array ( mixed $needle, array $haystack [, bool $strict] )
Reply With Quote
  #8 (permalink)  
Old 06-30-07, 02:51 AM
Deansatch Deansatch is offline
Coding Addict
 
Join Date: Jul 2006
Location: Northumberland
Posts: 375
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by clickzilla View Post
or
PHP Code:

if( in_array($variable$array) ) { 

         echo 
"Yeey! :) "; }
else {
         echo 
":(";

That's the one!!!!!!! Cheers. It works. First time I've heard of that function.
__________________
Aye!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me make the most random loop ever! (add on) nova912 PHP 12 08-04-06 01:07 AM
how to make a variable equal a do/while statement todayscoffee PHP 3 12-09-05 07:24 AM
how to loop mysql into template system??? how u guys make it??? Eric Hadson PHP 0 09-19-05 10:43 PM


All times are GMT -5. The time now is 06:07 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.