If and elseif's not working - php

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

Related

If either of two cookies exist do something, in PHP

I know that || or && need to be used but I can't work out the correct or best way to format this.
My code for one cookie:
if(isset($_COOKIE['mycookie'])) {
if($_COOKIE['mycookie']=="value1") {
// do some stuff
}
}
But I'd like to include another cookie in this routine where either one can be true for the "stuff" to work.
I'm just not sure how to format this. Is it something like this?
if(isset($_COOKIE['mycookie'] || ['mycookie2')) {
if($_COOKIE['mycookie']=="value1" || $COOKIE['mycookie2']=="value2") {
// do some stuff
}
}
You can write all in one if statement if you want like this:
(The OR statement in the isset() function is not going to work)
if ( (isset($_COOKIE['mycookie']) && $_COOKIE['mycookie'] == "value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] == "value2") )
You need to do the || outside the function, to combine the results of all the calls.
if (isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2'])) {
// do some stuff
}
It will be:
if (isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2'])) {
if ($_COOKIE['mycookie'] == "value1" || $_COOKIE['mycookie2'] == "value2") {
// do some stuff
}
}
Or even:
if ((isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2') && ($_COOKIE['mycookie'] == "value1" || $_COOKIE['mycookie2'] == "value2")) {
// do some stuff
}
to avoid nested if.
Try this
if((isset($_COOKIE['mycookie']) && $_COOKIE['mycookie']=="value1")
|| 9isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] =="value2" )) {
// do some stuff
}
Try this. It puts all requirements in one if statement:
if( (isset($_COOKIE['mycookie'] && $_COOKIE['mycookie']=="value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2']=="value2") ) {
// do some stuff
}
You can use one if condition instead of nested if. If you required to validate both then
if(isset($_COOKIE['mycookie'], $_COOKIE['mycookie2']) && ($_COOKIE['mycookie'] == "value1" && $_COOKIE['mycookie2']=="value2")) {
// do some stuff
}
Or if you have to validate one of them then
if((isset($_COOKIE['mycookie']) && $_COOKIE['mycookie']=="value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] == "value2") ) {
// do some stuff
}

Nested if's not working in php either does nothing or goes to the end of the code

I have been getting frustrated with this code for hours now and I just have to bow and ask for some help. I am trying to get these nested if's to work but it is either returning nothing and I am getting no errors or it is going to the end of the coding where it should have stopped beforehand. These inputs are all from groups radio buttons and the reason for the order is so that they would filter through from the simplest to the more complex once it gets to the function that is being called. I tried nested if's at first and that was my result and then tried switch as well and wound up in the same place with it. Here is where I am:
if ($req === "remind" || "tip" || "half" || "secHalf" || "fin")
{
//some code;
}
else
{
if ($manUpdate === "first15" || "first10" || "first5" || "sec15" || "sec10" || "sec5")
{
//some code;
}
else
{
if ($TypeA === "free throw.")
{
if($TypeA === "free throw." && $PointA === "1")
{
//some code;
}
else if($TypeA === "free throw." && $PointA === "2")
{
//some code;
}
else if($TypeA === "free throw." && $PointA === "3")
{
//some code;
}
}
else if ($TypeB === "free throw.")
{
if($TypeB === "free throw." && $PointB === "2")
{
//some code;
}
else if($TypeB === "free throw." && $PointB === "2")
{
//some code;
}
else if($TypeB === "free throw." && $PointB === "3")
{
//some code;
}
}
else
{
if ($TypeA === "shot." || "Slam Dunk!" || "from Downtown!")
{
//some code;
}
if ($TypeB === "shot." || "Slam Dunk!" || "from Downtown!")
{
//some code;
}
}
}
}
I know that the last "if" of the iteration is not supposed to be "else if" but it is because otherwise I get the syntax error for the unexpected "{" under the if parameters. I also have tried it similar to the final 2 "if's" where I just put if for them all and didn't nest them but that does work through the second if statement then it just goes to the end each time and bypasses all of the other possibilities.
I also know that the code within each of the statements work because when tried individually on their own pages they do function as intended so there was no reason to throw all that extra coding in there.
Any help is greatly appreciated as I haven't dealt with nesting if's further than just one inside the other before and this appears as I think it should but of course if we all knew everything we wouldn't need to ask? Thank you in advance for taking a look and any help that you can offer!
EDIT So here is the working code as listed below but I have ran into a new issue. It seems that I can run 1 instance of the code but it works beautifully but when I copy a new set of files in the folder to another file and run it the code inside is now broken and while the page that this links to fails to do anything. It acts as if everything is working properly and I am not getting any php errors in the log. Wondering if I need to change the session names due them being the same in both sets of files but they are in 2 separate folders as ultimately I intend to have 5 instances of this script running simultaneously.
if ($req === "remind" || $req === "tip" || $req === "half" || $req === "secHalf" || $req === "fin")
{
ManualBasic();
}
else
{
if ($manUpdate === "first15" || $manUpdate === "first10" || $manUpdate === "first5" || $manUpdate === "sec15" || $manUpdate === "sec10" || $manUpdate === "sec5")
{
Manual510();
}
else
{
if ($TypeA === "free throw.")
{
if($TypeA === "free throw." && $PointA === "1")
{
// some code
}
else if($TypeA === "free throw." && $PointA === "2")
{
// some code
}
else if($TypeA === "free throw." && $PointA === "3")
{
// some code
}
}
else if ($TypeB === "free throw.")
{
if($TypeB === "free throw." && $PointB === "1")
{
// some code
}
else if($TypeB === "free throw." && $PointB === "2")
{
// some code
}
else if($TypeB === "free throw." && $PointB === "3")
{
// some code
}
}
else
{
if ($TypeA === "shot.")
{
// some code
}
if ($TypeA === "Slam Dunk!")
{
// some code
}
if ($TypeA === "from Downtown!")
{
// some code
}
if ($TypeB === "shot.")
{
// some code
}
if ($TypeB === "Slam Dunk!")
{
// some code
}
if ($TypeB === "from Downtown!")
{
// some code
}
}
}
}
Try this,
if ($req === "remind" || $req === "tip" || $req === "half" || $req === "secHalf" || $req === "fin")
instead of (As AD7six stated - unintentional/illogical)
if ($req === "remind" || "tip" || "half" || "secHalf" || "fin")
Your IF statements are not doing what you think they are doing as that is not correct syntax for checking if a variable has more than one possible value. There are a few ways to do it but this might be the most concise:
if ($req === "remind" || "tip" || "half" || "secHalf" || "fin")
becomes
if (in_array($req, array("remind", "tip", "half", "secHalf", "fin")))

How do I extract a single column from another database?

Why this condition passes even if I change the $_GET variable?
I've this code
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && $_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc') { /*rest goes here*/ } else {redirect}
Link returns like this
http://localhost/system/results.php?script_id=2&results=reorder&sort_column=supplier_address&sort_order=desc
But when I change this sort_column=supplier_address to say for example sorcodsalumn=supplier_address it doesn't redirect, instead goes ahead, any idea why? But if I simply remove few letters and dont replace with something else it does redirect...
How come if am using this isset($_GET['sort_column'] and am modifying sort_column to something else still passes this condition
Basic PHP operator precedence... && evaluates before ||, so your entire statement boils down to:
(x && y && z && ....) || ($_GET['sort_order'] == 'desc')
You need to simplify that if(), add some () to enforce your own evaluation order, and then things should start working a bit better.
your AND's and OR's need to be bracketed properly.
else if (isset($_GET['results']) &&
$_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) &&
$_GET['sort_column'] != '' &&
isset($_GET['sort_order']) &&
$_GET['sort_order'] != '' &&
($_GET['sort_order'] == 'asc' || $_GET['sort_order'] == 'desc'))
{
/*rest goes here*/
} else {
redirect
}
More specifically your last || needs its own brackets, as shown above.
You need to put a bracket around your || (OR) statement like this:
elseif(isset($_GET['results']) && $_GET['results'] == 'reorder' &&
isset($_GET['sort_column']) && $_GET['sort_column'] != '' && isset($_GET['sort_order'])
&& $_GET['sort_order'] != '' && ($_GET['sort_order'] == 'asc'
|| $_GET['sort_order'] == 'desc')) { /*rest goes here*/ } else {redirect}
Otherwise your statement will return true anytime sort_order is set to 'desc'.

if statement with multiple condition in 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;
}

Simple PHP Condition help: if($Var1 = in list($List) and $Cond2) - Is this posslbe?

Is this a possible function?
I need to check if a variable is existent in a list of ones I need to check against and also that cond2 is true
eg
if($row['name'] == ("1" || "2" || "3") && $Cond2){
doThis();
}
It's not working for me and all I changed in the copy paste was my list and the variable names
if(in_array($row['name'], array('1', '2', '3')) && $Cond2) {
doThis();
}
PHP's in_array() docs: http://us.php.net/manual/en/function.in-array.php
You're lookin for the function in_array().
if (in_array($row['name'], array(1, 2, 3)) && $cond2) {
#...
if (in_array($name , array( 'Alice' , 'Bob' , 'Charlie')) && $condition2 ) {
/* */
}
use in_array function
if(in_array($row['name'], array(1,2,3)) && $cond2){
do ...
}
$name = $row['name'];
if (($name == "1" || $name == "2" || $name == "3") && $cond2)
{
doThis();
}
I have something simpler than that, if it's still possible...
if(strpos("1,2,3", $row['name']) !== false) && $Cond2) {
doThis();
}

Categories