Jump to content

varables varables confussion help


redarrow

Recommended Posts

Hi!

 

I'd like to think that variable variables are a god bless! :D

 

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.

 

Link to comment
Share on other sites

It's strange. But simple.

 

I'll try and explain. :P

 

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! :D

 

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?

Link to comment
Share on other sites

It's strange. But simple.

 

I'll try and explain. :P

 

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! :D

 

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 :D  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 :D

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.