This question already has answers here:
How to have multiple conditions on the same if statement? [duplicate]
(5 answers)
Closed 8 years ago.
hello I just wanted to check and see if this would be correct PHP syntax:
if ($input == "DeOnTRAY96#localhost"){
echo"
Projects: 1"?>
<br>
<?php
echo"
Admin: yes
";
}
elseif ($input == NULL){
die("Please enter password.");
}else{
header("Location:Invalidpassword.php");
exit;
}
Right where is says
if($input == "DeOnTRAY96#localhost"){
Could I put
if($input == "DeOnTRAY96#localhost" or "somethingelse"){
And still have it work?
You don't want
if($input == "DeOnTRAY96#localhost" or "somethingelse"){
You want
if($input == "DeOnTRAY96#localhost" or $input == "somethingelse"){
I might suggest using === instead of == in this case, as you'd like a type sensitive comparison.
Additionally, for $input == NULL you should use is_null($input). Null is weird in most programming languages, so language specific functions for testing are usually the way to go (rather than comparison)
OR syntax in PHP:
if($var == 'something' || $var == 'something else')
{
//do something
}
For reference:
|| means OR
&& means AND
For a more future-proof solution, consider in_array. I use it for as few as two options, if there's even the slightest chance there may be more added.
if( in_array($input, ["DeOnTRAY96#localhost", "somethingelse"]))
Once you get to four or more options, it's probably better to do something more like:
$whitelist = [
"DeOnTRAY96#localhost"
, "somethingelse"
, "anotheroption"
, "too many options to do inline!"
];
if( in_array($input, $whitelist))
Related
This question already has an answer here:
How to get OR(||) and AND (&&) statements to work together? PHP
(1 answer)
Closed 3 years ago.
I have an "if" statement I would like to write with multiple comparison operators of the "&&" and "||" type. I am not entirely sure if it will perform the way I am thinking it should. Here is an example of what I am trying to do.
if($alpha == "FF" && $bravo == "3sh" || $charlie == "6sh")
{
printf($alpha);
}
What I expect is that $alpha MUST equal "FF" in order for this to execute.
What I also expect is at least one of the other two conditions must be met in order to execute.
What I am concerned about is the code ignoring the first two conditions and executing the code because the last condition is met.
You can create wrap conditions accordingly as below:
if($alpha == "FF" && ($bravo == "3sh" || $charlie == "6sh"))
{
printf($alpha);
}
So here, $alpha == "FF" should have to be true. And either one condition should be true in between on braces. Hope it helps you.
It may be helpful to look into Boolean logic a litte more, but for your particular situation what you are trying to do is:
if($alpha == "FF" && ($bravo == "3sh" || $charlie == "6sh"))
{
printf($alpha);
}
Essentially, you say
$alpha == "FF" must be true AND
($bravo == "3sh" || $charlie == "6sh") must be true
- i.e., either $bravo == "3sh" OR $charlie == "6sh"
This question already has answers here:
PHP if not equal(!=) and or (||) issue. Why doesnt this work?
(6 answers)
Closed 4 years ago.
Hope u people will be fine.
Iām working on a following code in which i want to use multiple php if conditions with multiple not operators (example is below), but when i execute following php code, it always returns true (mean content in always parenthesis always executed) even no condition is true.
I want to ask what is the problem in following code. Is there any specific syntax or rule for using multiple != operator in php conditions. And i am amazed to see that if i use following code by replacing != operator with == operator it is working fine.
if( $ext!="exe" || $ext!="html" || $ext!="htm" || $ext!="js" || $ext!="iso" || $ext!="zip" || $ext!="rar" )
{ // ececk extension
echo $ext."extension";
}
else{
echo "not match";
}
Waiting for your kind replies. and sorry for my bad english.
Better code:
$allowed = array('jpeg','png');
if(in_array($ext,$allowed)){
echo "Correct";
}
else {
echo "Wrong";
}
User XOR operator instead of OR as or checked all condition need to be identical while XOR return true if anyone from all is identical:
if(!($ext!=="exe") xor ($ext=="html")){ //Add more conditions
//each condition in new brackets & ! sign in start before all conditions
{ // ececk extension
echo $ext."extension";
}
else{
echo "not match";
}
Hope it will help
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does ā===ā mean?
I am confused with the use of those operators in php, I am not quite sure when should I use === and when ==.
for example why/when should I write:
if( $some_method_that_returns_something_or_false() === FALSE) {
//do stuff
}
and when with ==?
Also, does === means I must return bool FALSE or I can return 0? When it is considered an bad practice to use === or ==?
Also when putting something like this:
if($some_method_that_returns_true_or_false()) {
}
is that $some_method_that_returns_true_or_false() == TRUE or
some_method_that_returns_true_or_false() === TRUE?
=== means exact value, so for true it has to be true, while == checks for the meaning of the value, so true will be also a value of '1' or a whatever String.
== is used for checking equallity and === is used for checking the equality as well as type.
And
if($some_method_that_returns_true_or_false()) {
}
is checking for $some_method_that_returns_true_or_false() == TRUE
This question already has answers here:
PHP IF OR Else statement - best way to do this
(3 answers)
Closed 8 years ago.
Can't believe I'm resorting to going on Stack Overflow to ask this rather than Google it, but is there a way to have multiple values in one if statement? For example, rather than using:
if ($var == 'value' || $var == 'othervalue') :
return true;
endif;
...you could do something along the lines of:
if ($var == ('value' || 'othervalue')) :
return true;
endif;
I know it's probably a very basic yes/no answer, but it's just something I've wanted to know but never really had the time to look up.
Something like this:
if (in_array('needle', array('value1', 'value2', 'needle'))){
}
There is no short syntax, but you can always simulate it if you want to. I.e., something like:
if (in_array($var, array('value', 'othervalue')) :
return true;
endif;
You could use in_array() function for this:
$arr = array('value', 'othervalue');
if (in_array($var, $arr))
return true;
You can use an array and then use in_array() to check if $var is present. Something on the lines -
$my_array = array('value', 'othervalue');
if ( in_array($var, $my_array) )
return true;
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
php multiple if conditions
What is better ? Multiple if statements, or one if with multiple conditions
So I was working on a segment of code in which I ended up using two different styles of if statements. That got me wondering - which one is more efficient in PHP? Are there times when one might be better than the other, despite general differences (if present)? Is there a certain level of complexity where the benefits become clear (if close) or close (if originally clear)? Also, are there other benefits of one method over the other, other than aesthetic or subjective differences?
if ($value == 'someval') {
if($otherval == 'someval') {
// do stuff
} else if ($otherval == 'otherval') {
// do same stuff
}
}
vs
if (($value == 'someval') && ($otherval == 'someval' || $thirdval == 'someval') {
// do stuff
}
Note - I don't care what works for C# or Java or whatever other language, unless someone can show that they handle the construct exactly the same.
So long as you're executing the same code in each block (as you've indicated by your comments) then they'll both do the trick (and no, there's really not much of a performance difference).
However, typically it's not the case that you'd execute the same code both ways, so logically the first block is actually different from the second in that case. Here's an example:
if($value == 'someval') {
if($otherval == 'someval') {
doSomething();
}
else if ($otherval == 'otherval') {
doSomethingElse();
}
}
versus:
if(($value == 'someval') && ($otherval == 'someval' || $otherval == 'otherval')) {
//we now still need to evaluate $otherval in order to determine which func to execute...
//this adds bloat...
if($otherval == 'someval') {
doSomething();
}
else if ($otherval == 'otherval') {
doSomethingElse();
}
}
So as you can see in the case above the second block is actually less efficient than the first.
There is no real difference, you should use whichever is more readable.
Nested or not nested if-blocks?