Click to See Complete Forum and Search --> : Variable swapping
bokeh
06-20-2006, 04:56 AM
$myCookie = $_COOKIE['val'];
$breed = $_POST['dog'];
$file = $_FILE['myfile']['name'];
Is there any good reason why people do this? As far as I see it it is pretty pointless and can only serve to make your code hard to decipher for anyone trying to help you, forcing them to continually backtrack or guess to try to discover where the variable originated!
In my opinion, the only time a new variable should be set is when the contents of the original should not be worked on directly but doing something like the following seems ridiculous, although it is commonplace.
<?php
# Is this really necessary?
$breed = $_POST['dog'];
echo $breed;
# Why not just do this
echo $_POST['dog'];
?>The first example is extremely difficult to follow especially if there are a few lines of code separating the two.
What are your thoughts?
GaryS
06-20-2006, 04:59 AM
Quite agree. Think it sometimes happens when people turn off register globals: fastest way to "fix" the code its to do a spot of "variable swapping" at the top of the code.
NogDog
06-20-2006, 05:16 AM
I usually don't do that, but I've done it occaisionally in situations where I plan to use the value in multiple places and don't want to have to bother with complex variable notation or string concatenation, especially if I'll be using it in multiple strings.
$breed = $_POST['breed'];
// this is a lot easier to type several times (and a bit easier to read)...
echo "<p>You selected $breed</p>\n";
// than these are:
echo "<p>You selected {$_POST['breed']}</p>\n";
echo "<p>You selected " . $_POST['breed'] . "</p>\n";
However, the code would probably be more clear if you made the new variable's name something that made it clear it came from the post values, maybe $P_breed or some other convention you want to use. In fact, you could just use the extract() function with the desired prefix and grab all your post variables in one fell swoop:
extract($_POST, EXTR_PREFIX_ALL, "P");
bokeh
06-20-2006, 07:09 AM
However, the code would probably be more clear if you made the new variable's name something that made it clear it came from the post values, maybe $P_breed or some other convention you want to use.Well that is the crux of it I guess. The big problem is that people use names that are often completely different from the source name.
acemo
06-20-2006, 07:48 AM
Well that is the crux of it I guess. The big problem is that people use names that are often completely different from the source name.
Because ppl dont know the extract function.
I didnt knew the extract function either..
I see it is quite usefull and will most likely use it sometimes.
Thanks for the explain nogdog!
NogDog
06-20-2006, 08:08 AM
Well that is the crux of it I guess. The big problem is that people use names that are often completely different from the source name.
Hmmm...somehow the renaming part in your original post escaped me. I claim innocence due to insomnia. :rolleyes:
chazzy
06-20-2006, 11:52 AM
Most issues like this go away when you develop your web apps in an object oriented manner.
<?php
include("/classes/mypage.class");
$thispage = new mypage($_POST);
$thispage->render();
?>
Then you never really see the $_POST array or the $_GET array, instead you use whatever local variables might be.
But in all honesty, it shouldn't matter what the post array's values are - that's not really important to the processor, but instead what it signifies. I use functions like this all the time:
function getElement(){
return $this->element." ".$this->color;
}
my 2 cents..