Thursday, October 20, 2011

PHP: Convert Long Integer to String

Sometimes long integer might automatic generate as a scientific notation/numerical format, when export to excel sheet for an example.
Eg: 725239016523 --> 7.25239E+11

To make sure all numbers are displayed, you may force the integer variable to string. There are many ways to convert it to string, below are some of the tricks, but might be more:-

<?php
$integer = 725239016523;
echo $integer . " is a ". gettype($integer) . "<br>"; // check type of $integer

$item = (string)$integer; // or
$item = strval($integer); // or
$item = trim($integer); // or
$item = $integer.""; // or
$item = $integer." "; // or
$item = $integer."&nbsp;"; // or
$item = "$integer"; // or ...

echo $item . ' is a '. gettype($item) . "<br>"; // check $integer whether is string or integer
?>


Refer: http://stackoverflow.com/questions/1035634/converting-an-integer-to-a-string-in-php

See also check type of a variable
sprintf() - Return a formatted string
gettype() - Get the type of a variable
is_bool() - Finds out whether a variable is a boolean
is_float() - Finds whether the type of a variable is float
is_int() - Find whether the type of a variable is integer
is_string() - Find whether the type of a variable is string
is_array() - Finds whether a variable is an array
is_object() - Finds whether a variable is an object
is_numeric() - Finds whether a variable is a number or a numeric string
is_scalar() - Finds whether a variable is a scalar
is_null() - Finds whether a variable is NULL
is_resource() - Finds whether a variable is a resource

1 comment:

  1. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. String functions php

    ReplyDelete