Jump to content

Archived

This topic is now archived and is closed to further replies.

manianprasanna

To write a looping function

Recommended Posts

I would like to write a looping function. for example as below

[code]
loop(10) {
    echo 'Please answer me';
}
[/code]

So that the above code would print 10 times ''Please answer me''. Actually the code looks similar to for loop, but the above code is not the actual functionality i need. The functionality i need is similar to the above code. So could anyone tell me that whether i can write a looping function. If yes then how?

Please help me.

Share this post


Link to post
Share on other sites
You should read this:
[url=http://www.w3schools.com/php/php_looping.asp]PHP and looping[/url]

Orio.

Share this post


Link to post
Share on other sites
This:

[code]
<?php

function loop($times, $text) {
for ($i = 0; $i < $times; $i++) {
echo "$text<br />";
}
}
loop(10, "Please answer me");

?>
[/code]

Should work.

Share this post


Link to post
Share on other sites
Thanks for those who had replied so far, but those are not the answers i want. Because as i already said that i want to looping statment.To explain more clear, see the below code

[code]
loop(10) {
    for($i=0; $i<5; $i++) {
        echo 'please help me';
    }
}
[/code]

The same loop function should also be able to use like below

[code]
loop(10) {
    if($i==8) {
        echo 'please help me';
    }
}
[/code]

I think now you all can understand more clear. To accomplish this type of function calling how should i declare the loop function.

Share this post


Link to post
Share on other sites
Where are you getting "function" from? I don't see any where in your code where you define a function.

Share this post


Link to post
Share on other sites
Yes that's what i would like to know that only., that is how to define the function loop to accomplish the functionality said in my previous post.

[quote]Thanks for those who had replied so far, but those are not the answers i want. Because as i already said that i want to looping statment.To explain more clear, see the below code

[code]
loop(10) {
    for($i=0; $i<5; $i++) {
        echo 'please help me';
    }
}

[/code]

The same loop function should also be able to use like below


[code]
loop(10) {
    if($i==8) {
        echo 'please help me';
    }
}
[/code]

I think now you all can understand more clear. To accomplish this type of function calling how should i declare the loop function.[/quote]

Share this post


Link to post
Share on other sites
Can't you see looking at the code I posted how to define a function? I don't see why the code I posted won't work for you?

[code]
<?php

function loop($times, $text) {
for ($i = 0; $i < $times; $i++) {
echo "$text<br />";
}
}
loop(10, "Please answer me"); //First declare the amount of times you want the text within the parenthesis to be displayed. Then within the parenthesis declare the text you want displayed.

?>
[/code]

Maybe I am totally mis-understanding you, if that is the case, I applogize. Maybe you can be more specific?

Share this post


Link to post
Share on other sites
[quote author=manianprasanna link=topic=107212.msg429772#msg429772 date=1157626439]
I would like to write a looping function. for example as below

[code]
loop(10) {
    echo 'Please answer me';
}
[/code]

So that the above code would print 10 times ''Please answer me''. Actually the code looks similar to for loop, but the above code is not the actual functionality i need. The functionality i need is similar to the above code. So could anyone tell me that whether i can write a looping function. If yes then how?

Please help me.
[/quote]

Here's a looping function to do what you specified:

[code]
<?php
function loop($iterations)
{
for ($i = 1; $i <= $iterations; $i++)
{
echo "Please answer me";
}
}

// usage...
loop(10);
?>
[/code]

The output on a web page will look like this:

[code]Please answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer mePlease answer me[/code]

I'm almost sure that's not what you want, but hey, who am I to judge?  I now re-quote you:

[quote author=manianprasanna link=topic=107212.msg429772#msg429772 date=1157626439]
Actually the code looks similar to for loop, but the above code is not the actual functionality i need. The functionality i need is similar to the above code.
[/quote]

How are we supposed to devine the special knowledge we need to write your code for you? 

Share this post


Link to post
Share on other sites
Thanks for all those who replied.

I would like to tell one more thing. The statemets inside the looping function is not same in all the case. I can be anything like conditional statements, DB manupulations, etrc....The loop function should take those statements as input and loop it.

case 1:
[code]
loop(10) {
    //some set of statements.
}
[/code]

case 2:
[code]
loop(10) {
    //Not the same set of statements as in case 1.
}
[/code]

Share this post


Link to post
Share on other sites
With my function, you can change what is looped and how many times, for example:

[code]
<?php

function loop($times, $text) {
for ($i = 0; $i < $times; $i++) {
echo "$text<br />";
}
}

loop(10, "Please answer me"); //Please answer me - looped 10 times

loop(8, "Hello, how are you"); //Hello, how are you - looped 8 times

//You could apply statements like:

$test = "8";

if ($test == "8") {
loop(5, "Test equals 8");
}

?>
[/code]

Share this post


Link to post
Share on other sites
[quote author=manianprasanna link=topic=107212.msg429814#msg429814 date=1157631990]
Thanks for all those who replied.
[/quote]

You're not a programmer, are ya...  :/

Hostfreak has given you the answer (twice).

Share this post


Link to post
Share on other sites
Again thankyou hostfreak. But you had changed my function calling from loop(10) to loop(10, "Please answer me"). First i don't like to have another parameter (your 2[sup]nd[/sup] parameter). And i would like to execute any statements inside the calling loop function.

Example 1:
[code]
loop(10) {
    ////MySQL insert statements.
}
[/code]

Example 2:
[code]
loop(10) {
    ////Some simple echo statemets.
}
[/code]

Example 2:
[code]
loop(10) {
    ////Some file manupulations.
}
[/code]

Sorry if i have hurted any body..
Please reply me.

[quote author=hostfreak link=topic=107212.msg429820#msg429820 date=1157632212]
With my function, you can change what is looped and how many times, for example:

[code]
<?php

function loop($times, $text) {
for ($i = 0; $i < $times; $i++) {
echo "$text<br />";
}
}

loop(10, "Please answer me"); //Please answer me - looped 10 times

loop(8, "Hello, how are you"); //Hello, how are you - looped 8 times

//You could apply statements like:

$test = "8";

if ($test == "8") {
loop(5, "Test equals 8");
}

?>
[/code]
[/quote]

Share this post


Link to post
Share on other sites
Hmmm , i think you need to hacking PHP code to do something like that , becuase it's a statement and not function .

you can do it with PHP by compile it , i mean write function open the php source file and search the "loop" statement in the php source file , if find the statement replace it by real loop statement like "while" or "for" , i mean do something like templates engine (for example Smarty) .

Share this post


Link to post
Share on other sites
Can you provide proper examples please. Posting
[code]loop(10) {
    ////MySQL insert statements.
}

loop(10) {
    ////Some simple echo statemets.
}

loop(10) {
    ////Some file manupulations.
}[/code]
Doesnt help much.

A good example will be to post the code you want to be repeated over and over in your loop function.

If you dont want a secound paramter, then prehaps use global:
[code]function loop($times)
{
    global $text;

    for ($i = 0; $i < $times; $i++)
    {
        echo "$text<br />";
    }
}

$text = "Please answer me";
loop(10); //Please answer me - looped 10 times

$text =  "Hello, how are you";
loop(8); //Hello, how are you - looped 8 times[/code]

Also you cannot use a function to do three different things, without having more than 1 parameter. How will the loop function know you want to run a query, or manupulate a file, echo something etc. I cannot see where you are going with this.

Share this post


Link to post
Share on other sites
The OP doesn't want a function, he wants a new statement type.

PHP already has looping statements: for, while, do ... while. Why can't one of those be used?

Example:

[code]<?php
for($i=0;$i<10;$i++) {
//
//  code block to be executed 10 times
//
}?>[/code]

Ken

Share this post


Link to post
Share on other sites
[quote author=kenrbnsn link=topic=107212.msg429899#msg429899 date=1157638850]
The OP doesn't want a function, she wants a new statement type.

PHP already has looping statements: for, while, do ... while. Why can't one of those be used?

Example:

[code]<?php
for($i=0;$i<10;$i++) {
//
//  code block to be executed 10 times
//
}?>[/code]

Ken
[/quote]

She? Lol, his profile would suggest otherwise.

Share this post


Link to post
Share on other sites
The OP appears to want a general function that loops but has a different body depending on from where it's called.

I'm guessing their intended use is:
[code]
// get DB items
$items = mysql_query($sql);
// Loop over items and do something
$results = Loop($items);
// Loop over the results and do something else entirely
$new_results = Loop($results);
[/code]

To the OP, if this is what you want, sorry you can't do that.  You'll just have to write a new function for each type of looping functionality you want to provide.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.