This question already has an answer here:
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?
</div>
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))