Help with a if else condition

How can I achieve the effect below?

I want to check if $var1, $var2, $var3

then display {

html, body, div;

}

else {

no results in your match please try again.

}

I have tried this with a parse error resulting



if(!empty($var1, $var2, $var3){

<div>good results</div>

}

else {

no results in your match please try again.

}


it will trow this parse error

Parse error: parse error, expecting `‘)’’ in

  • empty() takes only one argument
  • if() is missing its closing parentheses
  • <div> is not PHP code, you need to leave PHP mode or echo that string
  • no results… is not PHP code, you need to leave PHP mode or echo that string
if (!empty($var1) && !empty($var2) && !empty($var3)) {
  echo "<div>Good Results</div>";
} else {
  echo "Nor esults in your match please try again.";
}

First, Are you trying to see if var1, var2, and var3 are all populated or just one of them?

Second you will have to do something like this (I am assuming that you want all three variables to be set)




if(!empty($var1) && !empty($var2) && !empty($var3)){
echo ('<div>good results</div>');
}

else {
echo ('no results in your match please try again.');
} 


Looks like they were trying to do the equivalent of this:


$var1 = "" ;
$var2 = "" ;
$var3 = "" ;

$vars = $var1.$var2.$var3 ;

if( empty ( $vars ) ){
echo "not even one was filled in!" ;
}

Thank you very much all of them worked…

But isset can take multiple arguments:

if(isset($var1, $var2, $var3)){
//
}

but isset only checks for a small subset of what empty checks for.

Sometimes it’s all you need, and other times it’s not enough.

i have just realize that I was trying to do was form validation. I also realized that the conditions above won’t validate the way I wanted.

is there an standard validation way? Or it adapts to what the scriptwriter wishes

For checking the emptiness, using either isset() or empty() would be fine. If you want do more strict and/or complex validations, match functions would be good in my view.

for instance what would be the matching possibilities of validations in the form below?

How would go to validate it?

<div id="tresuno"> 
<p>Contac us we want to assist you better.....</p>
<div id="tresdos">
<form action="step2.php" method="post">
Name<br/><input name="username" type="text" id="username" />
<br/> 
Email<br/><input name="email" type="text" id="email"/>
<br/>
Message<br/><textarea  name="message"  cols="60" rows="8" id="message"></textarea>
<br/>
<input type="submit" value="Send your message" /><br/>
</form>
</div>
</div>

A text input in a form that’s been submitted is set but not empty, so using isset() is not a check for emptiness.

What about if a user input a not matching record… how is that call NULL?

For instance user input then it is submitted and set then the value can be NULL or empty at this point. The form is going to look in a database a record that matches the input of the user with the one in the database. But as I said “coloso” string was input but is not in the database, How the value is call at that point?

I am talking about a form that will retrieve and display data back from the database out of the user input.

The form i have posted above is for sending a e-amil with the name, email and message of the users.

Right now I am trying to validate two type of forms. One that retrieve and display info form that will submit user information and try to match with some database info. And another form that is for sending e-amil, name and a message to my mail box

preg_match is the better function to play with in such case of validation.

you mean the first of the second case?

filter_var is perhaps one of the best ways to sanitise and validate user input based on various types of [url=“http://www.php.net/manual/en/filter.filters.php”]filters that you may require.

Hi Dan, thanks for clarifying this. the text input is set even the value is empty. but it works for checkbox and radio inputs. and sometimes isset() even works better then empty(), for example, ‘0’ is a valid option of a radio button.

Validation is up to you. Perhaps you ask for a persons height, then perhaps you should check that someone didn’t enter 200ft tall, or 57 feet when they meant 5’7, or five instead of 5.

There is no simple way to do this. Even emails, so the email is valid, but it’s @example.com, or some other fictional address. But obviously there are some simple helpers, like checking for int, filter_var etc.

Here is the basic start (just raw code):

<?php
class Validator{
	private $_errors;
	
	function __construct(){
		$this->_errors = array();
	}
	
	function checkNumber($value, $message){
		if(!preg_match('/[0-9]+/', $value)){
			$this->_errors[] = $message;
		}else{
			return true;
		}
	}
	
	//and so on
	
	function hasErrors(){
		if(!empty($this->_errors)){
			return true;
		}else{
			return false;
		}
	}
	
	function getErrors(){
		return $this->_errors;
	}
}

//Usage:

$V = new Validator();
$V->checkNumber($_POST['some-field'], 'Please enter valid number.');
// so on ...
if($V->hasErrors()){
//show the error message here
print_r($V->getErrors());
}else{
//proceed ahead
}
?>

if I enter 180009 as age, then there is no problem?

You would need further conditions that check whether the age is an integer, is not less than zero, and (for a person) is less than 150 years old.