Quick question from C++

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Quick question from C++

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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 ;)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

iInt, fFloat, cChar, oObject, CClass, aArray, bBool, strString

I've been dedicated to C++ for years. Habits die hard. :o
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Hungarian was one of the first things I tossed out the window when I moved to OOP. It's a lovely feeling. :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I feel you. It just as always felt odd to me. Just like camelCasing. That seems reallyWierdToMeToSeeAllTheseUpperCaseLettersInAVarName.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

C# does that, doesn't it? And so does Java if I am not mistaken...
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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
Post Reply