Page 1 of 2
Quick question from C++
Posted: Mon Feb 12, 2007 8:30 am
by superdezign
I'm very used to C++ and am still getting used to the differences in PHP. One that concerned me was typecasting. I have discovered that PHP can type cast similarly to C++, but also have functions to do these for me. I was wondering if I should break my habit of C-style typecasting, or if it's acceptable. For example:
Code: Select all
function roundUp(&$i)
{
if($i < (int) $i)
(int) $i++;
}
Code: Select all
function roundUp(&$i)
{
if($i < intval($i))
$i = intval($i) + 1;
}
Just curious.
Posted: Mon Feb 12, 2007 9:39 am
by RobertGonzalez
intval() does not change the type of the var, it returns the integer value of the var. You can still use the type casting syntax you posted to type cast in PHP.
Posted: Mon Feb 12, 2007 9:44 am
by Chris Corbyn
I sometimes typecast rather than validate. It's me being lazy but it's prefectly acceptable IMHO. I only do that in places where the result of say, a string "x" being cast as int would produce result which didn't break anything.
I often cast (array) where the parameter can optionally be a single value, or a collection of values, so that it's always a collection when I work with it.
Posted: Mon Feb 12, 2007 9:58 am
by Mordred
1. ceil()
2. I find intval() to be the more readable variant of the two, but IIRC someone here said that internally casting and calling the function do the same. Moreover (esp. if you find loose typing confusing) you may try some form of Hungarian notation (I use $nNumber, $sString, $aArray, $pObject, $bBoolean, $fFloat) to keep track of the variables.
Posted: Tue Feb 13, 2007 2:06 pm
by superdezign
Mordred wrote:you may try some form of Hungarian notation (I use $nNumber, $sString, $aArray, $pObject, $bBoolean, $fFloat) to keep track of the variables.
Way ahead of ya.
Posted: Tue Feb 13, 2007 5:09 pm
by Mordred
superdezign wrote:Mordred wrote:you may try some form of Hungarian notation (I use $nNumber, $sString, $aArray, $pObject, $bBoolean, $fFloat) to keep track of the variables.
Way ahead of ya.
I showed you mine, you show me yours

Posted: Tue Feb 13, 2007 5:23 pm
by superdezign
iInt, fFloat, cChar, oObject, CClass, aArray, bBool, strString
I've been dedicated to C++ for years. Habits die hard.

Posted: Tue Feb 13, 2007 5:41 pm
by feyd
Hungarian was one of the first things I tossed out the window when I moved to OOP. It's a lovely feeling.

Posted: Tue Feb 13, 2007 5:51 pm
by Weirdan
Mordred wrote:
2. I find intval() to be the more readable variant of the two, but IIRC someone here said that internally casting and calling the function do the same.
Through testing I found the intval to be about 10 times slower compared to (int). Maybe it is because intval understands hexadecimal strings.
Posted: Tue Feb 13, 2007 5:51 pm
by RobertGonzalez
I had never heard of it before coming here for help. When I first saw code that did that I thought to myself 'What the bloody hell is all this $iInt, $strString crap?!?!?!'
I still don't use it.
Posted: Tue Feb 13, 2007 5:59 pm
by superdezign
Well it was something that I kept in C++ and have stayed true to. Especially since PHP has the ability to suddenly change types... It helps me notice an accidental change.
Posted: Tue Feb 13, 2007 6:09 pm
by RobertGonzalez
I feel you. It just as always felt odd to me. Just like camelCasing. That seems reallyWierdToMeToSeeAllTheseUpperCaseLettersInAVarName.
Posted: Tue Feb 13, 2007 6:18 pm
by superdezign
That's weird to you? Know what gets me in php (and ajax)? Not setting the return type in the declaration. THAT bothers me.
Posted: Tue Feb 13, 2007 6:24 pm
by RobertGonzalez
C# does that, doesn't it? And so does Java if I am not mistaken...
Posted: Tue Feb 13, 2007 6:43 pm
by superdezign
Yes. Both based off of C/C++.
And just my two cents... Java Runtime... I feel so helpless programming in Java. JavaScript is nothing, but Java feels like C++... until you try to do anything. :-p