1

I have a quick question about storing data in variable. This is out of curiosity. And also I want a clean code. Hope you may enlighten me about it.

In submitting a form in PHP, what is the difference if I store a POST data in a variable? example:

$username = $_POST['username'];

insert_user($username);

and

insert_user($_POST['username']);

I am currently using the first one, because I think it is much cleaner. But is there a performance impact, say, if I have 10 post data and store them in a variable?

Hope I explained myself clearly. Thank You.

Cheers!

2
  • 1
    There is absolutely no difference performance or otherwise unless the $_POST['variable'] contains a huge data, like for instance sending a base64 encoded image via form submission Commented Dec 6, 2018 at 2:51
  • @Viney Thanks. The values are just simple string values. If that is the case I think I'll keep using the first one. I think, it is much cleaner. Commented Dec 6, 2018 at 6:21

1 Answer 1

2

You are essentially copying the data to another variable. So yes, there is a performance impact but it's negligible. You will also be consuming more memory by copying the values, but that should not be a problem with small post requests.

Many frameworks will parse those values for you and return them in a more friendly way, stripping any XSS. For learning purposes, you could write your own functions like the example bellow:

function post($var) {

    if (empty($_POST[$var])) return '';

    return strip_tags($_POST[$var]);

}

insert_user(post('username'));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.