 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
05-24-2008, 10:39 PM
|
#1 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
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.
|
|
|
|
05-24-2008, 10:45 PM
|
#2 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
When typecasting, you loose all non-int data. You can not change it back to "Hello World".
|
|
|
|
|
The Following User Says Thank You to Village Idiot For This Useful Post:
|
|
05-25-2008, 12:06 AM
|
#3 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Village Idiot
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
|
|
|
|
05-25-2008, 04:18 AM
|
#4 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
What are you trying to do?
|
|
|
|
05-25-2008, 11:55 AM
|
#5 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
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.
|
|
|
|
|
The Following User Says Thank You to xenon For This Useful Post:
|
|
05-25-2008, 04:00 PM
|
#6 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by xenon
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
|
|
|
|
05-25-2008, 06:08 PM
|
#7 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
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"
|
|
|
05-25-2008, 06:20 PM
|
#8 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by ReSpawN
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
|
|
|
|
05-25-2008, 09:23 PM
|
#9 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
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"
|
|
|
05-25-2008, 10:29 PM
|
#10 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
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
|
|
|
|
05-25-2008, 10:30 PM
|
#11 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Posts: 666 - xD
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
05-26-2008, 03:10 AM
|
#12 (permalink)
|
|
is cute and cuddly
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
|
Quote:
Originally Posted by Orc
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 
|
|
|
|
05-26-2008, 03:53 PM
|
#13 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
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.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|