mkosmosports Posted December 9, 2007 Share Posted December 9, 2007 Hello, Im wondering about the usage of superglobals. Ive noticed a lot of tutorials tend to always put the superglobals variable into another variable. Ex. $testvar = $_POST['testvar']; If youre not planning to manipulate its value, wouldnt it make more sense (and potentially faster because you dont need to define another variable) to always just refer to $_POST['testvar']? Or are there some possible issues in doing this? Im hoping someone could clear this up for me. Thanks! mkosmosports Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 9, 2007 Share Posted December 9, 2007 In my opinion; I think it's faster when it comes to "integrating" your super globals in your code; to declare a variable and put the super global in it. I just think it's easier to type "$variable" multiple times; then to type $_POST['testvar'] multiple times. But I guess you could always "copy/paste" either one and it would be just as fast. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 Also, don't you want to check the $_POSTed data to make sure it's valid and sanitized? Would be easier to do that with a shorter version right? Yes, you could always do $_POST['data'] = trim (mysql_real_escape_string ($_POST['data'])); but wouldn't $data = be easier? Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 9, 2007 Share Posted December 9, 2007 as far as language construct concerns there are no issues doing this, the only issues there are are the person who is coding it, his motives/decisions although I have noticed this bug before in using naming $_POST variables you can't say $_POST['text.moretext'] in PHP 4 I think, last time I checked it changes that into 'text_moretext' , and replaces the dot Quote Link to comment Share on other sites More sharing options...
rab Posted December 9, 2007 Share Posted December 9, 2007 I think it depends on the programmers style of coding. Quote Link to comment Share on other sites More sharing options...
mkosmosports Posted December 9, 2007 Author Share Posted December 9, 2007 Thanks for your input everyone. I agree with your example revraz, but what about cases where your not manipulating the values, or the values are not user-inputted, like when using the $_SERVER or $_SESSION superglobals (I shouldnt of used the $_POST example in my intial post)? In those cases, does it really just boil down to preference and coding styles? (rab, it looks like you agree with that) I did read that as well regarding the $_POST superglobal dsaba. (it replaces all dots with underscores) Thanks again. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 Yes it would depend on style, but I personsally would still do $id = $_SESSION['id']; so I can work with $id within my page. Quote Link to comment 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.