Can I enforce function parameter typing?

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

Post Reply
User avatar
josamoto
Forum Commoner
Posts: 41
Joined: Fri Aug 24, 2007 6:57 am
Location: South Africa
Contact:

Can I enforce function parameter typing?

Post by josamoto »

I have the following function:

Code: Select all

<?php
function doSomething($withThis){
     // Do something $withThis 
     // ... for example - convert it to a string and echo it
}
?>
and I am calling it like this:

Code: Select all

<?php
doSomething(1234567890123456789012345678901234567890);
?>
PHP is interpreting the 1234567890... value as a double, meaning it displays as 1.2345678nnn+Exx when I convert it to a string. This is indeed the correct behaviour.

I want to convert the variable to a string and have PHP echo "1234567890123456789012345678901234567890", not scientific notation.

Oh, doSomething("1234567890...") with quotes is not an option, it has to be called without quotes if possible. You must be thinking: "Why in the hell is he trying to do that?"

Thanks for the help!
User avatar
deeessay
Forum Commoner
Posts: 55
Joined: Sat May 24, 2008 1:02 am

Re: Can I enforce function parameter typing?

Post by deeessay »

Try this:

function doSomething($withThis)
{
$convertedString = (string)$withThis;
return $convertedString;
}



<?php
echo doSomething(1234567890123456789012345678901234567890);
?>


hope this solves your problem...
Post Reply