How to change variable to 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: Show
answered Dec 16, 2011 at 4:13
deceze♦deceze 497k81 gold badges719 silver badges867 bronze badges 10 There are a few ways to do so:
answered Dec 16, 2011 at 4:12
fardjadfardjad 19.5k6 gold badges50 silver badges67 bronze badges 9 To avoid problems try
SharpC 6,4284 gold badges43 silver badges39 bronze badges
answered Jun 2, 2014 at 13:28
gopecagopeca 1,45311 silver badges12 bronze badges 4 In whatever (loosely-typed) language you can always cast a string to a number by adding a zero to it. However, there is very little sense in this as PHP will do it automatically at the time of using this variable, and it will be cast to a string anyway at the time of output. Note that you may wish to keep dotted numbers as strings, because after casting to float it may be changed unpredictably, due to float numbers' nature.
answered Dec 16, 2011 at 4:52
Your Common SenseYour Common Sense 156k39 gold badges208 silver badges331 bronze badges 9 Instead of having to choose whether to convert the
answered Jun 3, 2015 at 9:35
webNeatwebNeat 2,6481 gold badge18 silver badges22 bronze badges 1 Yes, there is a similar method in PHP, but it is so little known that you will rarely hear about it. It is an arithmetic operator called "identity", as described here: Aritmetic Operators To convert a numeric string to a number, do as follows:
answered May 10, 2018 at 18:31
5
If you want get a float for
It is little bit dirty, but it works.
answered Sep 15, 2014 at 4:44
9 You can use:
Or you can use:
answered Dec 16, 2011 at 4:13
noobie-phpnoobie-php 6,14715 gold badges49 silver badges94 bronze badges 6 In PHP you can use
answered Dec 16, 2011 at 4:11
Kashif KhanKashif Khan 2,56514 silver badges14 bronze badges 2 You can always add zero to it!
answered Jul 18, 2016 at 8:13
BoykodevBoykodev 8201 gold badge10 silver badges21 bronze badges 2 Just a little note to the answers that can be useful and safer in some cases. You may want to check if the string actually contains a valid numeric value first and only then convert it to a numeric type (for example if you have to manipulate data coming from a db that converts ints to strings). You can use
answered Aug 10, 2013 at 17:16
taseenbtaseenb 1,3481 gold badge15 silver badges30 bronze badges 1 Here is the function that achieves what you are looking for. First we check if the value can be understood as a number, if so we turn it into an int and a float. If the int and float are the same (e.g., 5 == 5.0) then we return the int value. If the int and float are not the same (e.g., 5 != 5.3) then we assume you need the precision of the float and return that value. If the value isn't numeric we throw a warning and return null.
answered Jan 1, 2016 at 19:51
If you want the numerical value of a string and you don't want to convert it to float/int because you're not sure, this trick will convert it to the proper type:
Source: https://www.php.net/manual/en/function.is-numeric.php#107326 answered Sep 29, 2020 at 8:11
klodomaklodoma 3,8831 gold badge27 silver badges39 bronze badges 1 In addition to Boykodev's answer I suggest this:
answered Oct 3, 2016 at 13:29
drugandrugan 7208 silver badges10 bronze badges 5 I've been reading through answers and didn't see anybody mention the biggest caveat in PHP's number conversion. The most upvoted answer suggests doing the following:
That's brilliant. PHP does direct casting. But what if we did the following?
Does PHP consider such conversions valid? Apparently yes. PHP reads the string until it finds first non-numerical character for the required type. Meaning that for integers, numerical characters are [0-9]. As a result, it reads Same would happen if you were to cast to float or double. PHP would read As a result, See also: https://www.php.net/manual/en/language.operators.comparison.php how conversions are done while comparing https://www.php.net/manual/en/language.types.string.php#language.types.string.conversion how strings are converted to respective numbers answered Jun 15, 2019 at 17:52
DragasDragas 9189 silver badges23 bronze badges 2 Only multiply the number by 1 so that the string is converted to type number.
answered Nov 2, 2019 at 15:44
rolodefrolodef 1071 silver badge3 bronze badges 3 Here is a function I wrote to simplify things for myself: It also returns shorthand versions of boolean, integer, double and real.
Calling type with parseNumeric set to true will convert numeric strings before checking type. Thus: type("5", true) will return int type("3.7", true) will return float type("500") will return string Just be careful since this is a kind of false checking method and your actual variable will still be a string. You will need to convert the actual variable to the correct type if needed. I just needed it to check if the database should load an item id or alias, thus not having any unexpected effects since it will be parsed as string at run time anyway. Edit If you would like to detect if objects are functions add this case to the switch:
answered Jan 23, 2014 at 20:30
Dieter GribnitzDieter Gribnitz 4,7222 gold badges40 silver badges36 bronze badges I've found that in JavaScript a simple way to convert a string to a number is to multiply it by 1. It resolves the concatenation problem, because the "+" symbol has multiple uses in JavaScript, while the "*" symbol is purely for mathematical multiplication. Based on what I've seen here regarding PHP automatically being willing to interpret a digit-containing string as a number (and the comments about adding, since in PHP the "+" is purely for mathematical addition), this multiply trick works just fine for PHP, also. I have tested it, and it does work... Although depending on how you acquired the string, you might want to apply the trim() function to it, before multiplying by 1.
answered Feb 25, 2014 at 14:58
1 Late to the party, but here is another approach:
answered Sep 10, 2020 at 10:46
BenniBenni 1,02311 silver badges14 bronze badges 1 Alright so I just ran into this issue. My problem is that the numbers/strings in question having varying numbers of digits. Some have no decimals, others have several.
So for me, using So, simple solution... divide the string by 1 server-side. This forces it to a number and retains all digits while trimming unnecessary 0's. It's not pretty, but it works.
answered Oct 21, 2021 at 0:13
MikelGMikelG 4093 silver badges16 bronze badges 4
You can use this to convert a string to an int in PHP.
answered Dec 16, 2011 at 4:13
PritomPritom 1,2548 gold badges19 silver badges37 bronze badges 2 You can use:
But the best solution is to use:
Or
answered Sep 29, 2013 at 19:18
Simply you can write like this:
answered Aug 28, 2018 at 11:20
3 There is a way:
Using Using the combination of
Will output: answered May 28, 2019 at 19:04
AnugaAnuga 2,30516 silver badges25 bronze badges 1 You can change the data type as follows
For historical reasons "double" is returned in case of a float. PHP Documentation
aksu 5,1815 gold badges23 silver badges39 bronze badges answered Sep 4, 2013 at 10:53
Now we are in an era where
strict/strong typing has a greater sense of importance in PHP, I use
answered Aug 21, 2021 at 19:49
AndyAndy 4,7365 gold badges33 silver badges56 bronze badges 2
answered Apr 11 at 10:53
HefHef 3383 silver badges11 bronze badges All suggestions lose the numeric type. This seems to me a best practice:
answered Apr 29, 2015 at 20:34
If you don't know in advance if you have a float or an integer,
answered Feb 12, 2020 at 17:44
4
answered Sep 9, 2020 at 18:54
How do I convert a string to a number?In Java, we can use Integer.valueOf() and Integer.parseInt() to convert a string to an integer.. Use Integer.parseInt() to Convert a String to an Integer. This method returns the string as a primitive type int. ... . Use Integer.valueOf() to Convert a String to an Integer. This method returns the string as an integer object.. Can you use numbers in variables PHP?A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive ( $age and $AGE are two different variables)
Can we use int in PHP?PHP Casting Strings and Floats to Integers
The (int), (integer), or intval() function are often used to convert a value to an integer.
How do you convert one variable type to another in PHP?The settype() function converts a variable to a specific type.
|