Visualizzazione dei risultati da 1 a 4 su 4

Discussione: Strano errore funzione

  1. #1

    Strano errore funzione

    Ciao,
    ho una funzione che deve controllare dei dati passati da un form...
    Stranamente funziona solo se la variabile è vuota oppure e corretta, ma nel controllo se è troppo corta o troppo lunga non assegna all'array errore i corretti valori:

    Codice PHP:
    private function validaDati(){
            
    $this->dati["Errori"] = false;
                
    // CONTROLLO IL NOME
                
    if($this->dati["Nome"] == ""){
                    
    $this->dati["Errori"] = true;
                    
    $this->dati["Nome"]["Valore"] = $this->dati["Nome"];
                    
    $this->dati["Nome"]["Errore"] = true;
                    
    $this->dati["Nome"]["ErroreDesc"] = "Non hai inserito il nome!";
                } else if(
    strlen($this->dati["Nome"])<= 2){
                    
    $this->dati["Errori"] = true;
                    
    $this->dati["Nome"]["Valore"] = $this->dati["Nome"];
                    
    $this->dati["Nome"]["Errore"] = true;
                    
    $this->dati["Nome"]["ErroreDesc"] = "Nome troppo corto!";
                } else if(
    strlen($this->dati["Nome"])>= 15){
                    
    $this->dati["Errori"] = true;
                    
    $this->dati["Nome"]["Valore"] = $this->dati["Nome"];
                    
    $this->dati["Nome"]["Errore"] = true;
                    
    $this->dati["Nome"]["ErroreDesc"] = "Nome troppo lungo!";
                } else {
                    
    $this->dati["Nome"]["Valore"] = $this->dati["Nome"];
                }
        return 
    $this->dati;
        } 
    Se io inserisco a nel form lui mi restituisce questo:
    array(4) { ["Nome"]=> string(1) "N" ["Cognome"]=> string(0) "" ["Mail"]=> string(0) "" ["Errori"]=> bool(true) }

    Come mai?

  2. #2
    Inoltre fa un'altra cosa strana: se io inserisco un nome corretto nel restituirmelo mangia la prima lettera (Lorenzo diventa orenzo)...



  3. #3
    Il problema sta nel mischiare array e stringhe senza una corretta inizializzazione.

    Studiati questo codice:

    Codice PHP:
    <pre>
    <?php 

    error_reporting
    (E_ALL);
    ini_set('display_errors'1);

    $stringa 'stringa';
    var_dump($stringa);

    $stringa['valore'] = 'test';
    var_dump($stringa);

    print(
    "\r\n");

    $stringa 'stringa';
    var_dump($stringa);

    $array['valore'] = 'test';
    $stringa $array;
    var_dump($stringa);

    ?>
    </pre>

  4. #4
    Quindi se io quando creo l'array creo anche gli argomenti (inizialmente vuoti) che aggiungo dopo li ho correttamente inizializzati. Giusto?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.