Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Pętla - gdzie błąd?
Forum PHP.pl > Forum > Przedszkole
Przemo_
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 ?
webdice
Ma być $x a nie x.
Przemo_
  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.
PiXel2.0
usun 'new' i 3^$x zamien na pow(3, $x)
Cezar708
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
Przemo_
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
Cezar708
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
Przemo_
Big Thx.
Naprawdę mi pomogłeś!
+ leci!
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.