Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Funkcja
victor_19
post
Post #1





Grupa: Zarejestrowani
Postów: 20
Pomógł: 0
Dołączył: 10.11.2010

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


  1. function tabela($tab)
  2. {
  3. $ilosc = count($tablica);
  4. echo '<table border = 1>';
  5. for($i=0; $i<2; $i++)
  6. {
  7. echo "<tr><td>$tab[$i]</td></tr>";
  8. }
  9. echo '</table>';
  10. }
  11.  
  12. $tablica = array('pierwszy', 'drugi');
  13.  
  14. tabela($tablica);
  15.  


Jak zrobić funkcję (z return)?
Go to the top of the page
+Quote Post
flashdev
post
Post #2





Grupa: Zarejestrowani
Postów: 812
Pomógł: 117
Dołączył: 2.12.2008

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


Cytat(victor_19 @ 13.12.2010, 17:13:03 ) *

  1. function tabela($tab)
  2. {
  3. // tutaj
  4. $ilosc = count($tablica);
  5. echo '<table border = 1>';
  6. for($i=0; $i<2; $i++)
  7. {
  8. echo "<tr><td>$tab[$i]</td></tr>";
  9. }
  10. echo '</table>';
  11. // i tutaj
  12. return ob_get_flush();
  13. }
  14.  
  15. $tablica = array('pierwszy', 'drugi');
  16.  
  17. tabela($tablica);
  18.  


Ten post edytował flashdev 13.12.2010, 17:16:38


--------------------
Go to the top of the page
+Quote Post
victor_19
post
Post #3





Grupa: Zarejestrowani
Postów: 20
Pomógł: 0
Dołączył: 10.11.2010

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


Chodzi mi o funkcję z samymi returnami bez echo...

  1. function tabela()
  2. {
  3.  
  4. return "<table border = 1>";
  5. return "<tr><td>jeden</td></tr>";
  6. return "</table>";
  7.  
  8. }
  9.  
  10. echo tabela();


Nie działa ta funkcja z kilkoma returnami. Czemu?
Go to the top of the page
+Quote Post
mmica
post
Post #4





Grupa: Zarejestrowani
Postów: 60
Pomógł: 20
Dołączył: 30.11.2010
Skąd: 127.0.0.1

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


Hey ;]
Nie działa, bo return "zabija" funkcję i zwraca wartość (najczęściej false/true, a więc gdyby funkcja zwróciła true i falsa to gdzie byłaby logika? ;D).

Poprawny kod:
  1. <?php
  2. function tabela($tab)
  3. {
  4. $ilosc = count($tablica);
  5.  
  6. $string = '<table border = 1>';
  7. for($i=0; $i<2; $i++)
  8. {
  9. $string .= "<tr><td>$tab[$i]</td></tr>";
  10. }
  11. $string .= '</table>';
  12.  
  13. return $string;
  14. }
  15.  
  16. $tablica = array('pierwszy', 'drugi');
  17. echo tabela($tablica);
  18. ?>
Pozdrawiam,
mmica

Ten post edytował mmica 13.12.2010, 17:27:09


--------------------
"Imagination is more important than knowledge, because knowledge is limited." ~Albert Einstein
<?php $var = array('foo', 'bar'); ?>
Go to the top of the page
+Quote Post
sazian
post
Post #5





Grupa: Zarejestrowani
Postów: 1 045
Pomógł: 141
Dołączył: 19.09.2006
Skąd: B-tów

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


  1. function tabela($tab)
  2. {
  3. $ilosc = count($tablica);
  4. $result= '<table border = 1>';
  5. for($i=0; $i<2; $i++)
  6. {
  7. $result.= "<tr><td>$tab[$i]</td></tr>";
  8. }
  9. $result.= '</table>';
  10. return $result;
  11. }
  12.  
  13.  
  14.  


edit:
spóźniony biggrin.gif

Ten post edytował sazian 13.12.2010, 17:29:28
Go to the top of the page
+Quote Post
flashdev
post
Post #6





Grupa: Zarejestrowani
Postów: 812
Pomógł: 117
Dołączył: 2.12.2008

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


Cytat(victor_19 @ 13.12.2010, 17:21:45 ) *
Nie działa ta funkcja z kilkoma returnami. Czemu?


Naprawdę nie działa?

Cytat(victor_19 @ 13.12.2010, 17:21:45 ) *
Chodzi mi o funkcję z samymi returnami bez echo...


No przykro mi, że nie chce Ci się nawet sprawdzić gotowego rozwiązania...


--------------------
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 Aktualny czas: 21.08.2025 - 14:26