1

Hello, I have a short question. I would like to reset a variable that is going to be posted:

I will post a variable that comes from an input field. When this variable $_POST['a']; is empty, I would like to fill it with new content.

The scheme is like that:

empty variable will be posted -> php recognizes that it is empty -> giving it new content -> echoing out new content.

This is just simple but it doesnt work for me.

So here is what I have:

$a = $_POST['a'];

if( empty($a){
    $errors['a'][]="text";
    $a =="new content";
}

So if there is someone whould could tell me whats being wrong with this, I really would appreciate. Thanks a lot.

3
  • 6
    $a =="new content"; doesn't look right. You probably want 1 equal sign for an assignment instead of a comparison. Commented Jul 27, 2012 at 12:49
  • 1
    = and == is different operators. You should use = in assignment and == in compassion. Commented Jul 27, 2012 at 12:50
  • also tried with === and leaving quotation marks just semicolons... Commented Jul 27, 2012 at 12:50

3 Answers 3

11

Three things:

$a = isset($_POST['a']) ? $_POST['a'] : ''; // always check if array index exists

if( empty($a)) { // one more bracket
    $errors['a'][]="text";
    $a ="new content"; // only one '='
}
Sign up to request clarification or add additional context in comments.

Comments

2

You have one syntax error in your code, and one logic error.

if (empty($a) { is missing an ending )

And you're using == to do assignments, but it's used for comparisons. You want to use just = in this case.

Revised code:

$a = $_POST['a'];
if(empty($a)) {
    $errors['a'][] = "text";
    $a = "new content";
}

3 Comments

That won't assign the value of $a to the $_POST variable, which I think the OP is asking for?
@BenM He's assigning it to $a and checking on $a, so I'm under the assumption he's never using $_POST['a'] again.
@BenM: I think it's just echo $a; and not reassigning the $_POST variable
-1

You need to re-assign it in the array if you want to access it by that in the future.

$_POST['a'] = $a;

5 Comments

Don't do that, validate the input ant put it into new variables. In your code you should not work directly with input data.
For what reason may I ask? It would seem best to re-use the memory instead of creating more variables.
For many many many reasons. You are right, re-using the memory is a good thing. But never trust input. So, don't reuse superglobals. Always put data in new variables. All input is evil. By doing so, you will not forget to validate the input.
One could run a validation foreach on the superglobal, discarding unknowns and validating others. How would you feel about that?
Better, from security point of view. However, decouple input validation, super-globals and use of input in code is a good idea.

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.