Converting an Integer to a String in PHP

Converting an integer to a string in PHP

You can use the strval() function to convert a number to a string.

From a maintenance perspective its obvious what you are trying to do rather than some of the other more esoteric answers. Of course, it depends on your context.

$var = 5;

// Inline variable parsing
echo "I'd like {$var} waffles"; // = I'd like 5 waffles

// String concatenation
echo "I'd like ".$var." waffles"; // I'd like 5 waffles

// The two examples above have the same end value...
// ... And so do the two below

// Explicit cast
$items = (string)$var; // $items === "5";

// Function call
$items = strval($var); // $items === "5";

How do I convert a string to a number in PHP?

You don't typically need to do this, since PHP will coerce the type for you in most circumstances. For situations where you do want to explicitly convert the type, cast it:

$num = "3.14";
$int = (int)$num;
$float = (float)$num;

php converting string to integer unexpected behaviour

Casting to an integer using (int) will always cast to the default base, which is 10.

Casting a string to a number this way does not take into account the many ways of formatting an integer value in PHP (leading zero for base 8, leading "0x" for base 16, leading "0b" for base 2). It will simply look at the first characters in a string and convert them to a base 10 integer. Leading zeroes will be stripped off because they have no meaning in numerical values, so you will end up with the decimal value 10 for (int)"010".

Converting an integer value between bases using (int)010 will take into account the various ways of formatting an integer. A leading zero like in 010 means the number is in octal notation, using (int)010 will convert it to the decimal value 8 in base 10.

This is similar to how you use 0x10 to write in hexadecimal (base 16) notation. Using (int)0x10 will convert that to the base 10 decimal value 16, whereas using (int)"0x10" will end up with the decimal value 0: since the "x" is not a numerical value, anything after that will be ignored.

If you want to interpret the string "010" as an octal value, you need to instruct PHP to do so. intval("010", 8) will interpret the number in base 8 instead of the default base 10, and you will end up with the decimal value 8. You could also use octdec("010") to convert the octal string to the decimal value 8. Another option is to use base_convert("010", 8, 10) to explicitly convert the number "010" from base 8 to base 10, however this function will return the string "8" instead of the integer 8.

Convert integer into X characters string in PHP

I would use sprintf():

$string = sprintf( "%04d", $number);

Using this demo:

foreach( array( 4, 134) as $number) {
$string = sprintf( "%04d", $number);
echo $string . "\n";
}

You get as output:

0004
0134

Convert number to string in php

It's failing because it's prefixed with a 0, making PHP attempt to interpret it as an octal number, where 8 is not a valid octal digit as it parses the string, so you get 0.

The solution is to use a (string) cast or strval(), but you need to remove the leading zero from your definition of $code.

$code = 87326487326;
var_dump( $code, (string) $code, strval( $code));

This will output (on an x64 machine):

int(87326487326) string(11) "87326487326" string(11) "87326487326" 

In php, how to convert string value into an int?

To convert string to int in PHP, you can use Type Casting method or PHP built-in function intval().

<?php
$string = "56";
$int = intval( $string );
echo $int;
?>

PHP - Converting a String float to an Integer - Wrong value

First of all, read Is floating point math broken?

8.54 happens to be one of those numbers which can be written precisely in decimal, but not in binary, so (float)"8.54" will actually create a number very very close to, but slightly under, 8.54.

Multiplying that by 100 will create a number very very close to, but slightly under, 854. And casting to int will find the next smallest integer (e.g. 853.9 becomes 853, not 854).

I can think of two solutions:

  • After multiplying by 100, round to the nearest whole number, then convert to integer. Unlike 8.54, 854 itself can be represented precisely in floating point, so round(8.54 * 100) will give a number that is exactly 854, rather than slightly under it. So (int)round((float)"8.54" * 100) will give 854 as an integer.
  • Instead of multiplying by 100, remove the . from the string, and convert to int directly, e.g. (int)str_replace(".", "", "8.54"));


Related Topics



Leave a reply



Submit