Page 1 of 1

Are Type-Casting and SetType similar?

Posted: Sat Dec 27, 2008 8:10 pm
by Goldeneye
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?

Re: Are Type-Casting and SetType similar?

Posted: Sat Dec 27, 2008 10:44 pm
by Chris Corbyn
I'd never seen the settype() function, but it sounds like they do the same thing yes ;)

Re: Are Type-Casting and SetType similar?

Posted: Sun Dec 28, 2008 12:19 am
by Goldeneye
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?

Re: Are Type-Casting and SetType similar?

Posted: Sun Dec 28, 2008 1:26 am
by josh
Goldeneye wrote: It's just a redundancy.
Nope.

Code: Select all

 
$type = 'int';
// invalid: $a = ( $type )'5a';
settype( $a, $type ); //valid

Re: Are Type-Casting and SetType similar?

Posted: Sun Dec 28, 2008 1:53 am
by Goldeneye
jshpro2 wrote:
Goldeneye wrote: It's just a redundancy.
Nope.

Code: Select all

 
$type = 'int';
// invalid: $a = ( $type )'5a';
settype( $a, $type ); //valid
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).

Re: Are Type-Casting and SetType similar?

Posted: Sun Dec 28, 2008 10:40 pm
by Ambush Commander
Of course, you could always do it with eval. :twisted: