There is an XML document, it is a parsing of field values, and then you need to know the type of value written in a string variable. To determine the type of value in a variable, wrote such a function.

public function getTypeValue($var) { if (is_numeric($var)) { if (is_int($var)) { return 'int'; }elseif (is_integer($var)){ return 'integer'; }elseif (is_long($var)){ return 'long'; }elseif (is_float($var)){ return 'float'; }elseif (is_double($var)){ return 'double'; }else{ return "len: " . strlen($var); } }else{ return 'string'; } } 

Whatever one may say, where there are strings, it returns string , where there are numbers (014982, 2, 3, 3.1.3, etc.) the function returns information on the length of the string but not the type. The else block for numbers made to check whether there are any extraneous characters in the string that would interfere. The results show that there is nothing outside there. If we take an ordinary array and fill it with data, the function works correctly, that is, it defines both strings and numbers of different types. How to overcome the situation?

XML processing code:

 public function parse() { $fullpath = PATH_XMLFOLDER . "/linecheck.xml"; $xml = simplexml_load_file($fullpath); $fields = $xml->xpath('//fields/*'); foreach ($fields as $field) { $fieldName = (string)$field->getName(); $value = (string)$field; $typeValue = $this->getTypeValue($value); SVTools::sv_debug($value,0); SVTools::sv_debug($typeValue,0); } } 

    2 answers 2

    After

     if (is_numeric($var)) { 

    put

      $var = 0 + $var; 

    PS is_int() === is_integer() === is_long() and is_float() === is_double()

    • if (is_numeric($var)) { executes correctly. - Dmitry Nail
    • About aliases of functions is known. - Dmitry Nail
    • Have you read the feature description? is_numeric () works both on the string and on the number. All other functions are only on numbers! For this line and translate into a number necessary. - Visman

    XML itself has no support for describing data types. Therefore, all scalar values ​​will be strings. Is always.

    To lead to some type of data and validate it accordingly, you should already be based on the knowledge of what exactly you want to find in this document.

    • Re-read the question ... it's about the values ​​and not about the types of variables. It's not about ghost types, but about the definition of the type Values. - Dmitry Nail
    • Once again: in XML there are only strings and no other types of scalars. Therefore, the type of the variable after the XML parser will always be string. - Small
    • Are you kidding me? I know this, your answer is inappropriate and comments for you do not correctly interpret the question. - Dmitry Nail
    • If you know that you get a string, then why checking the type of a variable are you surprised that it suddenly turned out to be not a number? You claim that the question is not about type casting and accept the answer about type casting. It is clear, of course - you just did not read the manual and really the question about is_int. My answer is that you don’t have to try to guess at all what type you need to cast a value. You already know that in such an element you expect to find an integer number - and on this basis it is necessary to check that there is an integer in the line, maybe in some range. The filter_var functions are intended for this. - Small
    • SO is not intended for general answers like yours, as well as for general questions. Therefore, you probably should have given a clear answer on how to resolve the situation technically, that is, by giving a ghost of your decision in the form of a code and not tips. - Dmitry Nail