PHP WampServer query

0

I have a doubt pass a code in php from a remote server to a local one to work in intranet. I am using WampServer 3.1.0 64bits and the code that used to work perfectly for me is now failing me. what I do is traverse a string and separate the letters of the number but I generate an error in all the lines where $ string appears [$ i] which for me does not make sense since it is already assumed to start the vector when passing the string in this case A1 so it would generate 2 memory fields $ string [0]="A" and $ string [1]="1" or at least that worked on the remote server. I do not know why it does not work now, what am I doing wrong?

Notice: Uninitialized string offset

$ CodGrupo is a parameter that leads by example A1

 $Grupo='';
        $Conteo='';
        $cadena='';
        $Retorna="";
        $CodInventario="";
        $CargarCodInventario=false;
        $longitud =0;
        $cadena=$CodGrupo;
        $longitud = strlen($CodGrupo);

        $longitud = strlen($CodGrupo);
        for ($i=0; $i<=$longitud ;$i++){
             if(is_numeric($cadena[$i])) {
                    //es numero
                    if($CargarCodInventario==true){
                        $CodInventario=$CodInventario.$cadena[$i];
                    }else{
                        $Conteo=$Conteo.$cadena[$i];
                    }
              }else{
                        //no es numerico
                    //busca el guion 
                    if($cadena[$i]=="-"){
                         $CargarCodInventario=true;
                    }else{
                        $Grupo=$Grupo.$cadena[$i];
                    }



            }


        }//fin for
    
asked by Luis Roberto Bastos 14.02.2018 в 16:08
source

1 answer

0

What happens is $longitud=2 when $CodGrupo = 'A1' and the loop will stop when $i is less than or equal to 2, so the index i equals 2 will continue to enter and $cadena 2 not is out of range. To solve this problem change the condition in your for of <= to < .

    
answered by 14.02.2018 в 16:22