# Converting String to Int in PHP

The easiest way to convert strings to numbers in PHP is to use the (int/float/double)\$variable cast, which directly converts the string to a number. You can also convert a string to a number using the intval(\$value, \$base), floatval(\$value), number_format(\$num, \$decimals) and settype(\$var, \$type) functions. The intval() function converts a string to an integer, and the floatval() function converts a string to a floating-point value. The number_format() function converts a string to a number and returns the formatted number on success; if it fails, the function throws an E_WARNING error. The settype() function is used to set or change the type of an existing variable. In this PHP String to Int example, we convert a string to a number using the intval() function. Below you can see more string-to-int conversion examples with detailed descriptions of each method. Click Execute to run the PHP String to Int Example online and see the result.
Converting String to Int in PHP Execute
``````<?php
\$string = "23";

echo intval(\$string);
?>``````
Updated: Viewed: 5358 times

### What is PHP?

PHP is an open source scripting language used for web development that can be embedded in HTML code. PHP is known for its flexibility and large developer community. PHP can run on many operating systems including Windows, Linux and macOS and supports multiple databases such as MySQL, PostgreSQL and SQLite.

### What is a string in PHP?

A PHP string consists of sequences of characters, where each character is represented by one byte. Since a byte can only be 256 characters long, Unicode strings are not natively supported in PHP. When creating a string in PHP, you can use single quotes ('...'), double quoted strings ("..."), and Heredoc syntax (<<<). PHP provides many built-in functions for replacing, splitting, concatenating, interpolating, and comparing strings. You can also convert string to lowercase, convert string to int, convert string to array, and find the length of the string.

### What is int in PHP?

In PHP, an integer (int) is a data type used to represent whole numbers without decimal points. Integers can be specified in decimal, hexadecimal, octal, or binary notation. PHP also automatically converts numeric strings to integers when used in a numeric context, but if the string is not a valid numeric string, PHP interprets it as 0.

### PHP Converting String to Int Examples

The following are examples of converting string to int in PHP:

##### Converting string to number using type casting

In PHP, we can change the type of a variable using the cast syntax: (int/float/double)\$variable. In most cases, this is not required since PHP does an implicit type conversion in most cases.

PHP String to Int Example
``````<?php
\$str = "23";

echo (int)\$str;
?>

#output: 23``````

##### Converting string to integer

To convert a string to an integer in PHP, you can use the intval() function:

PHP intval() Example
``````<?php
\$str = "15";

echo intval(\$str);
?>

#output: 15``````

##### Converting string to a floating-point number

To convert a string to a float in PHP, you can use the floatval() function:

PHP floatval() Example
``````<?php
\$str = "15.5";

echo floatval(\$str);
?>

#output: 15.5``````

##### Converting string to int by changing variable type

To set or change the type of a variable in PHP, you can use the settype(variable, type) function. The function takes two parameters: the variable we want to change and the type of the variable we need (integer, float, string, array, object, null).

PHP settype() Example
``````<?php
\$str = "791";

settype(\$str, 'integer');

echo \$str
?>

#output: 791``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.7828708,"math_prob":0.8873293,"size":2129,"snap":"2023-40-2023-50","text_gpt3_token_len":451,"char_repetition_ratio":0.16141176,"word_repetition_ratio":0.036931816,"special_character_ratio":0.21089713,"punctuation_ratio":0.14251782,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9710679,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-01T05:36:25Z\",\"WARC-Record-ID\":\"<urn:uuid:58bc662e-f847-4215-b62c-385e520cf369>\",\"Content-Length\":\"95215\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7e0aaa1c-03db-4e7f-8375-d7a355cc3d01>\",\"WARC-Concurrent-To\":\"<urn:uuid:608576ed-ae51-4f22-a59a-4dfc2a97e4e9>\",\"WARC-IP-Address\":\"104.26.6.224\",\"WARC-Target-URI\":\"https://reqbin.com/code/php/m2rrca1r/php-string-to-int-example\",\"WARC-Payload-Digest\":\"sha1:E5R7TGYTRVMCYWZYODVYVN4HS3WYJTWI\",\"WARC-Block-Digest\":\"sha1:OWNKNKY6KKJ4ADBZAAT3Q4UJOWFP4O5Z\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100276.12_warc_CC-MAIN-20231201053039-20231201083039-00176.warc.gz\"}"}