Exclamation Point - Question!


What does the exclamation point mean in front of the equals sign?



is/does NOT equal
if $nameerror (is) NOT(!) equal(=) to empty string, condition (?)

1 Like


I feel like there is a coding error in this example… or is there not?


^^^ - Is that valid?


I figured out my problem.

But I am curious to know if that is an error? If not, what does the equal sign mean infront of the $nameError and why does it not start with <?php but rather just <?.


I should also mention that it may be better to test not alone instead of forcing an evaluation to a string because a 0 is not a string. Examine your code and see if it is better to test as follows:

if(!$nameError) { }

then a zero will not penetrate the equation.


I can’t remember why shorthand code is implemented. I remember that % signs allow asp style php tags but either way this is not acceptable code today. I believe that the PHP manual instructs us to avoid this method. Thus, stick with <?php ... ?>


How do I set nameError to have ! ?




$nameError should be a variable to a string (and preferably a string which contains a name, since that it what the variable indicates as a data type.) Thus, a nameError might be an empty name field from a form. If the name is not entered into the form (the variable is empty, null or false), then the not logic will catch this in your equation. a false, null or empty value sets the not.


The logic in your example is illogical. If one wants to show an error message if name is NOT null,empty or false, then the error will need to be placed in an else condition.

if(!$nameError) { //there is no error } else { //show error message {


Does the exclamation point mean the variable is empty, null, or false?


just remember to read the code in your head while you evaluate your program. Thus your code will read this way:

if(!$nameError) { //there is no error } else { //show error message {

if there is NOT(!) a false, null or empty value to the $nameError variable, THEN show this code. ELSE a $nameError exists so show this error code.


How would I add a value to $nameError?


when you process the form, you check for (validate) acceptable input or any input, then validate. If the validation is successful (someone entered something other than empty, null, zero or false) then validate what was entered before processing the data. Otherwise, if someone typed nothing, null/false/empty or a zero, then detect it and set a value to the $nameError for display.


if(!$nameField) { $nameError = "Please enter a valid name."; } else { //validate the $nameField variable }

edit: actually, a form with POST values allows you to use isset() function of php. Thus, you can test a name input value as follows:

if(!isset($_POST['name'])) { //do something with $nameError }

read it in your head: if $_POST[‘name’] is NOT set (contains a value of empty, null, zero or false) THEN do something with an error message. Otherwise the name field IS set (contains a value other than null, false, zero or empty).


How do I echo the value of $nameError?


<?php echo $nameError; ?> does not show anything.

It works after I changed “contact-name” to just “name”.


This code example you have either found or been given as a starting point is unnecessarily complicated and has you spending more time beating on a keyboard creating bespoke code for each form field, than actually working on the logic of what the code is trying to accomplish. Keep It Simple (KISS.)

Your form processing code should -

  1. Detect that a post method form has been submitted.
  2. Treat the submitted data as a set and operate on it using array functions.
  3. Trim all the data at once (you can do this with a single statement), storing the trimmed data in a common php array variable that gets used in the remainder of the code.
  4. Store validation error messages in an array. This array is also an error flag. If it is empty, there are no errors. If it is not empty, there are errors. To display the error(s) at the appropriate point in the html document, reference the elements in the array.
  5. Only use isset() for check-box and radio-buttons. Once the form has been submitted, only unchecked check-box and radio-buttons will be unset. All other form fields will be set.
  6. Use the same name for any particular field throughout the code.

I’ll post an example showing the above items.

1 Like

No, I write:

			<?php if (isset($_POST["name"])) {
			$nameError = "There is content.";
		} else {
			$nameError = "There is no content.";
		} ?>

It just shows There is content. all the time.


Here is a working example of the points I made in the post above -


$errors = []; // define an array to hold validation errors
$post = []; // define an array to hold a trimmed copy of the submitted for data

// post method form processing code
	// trim the submitted data - if any of the form fields are arrays, you will need to write a recursive call-back trim function to use instead of php's trim function.
	$post = array_map('trim',$_POST);
	// validate the inputs - I'm going to assume the form field is named 'name' per your later post
	if($post['name'] == '')
		$errors['name'] = 'Name is required.';
	// other validation steps here...
	// if there are no errors, use the submitted data
		// put any database/email code here to use the data in $post...

<form method = 'post'>
// at the point of outputting the form field and error message
echo "<input type='text' name='name' value='".($post['name'] ?? '')."'><br>";
if(!empty($errors['name'])) { echo $errors['name'] . '<br>'; }
<input type='submit'>