Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Pętla - gdzie błąd?
Przemo_
post
Post #1





Grupa: Zarejestrowani
Postów: 62
Pomógł: 1
Dołączył: 12.06.2007

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


Witam.

Mam oto taki kod

  1. <?php
  2. function udb_hash($string){
  3.  
  4. for(new x=0; x < strlen($string); x++){
  5.  
  6. $string[x] += (3^x) * (% 15);
  7. if($string[x] > (0xff)){
  8.  $string[x] -= 256;
  9.  }
  10.  }
  11.  }
  12. ?>


Gdy wchodzę na stronę, gdzie jest ta funkcja wyskakuje mi błąd

Kod
Parse error: syntax error, unexpected '=', expecting ';' in reg.php on line 16


Mógłbym mi ktoś powiedzieć, albo dać wskazówki dlaczego takowy błąd występuje ?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 7)
webdice
post
Post #2


Developer


Grupa: Moderatorzy
Postów: 3 045
Pomógł: 290
Dołączył: 20.01.2007




Ma być $x a nie x.
Go to the top of the page
+Quote Post
Przemo_
post
Post #3





Grupa: Zarejestrowani
Postów: 62
Pomógł: 1
Dołączył: 12.06.2007

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


  1. <?php
  2. function udb_hash($string){
  3.  
  4. for(new $x=0; $x < strlen($string); $x++){
  5.  
  6. $string[$x] += (3^$x) * ($x % 15);
  7. if($string[$x] > (0xff)){
  8.  $string[$x] -= 256;
  9.  }
  10.  }
  11.  }
  12. ?>


Dodałem $ i dalej nie działa.
Go to the top of the page
+Quote Post
PiXel2.0
post
Post #4





Grupa: Zarejestrowani
Postów: 110
Pomógł: 13
Dołączył: 16.03.2007
Skąd: Łódź

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


usun 'new' i 3^$x zamien na pow(3, $x)

Ten post edytował PiXel2.0 17.05.2008, 18:15:29
Go to the top of the page
+Quote Post
Cezar708
post
Post #5





Grupa: Zarejestrowani
Postów: 1 116
Pomógł: 119
Dołączył: 10.05.2005
Skąd: Poznań

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


coś mieszasz,

masz zmienną $string, którą potem używasz jako tablicę a w pętli podczas ażdej iteracji sprawdzasz to znotu jako string.

Proponuję małą zmianę:

  1. <?php
  2. function udb_hash($string){
  3.  
  4. $c = strlen($string);
  5. $result = array();
  6. for(new $x=0; $x < $c $x++){
  7. $result[$x] += (3^$x) * ($x % 15);
  8. if($result[$x] > (0xff)){
  9.  $result[$x] -= 256;
  10. }
  11.  }
  12.  return implode("", $result);
  13. }
  14.  
  15. // uzycie 
  16. $string = 'jakis string';
  17.  
  18. echo udb_hash($string);
  19. ?>


oczywiście mógłbyś jeszcze napisać co ta funkcja tak naprawdę ma robić byłoby łatwiej winksmiley.jpg

Pozdrawiam

~EDIT

ewentualnie zamiast $string[$x] możesz używać w tym przypadku $string{$x}... czyli inne nawiasy smile.gif

Ten post edytował Cezar708 17.05.2008, 18:19:44
Go to the top of the page
+Quote Post
Przemo_
post
Post #6





Grupa: Zarejestrowani
Postów: 62
Pomógł: 1
Dołączył: 12.06.2007

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


Po usunięciu new i zamiany wyskakuje mi taki błąd

Kod
Fatal error: Cannot use assign-op operators with overloaded objects nor string offsets in reg.php on line 18


@UP:
Jest to funkcja wyciągnięta z języka PAWNO, i przerobiona do php, ma ona za zadanie hashować hasła

Cała funkcja
  1. <?php
  2. function udb_hash($string){
  3.  
  4. for($x=0; $x < strlen($string); $x++){
  5.  
  6. $string[$x] += pow(3,$x) * ($x % 15);
  7. if($string[$x] > (0xff)){
  8.  $string[$x] -= 256;
  9.  }
  10.  }
  11.  }
  12.  
  13. $code = udb_hash($pass);
  14. echo $code;
  15. ?>


//Cezar Twój kod także nie działa
Kod
Parse error: syntax error, unexpected '=', expecting ';' in reg.php on line 19


Ten post edytował Przemo_ 17.05.2008, 18:25:33
Go to the top of the page
+Quote Post
Cezar708
post
Post #7





Grupa: Zarejestrowani
Postów: 1 116
Pomógł: 119
Dołączył: 10.05.2005
Skąd: Poznań

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


to działa dokładnie tak jak tamten algorytm, tyle że ten pod php.

  1. <?php
  2. function udb_hash($string) {
  3. $aString = str_split($string);
  4. $return = '';
  5. foreach ($aString as $x => $l){
  6. $int = ord($l) + pow(3, $x) * ($x % 15);
  7. if ($int > 255) {
  8. $int -= 256;
  9. }
  10. $return .= chr($int);
  11. }
  12. return $return;
  13. }
  14. echo udb_hash($pass);
  15. ?>


Pozdrawiam
Go to the top of the page
+Quote Post
Przemo_
post
Post #8





Grupa: Zarejestrowani
Postów: 62
Pomógł: 1
Dołączył: 12.06.2007

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


Big Thx.
Naprawdę mi pomogłeś!
+ leci!
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: 19.08.2025 - 20:24