Convert string number to PHP integer

Asked

Viewed 49,921 times

7

I’m converting a string to an integer in PHP, but at the return of settype($variavel, "integer"), or (int)$variavel bring me a null value or equal to 1.

Have another way to try the conversion?

The code I’m in trouble with is this:

<script type="text/html" id="javo-map-tab-infobx-content">
 <div class="btn-group btn-group-justified pull-right">
                    <a id="botaoBrief" class="btn btn-primary btn-sm" onclick="window.javo_map_tab_func.brief_run(this);" data-id="{post_id}">
                        <i class="fa fa-user"></i> <?php _e("Briefaaaa", "javo_fr"); ?>
                    </a>

<?php
$idPost = '{post_id}';

var_dump($idPost); // resultado->   string(9) "5266"

$idPost = intval($idPost); //resultado-> 0

echo gettype($idPost); //resultado-> integer
  • intval($idPost); Worked for me

  • Greetings to you, Welisson. We are at Stack Overflow in Portuguese, so you can speak in Portuguese yourself; besides, your suggestion has already been given in the other answers, so it wasn’t very clear what you wanted to add with your answer. If it was just a report that worked, you can simply use the website’s voting system. Learn more by doing the [tour] and going to [help].

3 answers

13


If you are returning 1 through the intval may be the fact that the variable is an array. Note this. Make sure the variable is a string.

Example:

$variavel = '200';
$variavel = intval($variavel);

This will return: 200

If it is:

$variavel = array('foo'=>'200');

$variavel = intval($variavel);

This will return: 1

If possible I could put the variable debug here?

Debug like this:

   if(is_array($idPost))
        echo 'Array';
    else
        echo 'Not Array';

die;

5

settype returns whether the variable type has been successfully changed(true or 1), null or 0 to failure. You can cast to int.

<?php
   $variavel = (int) '200';
   echo gettype($variavel)

4

Use the function intval.

intval("123"); // retorna 123

Documentation: http://php.net/manual/en/function.intval.php

Other functions of the same type:

boolval($val);  // retorna o valor de $val convertido para um booleano
floatval($val); // retorna o valor de $val convertido para um float
strval($val);   // retorna o valor de $val convertido para um string
  • Using intval the feedback I’m getting is 1.

  • What is the content of $variavel?

  • I’m getting an id from a wordpress post. It comes to me as string, example $variable = '5625''

  • Give a var_dump in that variable to know the type and content of what you are receiving.

  • The value received is a String. The answer I got was: string(9) "5266"

  • 1

    Rodrigo’s answer is correct. If it returns 1 to you and the variable contains "5266", then you have some problem before the intval().

  • Daniel the problem is that there is no code between this variable that I receive from wordpress and the moment I make the conversion.

  • @Giuliocosta posts part of the code in the question.

  • @Rodrigorigotti posted the code and the returns I’m getting.

  • boolval only works for larger versions that 5.5.0

  • Check that the quotes are not coming together with the number as an element of your string and counting in the var_dump().

  • intval() should not be used on objects, as this will issue an E_NOTICE level error and return 1.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.