sandstorm140 Posted June 13, 2012 Share Posted June 13, 2012 Greetings! I'm using the raintpl template engine. I'm parsing a file and trying ignore all empty lines and comment lines. The if statement in the index file should be pretty understandable. The issue i'm having is with the logical operator || OR. If I have both of them inside the if statement (as in the code below), it returns true. However if i remove either one of them, it works fine. How can I go about fixing this using the || OR method? index file: $explodeByReturn = explode("\n", $file); foreach($explodeByReturn as $l) { if(trim($l) != '' || substr(trim($l),0,2) != '//') { //if((trim($l) != '') || (substr(trim($l),0,2) != '//')) { //ALSO TRIED THIS WITH SAME RESULT $explodeByLine = explode('=', $l); $tpl->assign(rtrim($explodeByLine[0]), ltrim($explodeByLine[1])); } } lang_file PAGE_TITLE = Test page OK = OK SUBMIT = SUBMIT USERNAME = Username: PASSWORD = Password: MIN_CHARS_LOGIN = Minimum 6 Characters LOGIN_SUBMIT = Login //SOME COMMENT MORE_VARS = test var Quote Link to comment https://forums.phpfreaks.com/topic/264117-if-statement-with-or/ Share on other sites More sharing options...
insidus Posted June 13, 2012 Share Posted June 13, 2012 trim($l) != ' ' Shouldn't that be trim($l) != '' ' ' is a space, and '' is the same as empty() And shouldn't you be using AND? If LINE Isnt Empty AND Isnt a Comment... do this not If LINE Isnt Empty.. OR If Line Isnt a comment Use && not || Quote Link to comment https://forums.phpfreaks.com/topic/264117-if-statement-with-or/#findComment-1353505 Share on other sites More sharing options...
sandstorm140 Posted June 13, 2012 Author Share Posted June 13, 2012 yes i accidentally put a space in between the single quotes. && works thank you. and/or always confuses me. I think of 'and' as IF both/all values returning true, 'or' as IF either one of the values returning true. Quote Link to comment https://forums.phpfreaks.com/topic/264117-if-statement-with-or/#findComment-1353508 Share on other sites More sharing options...
insidus Posted June 13, 2012 Share Posted June 13, 2012 Don't forget that with OR.. if the first value is true, then it won't bother processing the rest of the conditions.. cause one is true, so the OR passes.. With AND, if the first value is false, it won't process the rest of the condition Doing if(isset($_GET['id']) && $_GET['id'] != "") won't throw and error if then ET value hasn't been sent.. usally, the last condition would throw and error cause $_GET['id'] isn't set, so it cant check it, but the because the isset() failed, it didn't check the value of the GET Quote Link to comment https://forums.phpfreaks.com/topic/264117-if-statement-with-or/#findComment-1353510 Share on other sites More sharing options...
sandstorm140 Posted June 13, 2012 Author Share Posted June 13, 2012 That just confuses me more. That's exactly how i would expect OR to work. -Check if first condition is true. If not, check if next condition is true. with AND -Check if first condition is true. If it is, check next condition for true. I don't understand in my case, why AND works opposed to OR. I only need one condition to be true. It's not possible to have a comment line on an empty line (since it's empty). Quote Link to comment https://forums.phpfreaks.com/topic/264117-if-statement-with-or/#findComment-1353515 Share on other sites More sharing options...
insidus Posted June 13, 2012 Share Posted June 13, 2012 check to see if the line is not Empty. If its not empty check to see if its not a comment (AND) has to be both not empty and not a comment check to see if the line is not empty. regardless of if it is or not, check to see if its a comment (OR) doesn't matter if its a comment or not empty You only want to process lines that are not empty and that are not a comment, so you would use AND If you want to find all lines that WERE empty, OR WERE a comment, then use OR Quote Link to comment https://forums.phpfreaks.com/topic/264117-if-statement-with-or/#findComment-1353518 Share on other sites More sharing options...
sandstorm140 Posted June 13, 2012 Author Share Posted June 13, 2012 Ahhhhh I believe I'm starting to understand now. Thank you for explaining it Quote Link to comment https://forums.phpfreaks.com/topic/264117-if-statement-with-or/#findComment-1353521 Share on other sites More sharing options...
kicken Posted June 13, 2012 Share Posted June 13, 2012 My post in another thread about the and/or logic might help you visualize it a bit better if you still need a little help understanding it. You can read it here: http://forums.devshed.com/showpost.php?p=1398306&postcount=3 Quote Link to comment https://forums.phpfreaks.com/topic/264117-if-statement-with-or/#findComment-1353536 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.