redarrow Posted November 12, 2008 Share Posted November 12, 2008 I dont quite understand varables varables... can u kindly tell me, when it used or when to use it... some examples would be grate thank u...... my guess off use .... <?php $age="35"; $b="age"; echo "my $b is ${$b}"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/ Share on other sites More sharing options...
JasonLewis Posted November 12, 2008 Share Posted November 12, 2008 I don't really see the use of them either. Arrays seem more appropriate to me. Read about them on the manual. I still don't think it's very useful. Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688380 Share on other sites More sharing options...
redarrow Posted November 12, 2008 Author Share Posted November 12, 2008 i agree (i dont use them) just really wanted to no why there, there , it just every book gives a short example thort i was missing somethink..... i think the concept is very frushstrating........ Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688382 Share on other sites More sharing options...
NikkiLoveGod Posted November 12, 2008 Share Posted November 12, 2008 Hi! I'd like to think that variable variables are a god bless! They are awesome. For example, if you have a from with field like $id . '_name' and stuff like that, you can create a loop and make those variables into $name = $value, and do something with its ID. for example: <?php foreach($_POST as $key => $value) { $key = explode('_', $key); $id = $key[0]; $field = $key[1]; //Do something with them. Put them into a DB for example according to the id or smtn. } ?> And I think these are just ANOTHER way of doing something. I like these a bit more than just arrays, but im sure you can get the same result with arrays. Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688397 Share on other sites More sharing options...
PFMaBiSmAd Posted November 12, 2008 Share Posted November 12, 2008 Variable variables are 3x slower than using an array. Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688405 Share on other sites More sharing options...
JasonLewis Posted November 12, 2008 Share Posted November 12, 2008 It's strange. But simple. I'll try and explain. You have a variable called $color. And you give that variable the value of red: $color = "red"; Now say you wanted to make a variable called red, you could do it like this: $red = "color"; Or you could use variable variables: $$color = "color"; //Makes a variable: $red = "color" So basically, you create a new variable with the name being the value of the other variable. Make sense? You can also do it with arrays. Say you have an array: $colors = array("red"); The same things applies, except according to the manual you need to make sure you're referencing an index from an array or applying an index to the new array. So to create the same variable as we did above ($red = "color") you can do this: ${$colors[0]} = "color"; Now if you went: echo $red; You would get an output of 'color'. That's my explanation, which is basically the same thing as what php.net has. Don't know if I made it any easier. Don't think to hard on it or you may confuse yourself even more. Hi! I'd like to think that variable variables are a god bless! They are awesome. For example, if you have a from with field like $id . '_name' and stuff like that, you can create a loop and make those variables into $name = $value, and do something with its ID. for example: <?php foreach($_POST as $key => $value) { $key = explode('_', $key); $id = $key[0]; $field = $key[1]; //Do something with them. Put them into a DB for example according to the id or smtn. } ?> And I think these are just ANOTHER way of doing something. I like these a bit more than just arrays, but im sure you can get the same result with arrays. Would you call that variable variables? Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688406 Share on other sites More sharing options...
redarrow Posted November 12, 2008 Author Share Posted November 12, 2008 Thank u ProjectFear....... Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688409 Share on other sites More sharing options...
PFMaBiSmAd Posted November 12, 2008 Share Posted November 12, 2008 Think "indirect pointer". Anything you can do with variable variables can be done with arrays, but the code for the array version will be shorter, simpler, clearer, and execute faster. Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688415 Share on other sites More sharing options...
redarrow Posted November 12, 2008 Author Share Posted November 12, 2008 Agree getting into arrays cheers..... Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688420 Share on other sites More sharing options...
JasonLewis Posted November 12, 2008 Share Posted November 12, 2008 I don't know how you could prefer variable variables over arrays. Arrays are loads easier to manage, manipulate and use! Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688426 Share on other sites More sharing options...
NikkiLoveGod Posted November 12, 2008 Share Posted November 12, 2008 It's strange. But simple. I'll try and explain. You have a variable called $color. And you give that variable the value of red: $color = "red"; Now say you wanted to make a variable called red, you could do it like this: $red = "color"; Or you could use variable variables: $$color = "color"; //Makes a variable: $red = "color" So basically, you create a new variable with the name being the value of the other variable. Make sense? You can also do it with arrays. Say you have an array: $colors = array("red"); The same things applies, except according to the manual you need to make sure you're referencing an index from an array or applying an index to the new array. So to create the same variable as we did above ($red = "color") you can do this: ${$colors[0]} = "color"; Now if you went: echo $red; You would get an output of 'color'. That's my explanation, which is basically the same thing as what php.net has. Don't know if I made it any easier. Don't think to hard on it or you may confuse yourself even more. Hi! I'd like to think that variable variables are a god bless! They are awesome. For example, if you have a from with field like $id . '_name' and stuff like that, you can create a loop and make those variables into $name = $value, and do something with its ID. for example: <?php foreach($_POST as $key => $value) { $key = explode('_', $key); $id = $key[0]; $field = $key[1]; //Do something with them. Put them into a DB for example according to the id or smtn. } ?> And I think these are just ANOTHER way of doing something. I like these a bit more than just arrays, but im sure you can get the same result with arrays. Would you call that variable variables? Haha! sorry about that! I was thinking a bit too closely on what I did earlier on. So what I would do, is something like foreach($_POST as $key => $value) { //Do some altering to they keys, like put them all to caps, or smalls, //replace some text or something like that //$_POST['POST']; $key = strtolower($key); //Then assign them $$key = $value; } //and now you can access them like normal variables, but with lowercase echo $post; hehe, sorry to make up a bad examples But anyway, in that the idea is that you make some alters to the keys and then you can use em more easily. I hope I didnt make things more complicated Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688430 Share on other sites More sharing options...
NikkiLoveGod Posted November 12, 2008 Share Posted November 12, 2008 Oh, and I do not prefer variable variables over arrays in all situations, just where I have to echo alot of stuff, I would rathe write $name than $array['name']. But to store the data, then ofcourse arrays. Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688435 Share on other sites More sharing options...
JasonLewis Posted November 12, 2008 Share Posted November 12, 2008 Yeah I see what you mean. I still prefer the array approach to all things. Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688439 Share on other sites More sharing options...
PFMaBiSmAd Posted November 12, 2008 Share Posted November 12, 2008 Blindly looping over external data (post/get/cookie) and creating variables and assigning values to them, will allow a hacker to inject values into your program variables, such as saying he is logged in or is an administrator on your site. You should add a unique prefix to the variable names being created so that they cannot replace any existing program variables or simply use the extract() function with the EXTR_PREFIX_ALL option. Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688457 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 Honestly I never use variables variables. Just there is no point to it, slower and if you ask me that is a ton more complex than just using an array as stated earlier. Just my 2 cents. Quote Link to comment https://forums.phpfreaks.com/topic/132409-varables-varables-confussion-help/#findComment-688494 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.