Execute code inside IF statement in all cases but one
What would be the right way to do this? For example:
if ($var1 == 'value1' && $var2 == 'value2') {
//code2
return;
} else {
//code1
}
//code2
I was thinking of using keyword continue
insided if
but I would also like to know if there is a better way?
EDIT
To be more precise I am going to try explaining it better. $var1
is current logged in user's role and $var2
is role of user whose details (let's say email) are being inserted into table. code2
is part that always need to be executed and there are a lot of lines there and therefore I shouldn't duplicate it. code1
is part where I am informing user that he is supposed to verify his email address and I am inserting into table the same thing (that email is still not verified). There is only one case when this shouldn't happen and when email should be automatically verified upon adding and that case is when $var1
is ADMIN and $var2
is REGULAR USER. In all other cases, code1
should be executed.
php if-statement
add a comment |
What would be the right way to do this? For example:
if ($var1 == 'value1' && $var2 == 'value2') {
//code2
return;
} else {
//code1
}
//code2
I was thinking of using keyword continue
insided if
but I would also like to know if there is a better way?
EDIT
To be more precise I am going to try explaining it better. $var1
is current logged in user's role and $var2
is role of user whose details (let's say email) are being inserted into table. code2
is part that always need to be executed and there are a lot of lines there and therefore I shouldn't duplicate it. code1
is part where I am informing user that he is supposed to verify his email address and I am inserting into table the same thing (that email is still not verified). There is only one case when this shouldn't happen and when email should be automatically verified upon adding and that case is when $var1
is ADMIN and $var2
is REGULAR USER. In all other cases, code1
should be executed.
php if-statement
4
Don't understand your question please more elobrate
– user10186369
Nov 23 '18 at 10:29
Exactly. It seems to me you do not need the else statement at all, because the code 2 should be executed all the time
– Pavel Janicek
Nov 23 '18 at 10:30
add a comment |
What would be the right way to do this? For example:
if ($var1 == 'value1' && $var2 == 'value2') {
//code2
return;
} else {
//code1
}
//code2
I was thinking of using keyword continue
insided if
but I would also like to know if there is a better way?
EDIT
To be more precise I am going to try explaining it better. $var1
is current logged in user's role and $var2
is role of user whose details (let's say email) are being inserted into table. code2
is part that always need to be executed and there are a lot of lines there and therefore I shouldn't duplicate it. code1
is part where I am informing user that he is supposed to verify his email address and I am inserting into table the same thing (that email is still not verified). There is only one case when this shouldn't happen and when email should be automatically verified upon adding and that case is when $var1
is ADMIN and $var2
is REGULAR USER. In all other cases, code1
should be executed.
php if-statement
What would be the right way to do this? For example:
if ($var1 == 'value1' && $var2 == 'value2') {
//code2
return;
} else {
//code1
}
//code2
I was thinking of using keyword continue
insided if
but I would also like to know if there is a better way?
EDIT
To be more precise I am going to try explaining it better. $var1
is current logged in user's role and $var2
is role of user whose details (let's say email) are being inserted into table. code2
is part that always need to be executed and there are a lot of lines there and therefore I shouldn't duplicate it. code1
is part where I am informing user that he is supposed to verify his email address and I am inserting into table the same thing (that email is still not verified). There is only one case when this shouldn't happen and when email should be automatically verified upon adding and that case is when $var1
is ADMIN and $var2
is REGULAR USER. In all other cases, code1
should be executed.
php if-statement
php if-statement
edited Nov 23 '18 at 10:38
nikname
asked Nov 23 '18 at 10:27
niknamenikname
7718
7718
4
Don't understand your question please more elobrate
– user10186369
Nov 23 '18 at 10:29
Exactly. It seems to me you do not need the else statement at all, because the code 2 should be executed all the time
– Pavel Janicek
Nov 23 '18 at 10:30
add a comment |
4
Don't understand your question please more elobrate
– user10186369
Nov 23 '18 at 10:29
Exactly. It seems to me you do not need the else statement at all, because the code 2 should be executed all the time
– Pavel Janicek
Nov 23 '18 at 10:30
4
4
Don't understand your question please more elobrate
– user10186369
Nov 23 '18 at 10:29
Don't understand your question please more elobrate
– user10186369
Nov 23 '18 at 10:29
Exactly. It seems to me you do not need the else statement at all, because the code 2 should be executed all the time
– Pavel Janicek
Nov 23 '18 at 10:30
Exactly. It seems to me you do not need the else statement at all, because the code 2 should be executed all the time
– Pavel Janicek
Nov 23 '18 at 10:30
add a comment |
1 Answer
1
active
oldest
votes
From your question
if ($var1 == 'value1' && $var2 == 'value2') {
//code2
return;
} else {
//code1
}
//code2
That sounds to me that code2
should be executed no matter what. In that case, you have to switch the if
statement:
if( $var1 != 'value1' || $var2 != 'value2'){
code1();
}
code2();
The code2
will be always execute and in the case you need, the code1
will execute too
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444925%2fexecute-code-inside-if-statement-in-all-cases-but-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
From your question
if ($var1 == 'value1' && $var2 == 'value2') {
//code2
return;
} else {
//code1
}
//code2
That sounds to me that code2
should be executed no matter what. In that case, you have to switch the if
statement:
if( $var1 != 'value1' || $var2 != 'value2'){
code1();
}
code2();
The code2
will be always execute and in the case you need, the code1
will execute too
add a comment |
From your question
if ($var1 == 'value1' && $var2 == 'value2') {
//code2
return;
} else {
//code1
}
//code2
That sounds to me that code2
should be executed no matter what. In that case, you have to switch the if
statement:
if( $var1 != 'value1' || $var2 != 'value2'){
code1();
}
code2();
The code2
will be always execute and in the case you need, the code1
will execute too
add a comment |
From your question
if ($var1 == 'value1' && $var2 == 'value2') {
//code2
return;
} else {
//code1
}
//code2
That sounds to me that code2
should be executed no matter what. In that case, you have to switch the if
statement:
if( $var1 != 'value1' || $var2 != 'value2'){
code1();
}
code2();
The code2
will be always execute and in the case you need, the code1
will execute too
From your question
if ($var1 == 'value1' && $var2 == 'value2') {
//code2
return;
} else {
//code1
}
//code2
That sounds to me that code2
should be executed no matter what. In that case, you have to switch the if
statement:
if( $var1 != 'value1' || $var2 != 'value2'){
code1();
}
code2();
The code2
will be always execute and in the case you need, the code1
will execute too
answered Nov 23 '18 at 10:34
Pavel JanicekPavel Janicek
8,809114268
8,809114268
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444925%2fexecute-code-inside-if-statement-in-all-cases-but-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Don't understand your question please more elobrate
– user10186369
Nov 23 '18 at 10:29
Exactly. It seems to me you do not need the else statement at all, because the code 2 should be executed all the time
– Pavel Janicek
Nov 23 '18 at 10:30