2

I've been trying to convert string to int in PHP to operate on it, but PHP keeps interpreting it as 0.

var_dump ($Pux); // output: string (6) "89" 

I have tried several ways like

(int) $Pux, intval($Pux) settype($Pux, "integer") 

But they all give me 0 instead of 89.

How should this be done?

11
  • @ManuelRamos: Are you sure the space didn't cause your error in the first place? Commented Aug 25, 2015 at 12:41
  • intval($Pux) seems to work for me. Are you sure this is the exact code you're running? Commented Aug 25, 2015 at 12:41
  • 1
    If you're casting, you'll have to reassign the result of that cast: $pux = (int) $pux;, That ought to work just fine. Don't forget to check gettype($pux); at any given time. You might be converting the type later on Commented Aug 25, 2015 at 12:41
  • Rolled back the edit, since I'd suspect that is the real cause for this problem. Commented Aug 25, 2015 at 12:42
  • 3
    string __(6)__ "89" What are the 4 invisible bytes? Commented Aug 25, 2015 at 12:49

6 Answers 6

3

There is not problem if you dont put spaces between $ and varname and assign the result of the cast to a variable.

<?php $Pux = "89"; var_dump ($Pux); $t = (int)$Pux; var_dump ($t); ?> 

Output =

string(2) "89" int(89) 
Sign up to request clarification or add additional context in comments.

5 Comments

See that 6 from the var_dump(): string (6) "89". They probably have some html tags or hidden characters there, which may be the reason why it fails.
Mmmmm that sounds possible.
@Rizier123 Well I just tried adding spaces, then alphas and then odd ALT+12,ALT+13,ALT+14 type chars and it all still works just fine. Any others suggestions I can try.
html tags, e.g. $str = "<xx>89";
@Rizier123 Yes of course, put the rubbish at the front of the string and it goes nicely BANG
0

The easiest way to convert a string to a number:-

<?php $Pux = "89"; // Or any other number $Num = (int) $pux; ?> 

If you are still getting a zero there might be some odd, invisible chars in front of the number. To test for that :-

echo "($Pux)";

2 Comments

You are coping on variable case sensitiveness right
OP commented the space between $ and Pux isn't there in the actual code
0

You can convert string to integer in two ways:

Typecasting or intval function

<?php $pux = "89"; var_dump($pux); $pux = (int) $pux; var_dump($pux); $pux = "89"; $pux = intval($pux); var_dump($pux); ?> 

Comments

0

The code below removes the non numeric characters::

function numeric( $st_data ) { $st_data = preg_replace("([[:punct:]]|[[:alpha:]]| )",'',$st_data); return $st_data; } 

1 Comment

Better use \D or [^0-9] for the search, as this matches all non-digits.
0

As var_dump($Pux); outputs string(6) "89" (not string(2)), it means your string contains 6 symbol, I think they are 4 single brackets (') and 89. So try to remove brackets from your string:

$Pux = str_replace("'", '', $Pux);

or remove double brackets too (for safe)

$Pux = strtr($Pux, array("'" => '', '"' => ''));

EDITED

will be good to remove all non numeric values:

$Pux = preg_replace("/[^0-9,.]/", "", $Pux); 

and then convert to integer.

Thanks.

8 Comments

$PUx = strtr($PUx, array("'" => '', '"' => '')); $aux = intval($PUx); var_dump($aux); output: int(0)
@ManuelRamos , what returns: $PUx = strtr($PUx, array("'" => '', '"' => '')); var_dump($PUx); ?
output: string(6) "89"
@ManuelRamos please run this code and share result for($i = 0; $i < strlen($PUx); $i++) { var_dump($PUx[$i]); echo '<br>'; }
string(1) "<" string(1) "t" string(1) "d" string(1) ">" string(1) "8" string(1) "9"
|
0

I had the same mistake. Indeed when i did var_dump($myvar). it returns string(2) "89" and when i did all the casts fonctions it returned me 0. I search and finally the answer is in this page. So I share with you what i learned. You have just to do a regex php : $myvar = preg_replace("/[^0-9,.]","",$myvar). It removes all the not number in your string (in my case the string(2)).

2 Comments

I missed a / in the end od the regex $myvar = preg_replace("/[^0-9,.]/","",$myvar)
You can edit your answer at any time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.