Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Netscape Declaring a New Integer Variable

    Hey guys,

    I know PHP is a loosely-written language, but is there a way to declare a new variable that's an integer besides:

    $var;

    In C++, you would go:

    int var;

    Is there something like this in PHP so that you can declare a variable of a specific data type, like string, integer, float, etc.?

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    PHP Code:
    <?php
    $foo 
    "5bar"// string
    $bar true;   // boolean

    settype($foo"integer"); // $foo is now 5   (integer)
    settype($bar"string");  // $bar is now "1" (string)
    ?>
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • Users who have thanked Arcticwarrio for this post:

    JAG (03-04-2013)

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    though bear in mind that the data type once set is still changeable, i.e. the next time you (re)assign the variable, it will have the type of that (new) value.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    JAG (03-04-2013)

  • #4
    JAG
    JAG is offline
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thanks Arcticwarrio and Dormilich!

  • #5
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,376
    Thanks
    263
    Thanked 32 Times in 31 Posts
    i always use something like

    $value = intval($whatever);

    Is that the same i have not yet used settype?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    These all have different uses.
    Settype will change the $var zval to that of the type given. So if I originally had a double, and used settype on it to cast to an integer, it will permanently become an integer and truncate the floating point (by changing which union type it is using).

    Cast notation [(int)] beats intval() performance wise. But intval does let you convert a string number into an integer with base conversion which (int) does not. So I can convert 0123 to the correct 83 using intval(), but (int) would result in 123.

    So, if you want to permanently change (string)$var type to integer, use settype. If you want to change it as you reassign to a different variable (or I guess even to itself), you can use the (cast) notation. If you want to use potential conversion of something like an octal, use intval. These all go for pretty much every *val() type. Also, settype is the only one that can really be used dynamically logic wise. You cannot cast to a constant or variable type, but settype will accept a string.
    Actually I lied, the *val() will too since these are not constructs. So you can use variable functions to call them. (cast) notation is the only one that's actually locked into being hand written. I'm fine with that though, I typically use (cast) syntax.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •