Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Własne pętle
Sokal
post 1.06.2007, 18:00:36
Post #1





Grupa: Zarejestrowani
Postów: 237
Pomógł: 1
Dołączył: 8.02.2007

Ostrzeżenie: (10%)
X----


Hej,

Czy w PHP da się coś takie zrobić jak własna pętla? biggrin.gif

Np. chciałbym sobie takie coś zrobić:

repeat($x)
{
echo 'cos tam';
}

Gdzie $x to ilość razy jaką ma się wykonać kod.


--------------------
Jabber/E-Mail: dominiksokal[at]gmail.com | GG: #3795571
Go to the top of the page
+Quote Post
phpion
post 1.06.2007, 18:06:41
Post #2





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Chyba jedynym rozwiazaniem byłoby utworzenie funkcji repeat() i w niej odpalenie pętli np. for. Tylko, że ciało funkcji musiałoby być z góry ustalone. A może do funkcji przekazywać kod PHP jak parametr i przelecieć go eval'em? Nie wiem czy by to zadziałało...
Go to the top of the page
+Quote Post
Cysiaczek
post 1.06.2007, 18:12:07
Post #3





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




  1. <?php
  2. function repeat($text, $counts, $endline=false){
  3.  
  4. $availSufix=array("n", "<br />");
  5.  
  6. if($endline){
  7.  
  8. if(in_array($endline, $availSufix)){
  9. $sufix=$endline;
  10. }
  11. }
  12.  
  13. if(!is_integer($counts)){
  14. $counts=0;
  15. }
  16.  
  17. for($i=0; $i<$counts; $i++){
  18. $ret.=$text.$sufix;
  19. }
  20. return $ret;
  21. }
  22.  
  23. print repeat("hello", 10, "<br />");
  24. ?>

Napisałem w 3 minuty : >>
Pozdrawiam.

Ten post edytował Cysiaczek 1.06.2007, 18:16:53


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
Sokal
post 1.06.2007, 18:26:43
Post #4





Grupa: Zarejestrowani
Postów: 237
Pomógł: 1
Dołączył: 8.02.2007

Ostrzeżenie: (10%)
X----


Ale sie nie o to rozchodzi tongue.gif

Chce mieć uniwersalne ...

Czyli np.

repeat(3)
{
$i++;
}

Albo:

repeat(21)
{
$variable .= 'petla';
}


--------------------
Jabber/E-Mail: dominiksokal[at]gmail.com | GG: #3795571
Go to the top of the page
+Quote Post
Cysiaczek
post 1.06.2007, 18:34:56
Post #5





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




Takie rzeczy tylko... w manualu, w dziale poświęconym pisaniu własnych funkcji i rozszerzeń do języka.


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
dr_bonzo
post 1.06.2007, 19:59:24
Post #6





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


Nie da sie.
Mozesz sobie zrobic template do edytora ktory
repeat(n)
zamieni ci na petle
  1. <?php
  2. for ( $x = 0; $x < n; $x++ )
  3. {
  4. ...
  5. }
  6. ?>


21.times { print "use ruby instead\n" } # biggrin.gif


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
phpion
post 1.06.2007, 20:24:20
Post #7





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Hmmm
  1. <?php
  2. function repeat($code, $loops) {
  3. for ($i=0; $i<$loops; $i++) {
  4. eval($code);
  5. }
  6. }
  7.  
  8. repeat("echo 'lalala<br>';", 3);
  9. repeat("echo str_replace('a', 'o', 'lalala').'<br>';", 3);
  10. ?>

i śmiga smile.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 14:30