TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-24-2008, 10:39 PM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default Type casting

When you make a variable look like a different data type, how is this done in php?

I mean if I try this:
PHP Code:
<?php

$string 
"Hello World";
$string  = (int) $string;

// This to me sounds like the concept of type casting in php
// Now this only gives me 0 cause theres EDIT: NO -_- integers
// How do I change it back to a string, an integer to a string
// Maybe I don't understand the concept

?>
__________________
VillageIdiot can have my babbies ;d

Last edited by Orc : 05-25-2008 at 06:21 PM.
Orc is offline  
Reply With Quote
Old 05-24-2008, 10:45 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

When typecasting, you loose all non-int data. You can not change it back to "Hello World".
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
Orc (05-25-2008)
Old 05-25-2008, 12:06 AM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
When typecasting, you loose all non-int data. You can not change it back to "Hello World".
Alright, I'll just keep using zlib compression, using gzdeflate and inflate.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-25-2008, 04:18 AM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

What are you trying to do?
__________________

Village Idiot is offline  
Reply With Quote
Old 05-25-2008, 11:55 AM   #5 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Type casting is not made so you can 'transform' strings into integers, arrays, objects, and so on. It's made so you can ENFORCE a certain data type. Its only use is that you can make sure you're getting a certain data type, so you can perform a certain operation. See the example:

PHP Code:
function sum_ints($a$b)
{
    
$a = (int) $a;
    
$b = (int) $b;

    return 
$a+$b;

It's obvious that the sum function can't be used on strings, so that's why we use type casting, so we can enforce the parameters to be integers. If non-integer values are passed in, the function will simply return 0 instead of crashing if you pass in objects as parameters, therefore not breaking application flow. Operator overloading would come useful in here, but that's another problem.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
Orc (05-25-2008)
Old 05-25-2008, 04:00 PM   #6 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by xenon View Post
Type casting is not made so you can 'transform' strings into integers, arrays, objects, and so on. It's made so you can ENFORCE a certain data type. Its only use is that you can make sure you're getting a certain data type, so you can perform a certain operation. See the example:

PHP Code:
function sum_ints($a$b)
{
    
$a = (int) $a;
    
$b = (int) $b;

    return 
$a+$b;

It's obvious that the sum function can't be used on strings, so that's why we use type casting, so we can enforce the parameters to be integers. If non-integer values are passed in, the function will simply return 0 instead of crashing if you pass in objects as parameters, therefore not breaking application flow. Operator overloading would come useful in here, but that's another problem.
Thanks, also Village_Idiot, my main priority in making dynamic SQL Applications, is security, then features, and I like to encrypt the strings that go into the table, and the such and such, like always, you can give me tips on MySQL, though I've learned pretty much all of it, EXCEPT all the of proper security really, besides using comments and quotes to block certain things out so someone could do an sql injection.

I'm usually the paranoid type, when it comes to working on security, I'm always scared that someone would destroy my application, :( that is why I backup logs every time there are new active queries. I am going to extreme measures to make my own language or add a new extension in php, using C/C++.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-25-2008, 06:08 PM   #7 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

If you are interested in changing the type of variable, you can check out C# (CSharp) documentations on Microsoft. Among the common there is string, bool, int, double and char.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 05-25-2008, 06:20 PM   #8 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by ReSpawN View Post
If you are interested in changing the type of variable, you can check out C# (CSharp) documentations on Microsoft. Among the common there is string, bool, int, double and char.
Huh? This is php. :P
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-25-2008, 09:23 PM   #9 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Doesn't mean that PHP doesn't use functions originally designed for C++ and C#..?
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 05-25-2008, 10:29 PM   #10 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Does typecasting specifically only make it where it changes to the data type and nothing more? Like the example I gave? :/ Like I want to change the data type back, I cannot now.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-25-2008, 10:30 PM   #11 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Posts: 666 - xD
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-26-2008, 03:10 AM   #12 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Quote:
Originally Posted by Orc View Post
Does typecasting specifically only make it where it changes to the data type and nothing more? Like the example I gave? :/ Like I want to change the data type back, I cannot now.
Type casting
delayedinsanity is offline  
Reply With Quote
Old 05-26-2008, 03:53 PM   #13 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

You can look at it like this.

If a variable was a room where a party was going on, the equal is the door, and the (int) typecast would be the guard at the door. ($room = (guard) $partygoer) He does not change the type of room it is or the people inside it, all he does is only let people with money go in. Same thing goes with that command, $var = (int) value; does not modify the variable, it filters what goes in.
__________________

Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:09 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design