Echo and selfcall in functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HKB
Forum Newbie
Posts: 1
Joined: Mon Sep 19, 2005 3:22 am

Echo and selfcall in functions

Post by HKB »

I have this litle test functions:

Code: Select all

function test() {

$i = 2;

echo $i;

if($i < 9) { test(); }

$i = $i + 1;

}
But how do i make echo work and can i call the function in it selfe?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

that function will go on forever, never ending loop. that is probably your problem. why not try somtin like

Code: Select all

function test($i)
{
  echo $i.'<br>';
  
  if ($i < 9)
    test($i++);
}

test(2);
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You would need to put your $i=$i+1; inside of your loop.

Alternatively, $i=$i+1; is also equal to $i++; (easier to read :P)

Code: Select all

$i = 2;

if($i < 9)
{
   echo $i;
   test();
   $i++;
}
And you cannot call the function inside of itself, however you can call its parameters (the stuff in between the ()).
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

This would be more efficient:

Code: Select all

function test() {

  for ($i = 2; $i < 9; $i++) {

    echo $i;

  }

}
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

scrotaye wrote:And you cannot call the function inside of itself, however you can call its parameters (the stuff in between the ()).
what do you mean? its called recursive function (i think) but i do it all the time

Code: Select all

<?

function test($i)
{
    echo $i.'<br>';

    if ($i < 9)
        test($i+1);
}

test(2);

/*
output:
2
3
4
5
6
7
8
9
*/

?>
strange thing was when it tried test($i++); it did not ++ the $i. hummmm
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$i++ is a post-increment, the increment happens after the variable is returned.
++$i is a pre-increment, the increment happens before the variable is returned.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

feyd wrote:$i++ is a post-increment, the increment happens after the variable is returned.
++$i is a pre-increment, the increment happens before the variable is returned.
well ill be damned
Post Reply