Are Type-Casting and SetType similar?
Moderator: General Moderators
Are Type-Casting and SetType similar?
I was just looking through the PHP Manual for ways to verify that my variables are strings, numbers, integers, etc. And came across Type-Casting. Then I came across the settype() function, and now I'm just curious, they do (essentially) the same thing, correct?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Are Type-Casting and SetType similar?
I'd never seen the settype() function, but it sounds like they do the same thing yes 
Re: Are Type-Casting and SetType similar?
http://ca3.php.net/manual/en/function.settype.php
Alright, that's what I thought too. It's just a redundancy. Thanks
Oh one more question (this is a bit of tangent):
Say you have a session variable named 'key' ($_SESSION['key']) (which is to used SQL queries) and you assign it upon a one-time script execution (such as logging-in) as $_SESSION['key'] = (string)$database['key'];
Would it better to assign a type-cast upon initialization, or type-cast it solely when using it in queries?
Alright, that's what I thought too. It's just a redundancy. Thanks
Oh one more question (this is a bit of tangent):
Say you have a session variable named 'key' ($_SESSION['key']) (which is to used SQL queries) and you assign it upon a one-time script execution (such as logging-in) as $_SESSION['key'] = (string)$database['key'];
Would it better to assign a type-cast upon initialization, or type-cast it solely when using it in queries?
Re: Are Type-Casting and SetType similar?
Nope.Goldeneye wrote: It's just a redundancy.
Code: Select all
$type = 'int';
// invalid: $a = ( $type )'5a';
settype( $a, $type ); //validRe: Are Type-Casting and SetType similar?
Thanks for the clarification. That makes sense seeing as the first (invalid) method requires a constant not a string (as $type would considered to be).jshpro2 wrote:Nope.Goldeneye wrote: It's just a redundancy.Code: Select all
$type = 'int'; // invalid: $a = ( $type )'5a'; settype( $a, $type ); //valid
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Re: Are Type-Casting and SetType similar?
Of course, you could always do it with eval. 