So I finally got around to time for code again thankfully, however I'm having problems with constraint classes, the error message I'm getting is "Notice: Use of undefined constant Username - assumed 'Username'

<?php

require_once 'connection/db.php';

function username($val)
{
    return (bool)preg_match("/^([a-zA-Z0-9])+$/i", $val);
}	

$unameErr = $fnameErr = $snameErr = $emailErr = $passwordErr = $passwordConfirmErr = $phoneErr = "";
$uname = $fname = $sname = $email = $gender = $password = $passwordConfirm = $phone = "";

if ($_SERVER["REQUEST_METHOD"] == "POST"){

	if (empty($_POST["uname"])) 
		{
			$unameErr = " Username is required";
		} 

	else if ($_POST["uname"] < Username)
		{
			$unameErr = " Username is required";
		} 
}

?>


<form action="" method="POST">
	<input type="text" name="uname" placeholder="Username"><span class="error"><?php echo $unameErr; ?></span><br>

<input type="submit" value="Submit">
</form>



 class Username {
	 static $my_var = 10;
}


    The error output should provide you with the line where it occurred. That is definitely useful when troubleshooting problems.

    Anyway, right here...

    else if ($_POST["uname"] < Username)

    ...you are referencing "Username", but it's not in quotes and doesn't have a $ in front of it, indicating it's a variable. Since it's not in quotes and doesn't have a dollar sign, its syntax matches that of a constant. If you had a constant defined as Username, it would be evaluated in this operation. But there isn't a definition for one, so PHP has an issue with it. What it tries to do (as mentioned in the error message) is assume you meant 'Username' (note the quotes), meaning it thinks you meant the string 'Username'.

    What you probably meant was $Username or $uname, since I don't see a $Username variable declared in your code snippet.

      If it is indeed a constant then where are you defining it? At the top of the file? In another one? You may not be including it which would explain the error you are getting.

        As bonesnap explained, the syntax in this line is not strictly illegal in PHP but it presents a problem. It's valid syntax IF you have defined some constant Username. Take a look at [man]define[/man] entry in the manual.

        Bonesnap said it pretty clearly:

        Bonesnap wrote:

        you are referencing "Username", but it's not in quotes and doesn't have a $ in front of it, indicating it's a variable. Since it's not in quotes and doesn't have a dollar sign, its syntax matches that of a constant. If you had a constant defined as Username, it would be evaluated in this operation. But there isn't a definition for one, so PHP has an issue with it. What it tries to do (as mentioned in the error message) is assume you meant 'Username' (note the quotes), meaning it thinks you meant the string 'Username'.

          9 days later
           class Constraints {
          	public function testUsername()
          	 {
          	  $username = $_POST['Username'];	 
          		 switch(true)
          {
          
          case (empty($username)):
          						echo $error = "You muse enter a username";
          						$valid = true;
          						break;	
          
          case (strlen($username) > 10):
          						echo $error = "Username is too long";
          						$valid = true;
          						break;
          
           case (strlen($username) < 3):
          						echo $error = "Username is too short";
          						$valid = true;
          						break;
          
          case (!preg_match('/^[A-Z0-9]+$/i', $username)):
          						echo $error = "Please an alphanumeric username";
          						$valid = true;
          						break;
          
          
          default:
          $valid = false;
          break;
          }
          	}
          
          }

          This works but I'm not entirely happy with it

            cluelessPHP wrote:

            This works but I'm not entirely happy with it

            Neither am I:

            • A class named Constraints does not seem necessary. Perhaps you should use a namespace instead as testUsername only needs to be a non-member function.

            • Having the magic numbers 10 and 3 embedded in the logic is a Bad Thing. Also, hardcoding 'Username' might make your code unnecessarily inflexible.

            • I would not use a switch for this: if/elseif/else seems more appropriate.

            • You do not seem to actually use $valid, and you set $valid to true when the input is invalid, which sounds wrong.

            • Your only use for $error is to immediately print it. Printing such an error message might not be desired, so this is inflexible.

            Perhaps you could consider something like this:

            class InvalidInputException extends Exception {}
            
            function testUsername($min_length = 3, $max_length = 10, $field = 'Username')
            {
                if (empty($_POST[$field]))
                {
                    throw new InvalidInputException("You muse enter a username");
                }
            
            $username = $_POST[$field];
            
            if (strlen($username) > $max_length)
            {
                throw new InvalidInputException("Username is too long");
            }
            elseif (strlen($username) < $min_length)
            {
                throw new InvalidInputException("Username is too short");
            }
            elseif (!preg_match('/^[A-Z0-9]+$/i', $username))
            {
                throw new InvalidInputException("Please an alphanumeric username");
            }
            }

            Of course, if you prefer, you could return an error message string instead of throwing an exception.

              Thanks laserlight, I'll play around a little and see what I come up with, I'd never heard of extends before so it's some interesting reading

                
                 class Constraints {
                	public function testUsername()
                	 {
                		$fields = array('username');
                
                	$error = false; 
                	foreach($fields AS $fieldname) { 
                	if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
                		echo $fieldname .' is required <br>'; 
                		$error = true; 
                	}
                }
                
                if(!$error) { 
                }
                
                }
                 }

                This way didn't seem too bad.

                  Write a Reply...