Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/264117-if-statement-with-or/
Share on other sites

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 ||

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

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).

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.