Cut whole number, some way to do?

0

I need to cut an integer value to be able to use in my application, previously the field was a string in my bank and now is as integer, is there any way to do this? I've been doing this until then, now I've had the need to cut a whole number.

$IdUF = substr($rowData['IdMunicipio'],0,2)

Or am I talking some bullshit? I need some tips.

    
asked by anonymous 21.03.2017 / 23:37

1 answer

2

PHP is not strongly typed

$IdUF = substr($rowData['IdMunicipio'],0,2)  returns a string
but if $IdUF is used in an operation as its sum value will be converted as follows:

<?php
$IdUF = substr($rowData['IdMunicipio'],0,2);
//$IdUF = "10" - Str
//Quando fazemos alguma operação
$IdUF++;
//$IdUF = 11 - Int

Now if the problem is at the time of saving in the bank the solution depends on how you are implemented.

One solution to forcing the value is to cast or convert between the solutions you could:

<?php
$IdUF = (int)substr($rowData['IdMunicipio'],0,2);
//ou
$IdUF = intval(substr($rowData['IdMunicipio'],0,2));
//Dessa forma o $IdUF será sempre inteiro, caso seja retornando 
//um valor que ele não consiga converter como "olá"
// o valor de $IdUF será 0.
    
22.03.2017 / 05:32