Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Style and elegance of IF 3

Status
Not open for further replies.

sen5241b

IS-IT--Management
Sep 27, 2007
199
US
I have a recurring problem that is not a bug. I often have code where I don't care at all if the IF statement is true, I only care if the result is false. But to execute code for false, you must follow a true action with ELSE and then a false action. How can I just do a the false action? Isn't there a more elegant way to do this than below?

Example:

Code:
$NumOfWords = count($AWordAroundMatchedOW);
for ($x=0; $x <= $NumOfWords; $x++) 
{
    if (pspell_check($pspell_link, $AWordAroundMatchedOW)) 
	{   echo 'DO NOTHING'; }
	else
	{   $Indicator = 1; }
}
 
You would simply check for a negative value instead of the positive one.
Code:
    if ([!]![/!]pspell_check($pspell_link, $AWordAroundMatchedOW))
    {   $Indicator = 1; }

___________________________________________________________
[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
Thanks. Can you put the ! before the brace?
 
No, the ! has to go inside the parentheses. It's part of the expression the IF statement is evaluating. Putting it outside will get you a syntax error.
 
But if you have a compound IF statement, you will have to put the ! before each evaluation like so:

Code:
IF ((!$a == 2) AND (!$b == 4))
   { echo 'what I want to do'; }

I guess there is no way to put ! in front of the whole IF statement, true?
 
if ( !($a == 2 || $b == 4) )

or

if ( $a != 2 && $b != 4 )

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top