How to make a string number PHP

Learn how to convert strings to integers in PHP with an example: convert a string of digits to a number, cast a float to an integer, and more.

Creating a String Number in PHP

A string number is a string that contains a numerical value. In the PHP programming language, a string number can be created using the strval() function. This function will convert any type of value (integer, float, or boolean) into a string number.

For example, the following code snippet will create a string number from an integer:

$int_value = 10;
$string_number = strval($int_value);
echo "String Number: $string_number";
// Outputs: String Number: 10

The following code snippet will create a string number from a float:

$float_value = 3.14;
$string_number = strval($float_value);
echo "String Number: $string_number";
// Outputs: String Number: 3.14

Lastly, the following code snippet will create a string number from a boolean value:

$boolean_value = true;
$string_number = strval($boolean_value);
echo "String Number: $string_number";
// Outputs: String Number: 1

As you can see from the examples above, the strval() function can be used to convert different types of values into string numbers.

Answers (0)