PHP IF construct when dealing with a part that is an exception - php

I'm having some trouble getting my code to work. And I lost my overview. Who can give me some insight?
I'm looking for how to build an IF construct.
If I would describe the IF construct in words:
MismatchBundle = true unless (adminPass = true & user = admin)
I wrote the following code, but this didn't seem to work:
if (($MismatchBundle == true) && ($adminPass != true && $thisUser['rankid'] != 1)
Anyone who knows how to get the 'unless' part in the code?
What about this?
if (($MismatchBundle == true) && !($adminPass == true && $thisUser['rankid'] == 1)
Is this allowed with the ! before a () section?

if (($MismatchBundle == true) && !($adminPass == true && $thisUser['rankid'] == 1)
Using ! on the entire section within ( ) worked as intended.

Related

Why is my IF statement not working to define membership level?

I am very new to PHP and trying to get an IF statement to work to change the membership level based on the possibility of multiple answers provided in the registration form. However, it's not working. If anyone can help me understand what I am doing wrong/missing that would be a great help. Here is my code. Thank you.
function my_pmpro_checkout_level($level)
{
if ( ! empty( $_REQUEST['registrationlevel']) && $level->id == '1' ) {
if (( $_REQUEST['registrationlevel'] == 'eligibleforlimited' && $_REQUEST['experience'] != 'morethansixyears') || ($_REQUEST['registrationlevel'] == 'eligibleforlimited' && $_REQUEST['timeassesment'] == 'no')) {
$level->id = '3';
} elseif ( $_REQUEST['registrationlevel'] == 'eligibleforprovisional' && $_REQUEST['experience'] != 'noexperience') {
$level->id = '3'}
}
return $level;
}
add_filter("pmpro_checkout_level", "my_pmpro_checkout_level");

if isset statement is incorrect

I want to check if the reaction count is 0 then go in the if statement. The part without && ($app->count_reactie($topic['id']) == 0) does work but when I add this it won't work.
In the line above this code I use: implode($app->count_reactie($topic['id']))and it works so it is not that this code doesnt work. This code will return the number of reactions.
But this code down below is what I am trying to get to work.
if(isset($actiefboardid)){
$toppic = $app->get_topics($actiefboardid);
foreach($toppic as $topic){
if(isset($_SESSION['klant_id']) && ($_SESSION['klant_id'] == $topic['klant_id']) && ($app->count_reactie($topic['id']) == 0)){
}}}
Hopefully someone can help me.
Try this:
$count = $app->count_reactie($topic['id']);
if(isset($_SESSION['klant_id']) && ($_SESSION['klant_id'] == $topic['klant_id'])
&& ($count['COUNT(reactie)'] == 0)){

Boolean handling in PHP

I have following code in my php script:
if ( $x != 'some_val_1' && $y['index']->title != "some_val_2" && $y['index']->title != "some_val_3" ) {
// Do something.
}
In the code shown above, index may/may not be set and thus when it is not set this code throws a php notice.
I changed it to:
if (isset($y['index']->title)) {
if ( $x != 'some_val_1' && $y['index']->title != "some_val_2" && $y['index']->title != "some_val_3" ) {
// Do something.
}
}
However since the "Do something" part is set to work when isset($y['index']->title is not equal to some_val_2 and some_val_3, the above does not work because the isset() condition I added skips the code.
So now the script does not throw notice but at the cost of completely changing the condition to something not desirable.
How can I change this code to not throw the PHP notice?
You can handle it in different ways, I initialize the variables if they are not defined:
if (! isset($y['index']->title)) {
$y['index']->title = '';
}
if ( $x != 'some_val_1' && $y['index']->title != "some_val_2" && $y['index']->title != "some_val_3" ) {
// Do something.
}
I prefer this way because I don't need to change my code and add if conditions (with isset for example) in all my program. I just add the conditions at the top of my code.
Since you are dealing with an array of objects, you can use the function array_key_exists().
Your code will look like this:
if ( array_key_exists('index',$y) && $x != 'some_val_1' && $y['index']->title != 'some_val_2' && $y['index']->title != 'some_val_3' ) {
// Do something.
}
You can read more about it here: http://php.net/manual/en/function.array-key-exists.php
You can try using isset & in_array in this scenario.
if ( $x != 'some_val_1'
&& isset($y['index']->title)
&& !in_array($y['index']->title, array("some_val_2", "some_val_3") ) ) {
// Do something.
}
The benefit of using in_array is that you validate case-sensitivity by passing TRUE / FALSE in the third parameter.

PHP: If-statement, need value of condition that triggered

I'm sorry for the vaguely described title. This is what I want:
if($a[$f] === false || $a[$g] === false || $a[$h] === false || $a[$i] === false || $a[$j] === false)
{
// do something
}
I want to do something with the condition that actually triggered the statement (if a[$f] = true and a[$g] = false, I want to do something with $g).
I know that in this case, the first statement that went true (i.e. $a[$g] == false) triggers. But is there any way to do something with $g? I've never seen this in my programming life before and can't seem to find anything about it.
Thanks in advance.
--- Edit ---
I forgot to mention: I'm using a function on all the array data. So, shortened, I get this:
if(valid($a[$f]) === false || valid($a[$g]) === false)
{
// do something
}
--- Edit 2 ---
This piece of OOP-based PHP, where I'm in a class, is my code.
if($this->validatedText($product[$iName]) == false ||
$this->validatedUrl($product[$iUrl]) == false ||
$this->validatedNumber($product[$iTax]) == false ||
$this->validatedValuta($product[$iPrice]) == false ||
$this->validatedText($product[$iArticleNumber]) == false ||
$this->validatedText($product[$iDescription]) == false ||
$this->validatedText($product[$iMetaDescription]) == false ||
$this->validatedText($product[$iTitle]) == false)
{
// do something with the first iVariable
}
Simplest solution will be
if(false!==($sIndex = array_search(false, $a, 1)))
{
//your $sIndex is first index with false value
}
if you want all keys, you may use array_filter(), like this:
$rgFalse = array_keys(array_filter($a, function($x)
{
//here valid is your function
return false===valid($x);
}));

PHP if not statements

This may be the way my server is set up, but I'm banging my head against the wall. I'm trying to say that if $action has no value or has a value that is not "add" or "delete" then have an error, else keep running the script. However, I get an error no matter what $action is.
$action = $_GET['a'];
if((!isset($action)) || ($action != "add" || $action != "delete")){
//header("location:index.php");
echo "error <br>";
}
$action is being set properly and if run something like if($action =="add") it works. This is on my local host, so it could be a settings issue.
Your logic is slightly off. The second || should be &&:
if ((!isset($action)) || ($action != "add" && $action != "delete"))
You can see why your original line fails by trying out a sample value. Let's say $action is "delete". Here's how the condition reduces down step by step:
// $action == "delete"
if ((!isset($action)) || ($action != "add" || $action != "delete"))
if ((!true) || ($action != "add" || $action != "delete"))
if (false || ($action != "add" || $action != "delete"))
if ($action != "add" || $action != "delete")
if (true || $action != "delete")
if (true || false)
if (true)
Oops! The condition just succeeded and printed "error", but it was supposed to fail. In fact, if you think about it, no matter what the value of $action is, one of the two != tests will return true. Switch the || to && and then the second to last line becomes if (true && false), which properly reduces to if (false).
There is a way to use || and have the test work, by the way. You have to negate everything else using De Morgan's law, i.e.:
if ((!isset($action)) || !($action == "add" || $action == "delete"))
You can read that in English as "if action is not (either add or remove), then".
No matter what $action is, it will always either not be "add" OR not be "delete", which is why the if condition always passes. What you want is to use && instead of ||:
(!isset($action)) || ($action !="add" && $action !="delete"))
You're saying "if it's not set or it's different from add or it's different from delete". You realize that a != x && a != y, with x != y is necessarily false since a cannot be simultaneously two different values.
You could also try:
if ((!isset($action)) || !($action == "add" || $action == "delete")) {
// Do your stuff
}
For future reference, you can quickly create a truth table to check if it evaluates the way you want... it's kind of like Sudoku.
(!isset($action)) && ($action != "add" && $action != "delete"))
Example:
column 1 is issetaction, column 2 and 3 evaluates !="add","delete" respectively
if($a=add) T && (F && T) => T && F => FALSE
if($a=delete) T && (T && F) => T && F => FALSE
if($a=nothing) T && (T && T) => T && T => TRUE
I think this is the best and easiest way to do it:
if (!(isset($action) && ($action == "add" || $action == "delete")))
Not an answer, but just for the sake of code formatting
if((isset($_GET['a'])) $action=$_GET['a']; else $action ="";
if(!($action === "add" OR $action === "delete")){
header("location: /index.php");
exit;
}
Note the exit; statement after header(). That's the important thing. header() does not terminate script execution.

Categories