if statement with multiple condition in php - php

I have if statement with multiple condition,
what is the differece between this two conditon:
1.
if($province=="AB" || "NT" || "NU" || "YT")
{
$GST=5;
}
else if($province=="BC" || "MB")
{
$GST=5;
$PST=7;
}
else if($province=="NB" || "NF" || "ON")
{
$HST=13;
}
and second is:
2.
if($province=="AB" || $province=="NT" || $province=="NU" || $province=="YT")
{
$GST=5;
}
else if($province=="BC" || $province=="MB")
{
$GST=5;
$PST=7;
}
else if($province=="NB" || $province=="NF" || $province=="ON")
{
$HST=13;
}

The difference between the two is that the first one won't work as expected, and the second is technically correct.
The code:
if($province=="AB" || "NT" || "NU" || "YT")
will always evaluate to true and execute the code in that conditional block.
The reason is because you are only checking if $province == "AB" and then you are checking if "NT" == true which will evaluate to true.
To check province against all of those values (AB, NT, NU, YT) you need to explicitly check $province against each value, not just the first, which is what you are correctly doing in the second example.

I add to drew010 answer that you can do this too (wich is simpler) :
if(in_array($province,array("AB","NT","NU","YT"))
{
$GST=5;
}
else if(in_array($province,array("BC","MB"))
{
$GST=5;
$PST=7;
}
else if(in_array($province,array("NB","NF","ON"))
{
$HST=13;
}

The first one will always evaluate to true. In the second, third, and forth OR clauses in the first example you are basically asking PHP to convert the strings to Boolean values, and non-empty strings will always evaluate as true.
Just for fun, my favorite way to handle conditionals like this is with a switch statement. It's a lot easier for me to read, but it's really a personal preference thing.
switch ( $province ) {
case 'AB' :
case 'NT' :
case 'NU' :
case 'YT' :
$GST = 5;
break;
case 'BC' :
case 'MB' :
$GST = 5;
$PST = 7;
break;
case 'NB' :
case 'NF' :
case 'ON' :
$HST = 13;
break;
}

Related

Checking Multiple $_GET variables if equals a string

if(isset($_GET['a']) || isset($_GET['b']) || isset($_GET['c'])){
if(($_GET['a'] || $_GET['b'] || $_GET['c']) == "x"){
echo "YES";
} else {
echo "NO";
}
}
in this php code, i'm trying to check if one of those requests isset and if one of them value == 'x' or not, But the 2nd part if(($_GET['a'] || $_GET['b'] || $_GET['c']) == "x") doesn't work as intended at all, I wrapped it inside () hoping it would work, In this condition, do i have to separate it as i did inthe isset() part? or is there a better method to do that?
This is likely what you are looking for
UPDATE - I just changed || to && for the last condition in case you were quick to try it out.
if( (isset($_GET['a']) && $_GET['a'] == "x") || (isset($_GET['b']) && $_GET['b'] == "x") || (isset($_GET['c']) && $_GET['c'] == "x")){
echo "YES";
} else {
echo "NO";
}
If you have to write a lot of conditionals you could use one of the following:
Using a foreach and a conditional:
$either_abc_is_x = function() {
$keys = ['a','b','c'];
foreach($keys as $key)
if(isset($_GET[$key]) && $_GET[$key] == "x")
return true;
return false;
};
echo $either_abc_is_x() ? 'YES' : 'NO';
Using a an array filter with a conditional:
$get_abc_keys_equal_to_x = array_filter(['a','b','c'], function($v) {
return isset($_GET[$v]) && $_GET[$v] == 'x';
});
echo $get_abc_keys_equal_to_x ? 'YES' : 'NO';
Array gymnastics:
$either_abc_is_x = isset($_GET) && in_array('x', array_intersect_key($_GET, array_flip(['a','b','c'])));
echo $either_abc_is_x ? 'YES' : 'NO';

If and elseif's not working

Sorry for the messy code but basically this code will only ever make $booktype = "ICT" and if I remove the "else" from elseif, it makes it equal ENGLISH.
Whats going wrong?
I've echoed POST_ Book room and that comes out fine.
if ($_POST['bookroom'] == "142" || "040" || "139"|| "104") {
$booktype = "ICT";
} elseif ($_POST['bookroom'] == "015" || "016" || "017" || "018" || "027" || "028") {
$booktype = "MATHS";
} elseif ($_POST['bookroom'] == "E03") {
$booktype = "MUSIC";
} elseif ($_POST['bookroom'] == "202" || "204" || "205" || "206" || "207") {
$booktype = "ENGLISH";
}
At the end of the day, you're not using 'or' correctly. To shorten your code, you can do your if-statement like this:
if(in_array($_POST['bookroom'], array("142","040","139","104"))) {
To properly use an "or" or "and" clause, you'd need to make the statement self-contained. That is,
if(this option is true || this option is true || this option is true)
if($a == 4 || $a == 5 || $a == 6)
You can't chain conditions like this. Change it to
$_POST['bookroom'] == "142" || $_POST['bookroom'] == "040" || [...]
Also, using a switch case with fall-throughs would be more readable here.
You can also change it to use in_array function:
From:
if ($_POST['bookroom'] == "142" || "040" || "139"|| "104"){
to
if (in_array($_POST['bookroom'], array("142","040","139","104"))){
You can use switch !
switch($_POST['bookroom']){
case "142":
...
break;
case "040":
...
break;
}

Best way to address several type of users in PHP/MySQL

I'm looking for something like this:
<?php
if($condition1 == 'a' && $condition2 == 'a' && $condition3 == 'a'){
//echoing some HEREDOCS here
}
if($condition1 == 'a' && $condition2 == 'a' && $condition3 == 'b'){
//echoing some HEREDOCS here
}
if($condition1 == 'a' && $condition2 == 'b' && $condition3 == 'a'){
//echoing some HEREDOCS here
}
if($condition1 == 'a' && $condition2 == 'b' && $condition3 == 'b'){
//echoing some HEREDOCS here
}
?>
I have some 7 conditions and dozens of combinations. I want to know is this the right way to address each unique type of user or there can be some better approach?
HEREDOCS for every single combination is totally different from others.
A switch with concatenated key may be the answer for you:
switch ($condition1.":".$condition2.":".$condition3){
case "a:a:a":
//echoing some HEREDOCS here
break;
case "a:a:b":
//echoing some HEREDOCS here
break;
case "a:b:a":
//echoing some HEREDOCS here
break;
case "a:b:b":
//echoing some HEREDOCS here
break;
}
Or alternatively you could create a message map
$messages = array(
"a:a:a"=>"Docs a:a:a",
"a:a:b"=>"Docs a:a:b",
"a:b:a"=>"Docs a:b:a",
"a:b:b"=>"Docs a:b:b",
);
echo $messages[$condition1.":".$condition2.":".$condition3];
You might be looking for the switch operator.
As far as your design is concerned, you might want to have each persons text stored in the database and pull it up with the user!
I always like to design my code to avoid this kind of situation. Try to define a linear list of situations first, like:
<?php
if($condition1 == 'a' && $condition2 == 'a' && $condition3 == 'a') $user_type = 1;
elseif($condition1 == 'a' && $condition2 == 'a' && $condition3 == 'b') $user_type = 2;
elseif($condition1 == 'a' && $condition2 == 'b' && $condition3 == 'a') $user_type = 3;
elseif($condition1 == 'a' && $condition2 == 'b' && $condition3 == 'b') $user_type = 4;
?>
Just don't forget to cover every possible combination, otherwise all ifs may fail, making $user_type undefined.
Later, when you want to print something, or take any action based on user type, just do a switch / case:
switch($user_type)
{
case 1:
//echoing some HEREDOCS here
break;
case 2:
//echoing some HEREDOCS here
break;
}
The general idea is to handle the complicated thing only once, to avoid duplicating your complex logic all around your code.

Cannot figure out how to create this if statement

This is the worlds easiest php if statement ever created, how ever ever I cannot figure out how to do it, in one. Essentially I am having a stumped moment and require the communities help .
This is my function:
protected function _traverse_options($name, $type = ''){
if(isset($this->_options[$name][$type])){
echo $this->_options[$name][$type];
}
}
The if statement I need is to check for three things:
If type is not null but type is not 'before'
if type is not null but type is not 'after'
I tried doing:
if($type != '' && $type != 'before' || $type != '' && $type != 'after'){}
How ever that doesn't work.
I know this is simple, but I cannot figure it out? should || be && ??
This'll do the job:
if( $type != '' && $type != 'before' && $type != 'after'){}
So any non-empty string that is neither before nor after.
if ('' !== $type && !in_array($type, array('before', 'after')))
{
}
If I understood it correctly you dont need OR on that statement.
Try with:
if (!is_null($type) && strlen($type) > 0 && $type !== 'before' && $type !== 'after') { ... }
Use parentheses so that you can see what belongs to what.
if ($type != null && ($type !== 'before' && $type !== 'after')) {
// ...
}
If you are not careful you can end up with problems, such as in math, where a calculation can give two different results depending on what operation you do first. For example 2*5-10 which can be either 0 or -10 depending on the order in which you multiply or subtract: (2*5)-10 or 2*(5-10).
You can also make it a little easier to follow by breaking up the logic into variables, making the if statement simpler:
$notNull = ($type != null);
$notBeforeAndAfter = ($type !== 'before' && $type !== 'after');
if ($notNull && $notBeforeAndAfter) {
// ...
}
If you know exactly what the $type variable must contain then you can also use a switch statement. It is a lot easier to understand:
switch ($type) {
case 'before':
echo '$type is "before"';
break;
case 'after':
echo '$type is "after"';
break;
// $type is not 'before' or 'after', which means that
// it is something else which we cannot use...
default:
echo '$type is an unknown value. Error maybe?';
break;
}

'If' statement with the 'or' operator in PHP

I'm trying to build up a PHP if statement with or ("||") operators, but it doesn't seem to work.
$country_code = "example_country_code";
if ($country_code != 'example_country_code' || !clientIscrawler()) {
echo 'the script can be executed';
}
else {
echo 'skipping';
}
With the given example, it should be echoed skipping, but it doesn't happen like that. What am I doing wrong?
Perhaps the double negatives are giving you problems. Let's rewrite it to:
!($country_code == 'example_country_code') || !clientIscrawler()
This can be turned into an equivalent condition with &&:
!($country_code == 'example_country_code' && clientIscrawler())
By reversing the if you would get this:
if ($country_code == 'example_country_code' && clientIscrawler()) {
echo 'skipping';
} else {
echo 'the script can be executed';
}
Therefore, in your code, it will only print skipping if clientIscrawler() is truthy.
If you have multiple conditions with the OR operator, in which case you don't want the if statement to evaluate as true, the syntax is:
if(!($something == "something" || $something == 'somethingelse')){
do stuff...
}
Here is an example:
$apples = array (
1 => "Pink Lady",
2 => "Granny Smith",
3 => "Macintosh",
4 => "Breaburn"
);
foreach($apples as $apple){
// You don't want to echo out if the apple name is "Pink Lady" or "Macintosh"
if(!($apple == "Pink Lady" || $apple == "Macintosh")){
echo $apple."<br />";
}
}
// Output is:
Granny Smith
Breaburn
In your given code, it all depends on your function call
!clientIscrawler()
You will be getting the script can be executed output only when your function call returns FALSE. I think it is returning TRUE right now, which is why you are not getting the desired output.
Maybe this can help you:
if ( ($country_code != 'example_country_code') || clientIscrawler() == false) {
Try this way:
if ( ($country_code != 'example_country_code') || !clientIscrawler()) { ...

Categories