Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-27-2013, 08:27 AM   PM User | #1
JAG
New Coder

 
Join Date: Feb 2011
Posts: 51
Thanks: 17
Thanked 0 Times in 0 Posts
JAG is an unknown quantity at this point
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.?
JAG is offline   Reply With Quote
Old 02-27-2013, 09:00 AM   PM User | #2
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 597
Thanks: 15
Thanked 67 Times in 67 Posts
Arcticwarrio is on a distinguished road
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.
Arcticwarrio is offline   Reply With Quote
Users who have thanked Arcticwarrio for this post:
JAG (03-04-2013)
Old 02-28-2013, 07:53 AM   PM User | #3
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Users who have thanked Dormilich for this post:
JAG (03-04-2013)
Old 03-04-2013, 09:40 AM   PM User | #4
JAG
New Coder

 
Join Date: Feb 2011
Posts: 51
Thanks: 17
Thanked 0 Times in 0 Posts
JAG is an unknown quantity at this point
Thumbs up

Thanks Arcticwarrio and Dormilich!
JAG is offline   Reply With Quote
Old 03-04-2013, 10:04 AM   PM User | #5
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
i always use something like

$value = intval($whatever);

Is that the same i have not yet used settype?
durangod is offline   Reply With Quote
Old 03-04-2013, 02:58 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
declare, int, php, specific, variable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:13 PM.


Advertisement
Log in to turn off these ads.