Php operators

Hi hope you can help me :

I want to say if the variable from is not empty AND the variable answer is equal to ‘4’ or ‘four’
not sure the below is correct :

	if(($from != "" && ($answer == "4" || "four")))


please help !
thanks


if( false === empty($var) && (4 === $var || 'four' === $var) ){
    
}


# will catch '4' too, not a strict int 4.
if( false === empty($var) && (4 == $var || 'four' === $var) ){
    
}


if( ! empty($var) && in_array($var, array(4, 'four')) ){
    
}

if ($answer != (‘4’ || ‘four’) )
{
$display_blockmsg = “You have entered the security question incorrectly. Your request will NOT be processed”;
exit();
}

Anthony thanks , but u’ve lost me…i’m a novice is the above any good ?

basically if $answer is not equal to 4 or four I want the program to stop
thnaks

No, not really. You cannot compare a single value to many like that, take a look at this; hopefully you’ll see what’s happening.


if( ! in_array($var, array(4, 'four')) ){
    
}

:slight_smile:

while ( ! in_array($answer, array(4, ‘four’)) )
{

?

is array a reserverd word used here ?
thnks

Why do you think it is? Why ‘reserved’ ?

glad ur still with me ! I’ve worked it out in_array is a PHP function and the code is checking …
I just have to work out where to bung this code - if I post it all could you kindly help me ?

if( ! in_array($answer, array(4, ‘four’))) // this is the first condition if it isn’t met the program terminates
{
//if answer is not in an array containing either 4 or four then…
echo “You have entered the security question incorrectly. Your request will NOT be processed”;
exit();

if ($from != "")     // then I want this IF statement to run 
{

		//test send mail to me
		mail($to, $subject, $contents, $from_header);
		
	    // redirect back to url visitor came from

	    $display_blockmsg = "Thank you, <b>$_POST[title] $_POST[firstname] $_POST[lastname]</b>, <br>
							Your contact form details have been sent to us <br>
							Your contact details are:<br><br>
							<b>Address:</b> $_POST[address1] 
							$_POST[address2],<br>
							<b>City:</b> $_POST[city]<br>
							<b>County:</b> $_POST[county]<br>
							<b>Postcode:</b> $_POST[postcode]<br>
							<b>Country:</b> $_POST[country]<br>
							<b>Telephone:</b> $_POST[telephone]<br>
							<b>email:</b> $_POST[from]<br><br>";		
}								

else
{
$display_blockmsg = “<center><b>The email field</b> is empty. Please go back and complete this field.</center>”;
}

} //end of first if

I have got teh syntax of tHE if statements mixed up
apologies foru funny faces - dont know where they came from !

thanks for ur help

Just to add, you’re not restricted to && or ||, you can also use the words ‘and’ and ‘or’ in your PHP.

No-one encourages this in PHP circles however its been present in PHP4 and continues to be in v5.

Please, do not make a habit out of using ‘and’ and ‘or’, because it will inevitably end in problems (try $g = false or true; sometime).

getting there…

what is wrong with my brackets and IF STATEMENT ?
STILL NOT WORKING

Thanks

	
if( $answer != 4 && $answer != 'four')
	{	
	   //if answer is not either 4 or four then..
		echo "You have entered the security question incorrectly. Your request will NOT be processed";
		exit(); 
        }
    
   else if ($from != "")	// ANOTHER condition is true 
	{       	//test send mail to Me 
			mail($to, $subject, $contents, $from_header);
			
		    // redirect back to url visitor came from

		    $display_blockmsg = "Thank you, <b>$_POST[title] $_POST[firstname] $_POST[lastname]</b>, <br>
								Your contact form details have been sent to us <br>
								Your contact details are:<br><br>
								<b>Address:</b> $_POST[address1] 
    							$_POST[address2],<br>
    							<b>City:</b> $_POST[city]<br>
    							<b>County:</b> $_POST[county]<br>
   								<b>Postcode:</b> $_POST[postcode]<br>
    							<b>Country:</b> $_POST[country]<br>
    							<b>Telephone:</b> $_POST[telephone]<br>
								<b>email:</b> $_POST[from]<br><br>";		
									
	   }
		
  else    	
  {		
	$display_blockmsg = "<center><b>The email field</b> is empty. Please go back and complete this field.</center>"; 
   }

[ot]Please put code tags around the code you post, it makes it much more readable (and gets rid of the smilies).
If you use the advanced editor (click on Go Advanced) you will find a drop down box with code tags for several languages in the editor menu bar.[/ot]

HI Guido can you tell me how pls ?
i just had to edit my code

Still not working is so vague. IF you’re still going to completely ignore the code that Mr Sterling has given you then perhaps you should try this:

	
if(($answer != 4) && ($answer != 'four')) //Note each check has its own set of brackets

Why you couldn’t jus use Mr Sterlings code is beyond me.

Where is $answer set?

Thank you
$answer is a string variable and so i can’t use an array. ?

in_array() checks if a value (in your case a string) is present in an array.
So Anthony’s solutions all work.

And could you explain what ‘not working’ means? What is happening when you run your script? And what should be happening?

And please do an echo of $answer to see what value it contains.

thanks guido - i have to get back about this much later - please stay with me.
M

Hi again Guido
the whole problem is that there are TWO if statements at work here…
an outside one and a nested inner one.

IF the $answer is 4 or four then I want the INNER IF statements to go on and execute…
IF the $answer is NOT 4 or four then the program should give a message and exit.

This php code is working on inputs from an html FORM and the variable $answer is just a verification answer (against SPAM)

I’M SURE it is my brackets and IF statements which are at fault …have tried them every which way…

At the moment I’m just getting a blank screen and no processing …

Thanks for your help

M


if( ! in_array($answer, array(4, 'four')))
{
       //if answer is not in an array containing either 4 or four then..
        echo "You have entered the security question incorrectly. Your request will NOT be processed";
        exit();     }  //?

else
{
    if ($from != "")
    {
            //test send mail to Melanie
            mail($to, $subject, $contents, $from_header);

            // redirect back to url visitor came from
            $display_blockmsg = "Thank you, <b>$_POST[title] $_POST[firstname] $_POST[lastname]</b>, <br>
                                Your contact form details have been sent to us <br>
                                Your contact details are:<br><br>
                                <b>Address:</b> $_POST[address1]
                                $_POST[address2],<br>
                                <b>City:</b> $_POST[city]<br>
                                <b>County:</b> $_POST[county]<br>
                                <b>Postcode:</b> $_POST[postcode]<br>
                                <b>Country:</b> $_POST[country]<br>
                                <b>Telephone:</b> $_POST[telephone]<br>
                                <b>email:</b> $_POST[from]<br><br>";
  }
  else
  {
      $display_blockmsg = "<center><b>The email field</b> is empty. Please go back and complete this field.</center>";
  }
} //end of first else ...if

Assuming you actually display $display_blockmsg at some point in your code (after the piece you posted here), it should always display something.
If you get an empty page, the problem might lie in the part of the code you didn’t post here.
While debugging, to see what parts of the if-else are executed, you can add some echo statements in the if and else branches, and then exit(); after the entire if-else (to prevent executing any redirects or whatever there is in the following code that is causing the blank page).