IrOnMaSk Posted July 21, 2011 Share Posted July 21, 2011 Hey guys, I encoutered something strange in php, or maybe it suppose to. so i created a mail function then call it using if else statement, base on that, i send out email to one person if conditon meets require criteria then email anoter person in else... but it execute the if condition (that's fine) and it also execute the else condition too, both emails got send. I thought only one condition got executed. heres the mail function function email($to,$message)//create email function { $subject = "Please Ignore"; $headers = "MIME-Version: 1.0" . "\r\n"; $headers.= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; $headers .= 'From: <[email protected]>' . "\r\n"; mail($to,$subject,"<strong>".'Expiration Status'."</strong>"."<br />"."<br />" .$message,$headers); } here's the if else sending mail condition $file = file_get_contents ('content.html', true);//get content from file for reading if (preg_match('/WSNet/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'WSNet' ."<br />"."<br />". $file); } else{ email('[email protected]',"<strong>".'Expire App: '."</strong>".'No expire apps between now and the next 60 days' ."<br />"."<br />". $file); } so the if condition met and sending email to me, shouldn't the else condition be ignored? but it send email to me too... thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/242567-why-this-code-executes-both-if-then-else-statement/ Share on other sites More sharing options...
phpSensei Posted July 21, 2011 Share Posted July 21, 2011 You ran the script once correct? If it does run both conditions then check the output of preg_match and see what that gives you. Quote Link to comment https://forums.phpfreaks.com/topic/242567-why-this-code-executes-both-if-then-else-statement/#findComment-1245783 Share on other sites More sharing options...
IrOnMaSk Posted July 21, 2011 Author Share Posted July 21, 2011 thanks for reply Sensei. Yes I only run it once, and i tested the preg_match <?php $file = file_get_contents ('content.html', true); if (preg_match('/Google Search Appliance/', $file))//search the match keyword under the app_owner { echo 'Google Search Appliance found'; } else { echo 'Google Search Appliance not found'; } ?> Google Search Appliance is in the file so it echo found. and else never executed. and when it's not found the else exectued. so that seems to work fine. don't where the bug is Quote Link to comment https://forums.phpfreaks.com/topic/242567-why-this-code-executes-both-if-then-else-statement/#findComment-1245797 Share on other sites More sharing options...
IrOnMaSk Posted July 21, 2011 Author Share Posted July 21, 2011 oh and i don't know if this info matters or not,,, but i have at least 20 if statements, like if(){} if(){} if(){} ... then the last one is else Quote Link to comment https://forums.phpfreaks.com/topic/242567-why-this-code-executes-both-if-then-else-statement/#findComment-1245801 Share on other sites More sharing options...
phpSensei Posted July 21, 2011 Share Posted July 21, 2011 oh and i don't know if this info matters or not,,, but i have at least 20 if statements, like if(){} if(){} if(){} ... then the last one is else Post the full code, i would be more than happy to examine it for you. Quote Link to comment https://forums.phpfreaks.com/topic/242567-why-this-code-executes-both-if-then-else-statement/#findComment-1245804 Share on other sites More sharing options...
IrOnMaSk Posted July 21, 2011 Author Share Posted July 21, 2011 here's the full code, <html> <head> <title>Email Application</title> </head> <body> <?php function email($to,$message)//create email function { $subject = "Please Ignore"; $headers = "MIME-Version: 1.0" . "\r\n"; $headers.= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; $headers .= 'From: <[email protected]>' . "\r\n"; mail($to,$subject,"<strong>".'Expiration Status'."</strong>"."<br />"."<br />" .$message,$headers); } $file = file_get_contents ('content.html', true);//get content from file for reading if (preg_match('/ADAM\/ MIIS\/ LDAP/',$file))//search the match keyword under the app_owner 'ADAM / MIIS / LDAP' { email('[email protected]',"<strong>".'Expire App: '."</strong>".'ADAM / MIIS / LDAP' ."<br />"."<br />". $file);//mail the owners } if (preg_match('/AMAG SMS/', $file)) { email('[email protected], [email protected]',"<strong>".'Expire App: '."</strong>".'AMAG SMS' ."<br />"."<br />". $file); } if (preg_match('/Aspect eWFM/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'Aspect eWFM' ."<br />"."<br />". $file); } if (preg_match('/Aspect RTA/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'Aspect RTA' ."<br />"."<br />". $file); } if (preg_match('/AutoSys \/ iXp Agent/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'AutoSys / iXp Agent' ."<br />"."<br />". $file); } if (preg_match('/BES Standard, BlackBerry/', $file)) { email('#[email protected]',"<strong>".'Expire App: '."</strong>".'BES Standard, BlackBerry' ."<br />"."<br />". $file); } if (preg_match('/Clarify/', $file)) { email('[email protected], [email protected], [email protected]',"<strong>".'Expire App: '."</strong>".'Clarify' ."<br />"."<br />". $file); } if (preg_match('/Clients and Profits/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'Clients and Profits' ."<br />"."<br />". $file); } if (preg_match('/Connect: Direct/', $file)) { email('[email protected], [email protected], [email protected], [email protected], [email protected]',"<strong>".'Expire App: '."</strong>".'Connect: Direct' ."<br />"."<br />". $file); } if (preg_match('/ConnectShip/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'ConnectShip' ."<br />"."<br />". $file); } if (preg_match('/CorpTax/', $file)) { email('#OWNED@dishnetwork,com',"<strong>".'Expire App: '."</strong>".'CorpTax' ."<br />"."<br />". $file); } if (preg_match('#CSG / ACSR#', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'CSG' ."<br />"."<br />". $file); } if (preg_match('/DeskAlerts/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'DeskAlerts' ."<br />"."<br />". $file); } if (preg_match('/ELM \/ iLearning/', $file)) { email('[email protected], [email protected]',"<strong>".'Expire App: '."</strong>".'ELM / iLearning' ."<br />"."<br />". $file); } if (preg_match('/F-Secure Anti-Virus/', $file)) { email('[email protected], [email protected], [email protected]',"<strong>".'Expire App: '."</strong>".'F-Secure Anti-Virus' ."<br />"."<br />". $file); } if (preg_match('/F5 \/ Big-IP/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'F5 / Big-IP' ."<br />"."<br />". $file); } if (preg_match('/FileNet/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'FileNet' ."<br />"."<br />". $file); } if (preg_match('/GEAC MPC/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'GEAC MPC' ."<br />"."<br />". $file); } if (preg_match('/Gemini Scanner/', $file)) { email('[email protected], [email protected]',"<strong>".'Expire App: '."</strong>".'Gemini Scanner' ."<br />"."<br />". $file); } if (preg_match('/Google Search Appliance/', $file))//search the match keyword under the app_owner { email('[email protected]',"<strong>".'Expire App: '."</strong>".'Google Search Appliance' ."<br />"."<br />". $file); } if (preg_match('/Hyperion \/ OBIEE/', $file)) { email('[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]',"<strong>".'Expire App: '."</strong>".'Hyperion / OBIEE' ."<br />"."<br />". $file); } if (preg_match('/Instant Service/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'Instant Service' ."<br />"."<br />". $file); } if (preg_match('/Kronos Workforce Central/', $file)) { email('[email protected], [email protected], [email protected], [email protected], [email protected]',"<strong>".'Expire App: '."</strong>".'Kronos Workforce' ."<br />"."<br />". $file); } if (preg_match('/Liquid Engines/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'Liquid Engines' ."<br />"."<br />". $file); } if (preg_match('/Merced \/ DishMetrics/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'Merced / DishNetrics' ."<br />"."<br />". $file); } if (preg_match('/PGP/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'PGP' ."<br />"."<br />". $file); } if (preg_match('/PL\/SQL Developer/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'PL / SQL Developer' ."<br />"."<br />". $file); } if (preg_match('/PTMS/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'PTMS' ."<br />"."<br />". $file); } if (preg_match('/SecureAuth/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'SecureAuth' ."<br />"."<br />". $file); } if (preg_match('/Siebel/', $file)) { email('[email protected], [email protected]',"<strong>".'Expire App: '."</strong>".'Siebel' ."<br />"."<br />". $file); } if (preg_match('/Stoneriver Tracker/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'Stoneriver Tracker' ."<br />"."<br />". $file); } if (preg_match('/TeamMate/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'teamMate' ."<br />"."<br />". $file); } if (preg_match('/TeamTrack/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'TeamTrack' ."<br />"."<br />". $file); } if (preg_match('/WCCMS/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'WCCMS' ."<br />"."<br />". $file); } if (preg_match('/WebMethods/', $file)) { email('[email protected], [email protected], [email protected], [email protected], [email protected]',"<strong>".'Expire App: '."</strong>".'WebMethods' ."<br />"."<br />". $file); } if (preg_match('/WSNet/', $file)) { email('[email protected]',"<strong>".'Expire App: '."</strong>".'WSNet' ."<br />"."<br />". $file); } else { email('[email protected]',"<strong>".'Expire App: '."</strong>".'No expire apps between now and the next 60 days' ."<br />"."<br />". 'relax'); } ?> </body> </html> the content of the $file is in the picture, [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/242567-why-this-code-executes-both-if-then-else-statement/#findComment-1245831 Share on other sites More sharing options...
phpSensei Posted July 21, 2011 Share Posted July 21, 2011 I am pretty sure that based on this code you need your if/elseif/else statements structured properly, and your script here posts the email not from the ELSE but from the other if statements. Quote Link to comment https://forums.phpfreaks.com/topic/242567-why-this-code-executes-both-if-then-else-statement/#findComment-1245832 Share on other sites More sharing options...
IrOnMaSk Posted July 21, 2011 Author Share Posted July 21, 2011 your'e absolutely right, Sensei!!! I thought the if elseif else is same as if, if, if, if else statement :'( everything is working fantastically now, thanks, Quote Link to comment https://forums.phpfreaks.com/topic/242567-why-this-code-executes-both-if-then-else-statement/#findComment-1245854 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.