Learn how to Make Money Online | Free Tech Magazines
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Web Development > PHP

Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Article Tools Search this Article Display Modes

Typecasting in PHP

By pradeep pradeep is offline

On 30th August, 2006
Post Typecasting in PHP

ADVERTISEMENT
Show Printable Version Email this Page Subscription Add to Favorites Copy Typecasting in PHP link

Author

pradeep ( Team Leader )

Yet to provide details about himself


All articles By pradeep

Recent Articles

Similar Articles

PHP will automatically convert data types as necessary across the board - you need not worry about it happening. If you specifically wish to override PHP's type conversion, you can perform what is called a type cast - you forcibly convert a variable of type A to type B. In PHP, type casting looks like this:

Code: PHP
<?php
    $mystring = "wombat";
    $myinteger = (integer)$mystring
?>

At first, $mystring contains a string. However, we type cast it to be an integer, so PHP will convert it to an integer, and place the result into $myinteger. You can type cast as boolean using (bool), string using (string), and floating-point using (float).

Type casting is most often used to specifically enforce a type in order to provide extra security or just to make sure a set type of data is being used. For example, if your script absolutely requires an integer number, it's a smart move to typecast your variable with (integer) so that PHP will convert any other type to integer or do nothing if the type is already integer. Converting a float to an integer will automatically round the number down, and is actually faster than using the equivalent function.
Old 03-14-2007, 05:08 PM   #2
gunwant
Newbie Member
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
gunwant is on a distinguished road

Re: Typecasting in PHP


Hi pradeep
i want to typecast a string to inteiger ,
c se this code
$date = new DateTime($jdate);

$startDay =(integer)date("d", strtotime($jdate));


//echo $jdate."*****".$startDay ;

//$date->modify("+1 day");
//echo $date->format("Y-m-d");
if($startDay<15) $Balance=1.5;
else if($startDay>14 && $startDay< 25) $Balance=1;
else if($startDay>24 && $startDay<32) $Balance=0.5;

giving an error on last line, error message is ,
parse error, unexpected '<' in C:\\Program Files\\sugarcrm-4.2.0c\\htdocs\\sugarcrm\\modules\\HR\\submitEmplo yee.php on line 103,

please suggest
gunwant is offline   Reply With Quote
Old 03-14-2007, 05:17 PM   #3
pradeep
Team Leader
 
pradeep's Avatar
 
Join Date: Apr 2005
Location: Kolkata, India
Posts: 1,461
Thanks: 0
Thanked 19 Times in 16 Posts
Rep Power: 6
pradeep will become famous soon enough
Send a message via Yahoo to pradeep

Re: Typecasting in PHP


Well, i tried running the code, it does not give any such error, post the whole code!
__________________
Vote for the Most Entertaining Member of 2008

To err is human,to detect is divine!
pradeep is offline   Reply With Quote
Old 03-14-2007, 08:28 PM   #4
gunwant
Newbie Member
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
gunwant is on a distinguished road

Re: Typecasting in PHP


thanks to repluy pradeep
I tried this way
(c the second & third line)
it is working fine
but i wan to know that whay it is not workin with earlier code

$date = new DateTime($jdate);

$temp =date("d", strtotime($jdate));

$startDay=(int)$temp;
//echo $jdate."*****".$startDay ;

//$date->modify("+1 day");
//echo $date->format("Y-m-d");
if($startDay<15) $Balance=1.5;
else if($startDay>14 && $startDay< 25) $Balance=1;
else if($startDay>24 && $startDay<32) $Balance=0.5;
gunwant is offline   Reply With Quote
Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
PHP On-The-Fly! Kings PHP 5 05-01-2008 12:28 PM
Track your visitors, using PHP Kings PHP 8 12-11-2006 02:53 AM
Optimize file uploads in PHP pradeep PHP 0 02-20-2006 01:03 PM
PHP and Cookies; a good mix! Kings PHP 1 12-07-2004 09:53 AM
Begining with PHP shabbir PHP 0 07-15-2004 04:22 PM

 

All times are GMT +5.5. The time now is 05:54 AM.