php data testing - php

I have the following test on some data:
if(isset($option_type)
&& ($option_type == 3 || $option_type == 4
&& (isset($ext_data) && $ext_data != "")
)
){}
What it should do, is if $option type is NOT 3 or 4, the test should return true. If $option type is 3 or 4 AND $ext_data does not exist or equals "", then the test should return false.
However, no matter what $option_type is, it returns false.
How can I format this test so when the $option_type is 3 or 4 and $ext_data exists and is not "", the result is true?
lee

I would split the tests into two to make it clearer:
if (!isset($ext_data) || $ext_data == "") return false;
return $option_type != 3 && $option_type != 4;

//try this
if(isset($option_type)&& ($option_type == 3 || $option_type == 4)){
if (isset($ext_data) && $ext_data == ""){
return false;
}else{
return true;
}
}

Related

IF statement checking null variable to use min

This seems pretty simple, but I can't figure that out.
I am checking a few variables before showing some stuff on the screen, and I have a variable which can be null at some point.
On my if statement I have:
if ($a != 'abc' && ($a == $b || $b == $c) && min($variable) > 3) { ... }
How can I set it true if min($variable) is null, if all the other statements are true?
As I understood from your question,
the min($variable) maybe null and you want to check it whether it's value less than 3 or null value ?
if ($a != 'abc' && ($a == $b || $b == $c)
&& (min($variable) > 3 || min($variable) == null)) { ... }
could be using a ternary operator inside ( )
if ($1 != 'abc' && ($1 == $2 || $2 == $3) &&
( $variable == null ? true : min($variable) > 3) ) { ... }

Php OR operator gives wrong result?

I am surprised with the output of OR operator in php
$a = 5;
echo $b = ((intval($a) == 8 || 2) && intval($a) != 0 ? $a : NULL);
It echo 5 but i expect NULL
It should be like this.
$a = 5;
echo $b = ((intval($a) == 8 || intval($a) == 2) && intval($a) != 0 ? $a : NULL);
As you stated, your $a is not 8 or 2. So assume it's 5.
How your evaluation works:
((intval($a) == 8 || 2) && intval($a) != 0 ? $a : NULL);
Compare intval($a) == 8.
No matter what you get from #1, do || 2 which leads to true (non-zero number is loosely equals true)
Compare intval($a) != 0 - leads to true.
Compare true && true => true
Answer is $a.
Step-by-step:
(intval($a) == 8 || 2) && intval($a) != 0 ? $a : NULL;
(false || 2) && intval($a) != 0 ? $a : NULL);
(false || true) && intval($a) != 0 ? $a : NULL;
true && intval($a) != 0 ? $a : NULL;
true && true ? $a : NULL;
$a;
TL;DR
To get NULL, change || 2 to || intval($a) == 2
change this
intval($a) == 8 || 2
to
intval($a) == 8 || intval($a) == 2
i.e
echo $b = ((intval($a) == 8 || intval($a) == 2) && intval($a) != 0 ? $a : NULL);
The way you write comparison for OR is not possible in PHP. Use below code.
echo $b = ((intval($a) == 8 || intval($a) == 2) && intval($a) != 0 ? $a : NULL);

Undefined Offset 1 and 2

I am having Undefined 1 and 2 on line 32 and 33. I don't even have an idea of what is going on and what the error means. How can i fix this please?
$splittedGSM = str_split($gsm);
$correctGSM = TRUE;
if(count($splittedGSM ) != 11) $correctGSM = FALSE;
if($splittedGSM[0] != 0) $correctGSM = FALSE;
if($splittedGSM[1] != 7 && $splittedGSM[1] != 8 && $splittedGSM[1] != 9) $correctGSM = FALSE; //Line 32
if($splittedGSM[2] != 0 && $splittedGSM[2] != 1) $correctGSM = FALSE; //Line 33
foreach ($splittedGSM as $realgsm) {
if(!is_numeric($realgsm)){
return FALSE;
}
}
Try Below code. Offset is not defined because it's not set you have to check that if it's not set then offset will be null. You have to use isset() function for it.
$splittedGSM = str_split($gsm);
$correctGSM = TRUE;
// check using isset function
if(!isset($splittedGSM[1]))
{
$splittedGSM[1] = null;
}
if(!isset($splittedGSM[2]))
{
$splittedGSM[2] = null;
}
if(count($splittedGSM ) != 11) $correctGSM = FALSE;
if($splittedGSM[0] != 0) $correctGSM = FALSE;
if($splittedGSM[1] != 7 && $splittedGSM[1] != 8 && $splittedGSM[1] != 9) $correctGSM = FALSE; //Line 32
if($splittedGSM[2] != 0 && $splittedGSM[2] != 1) $correctGSM = FALSE; //Line 33
foreach ($splittedGSM as $realgsm) {
if(!is_numeric($realgsm)){
return FALSE;
}
}
The problem is that you try to access elements 1 and 2 of $splittedGSM without first checking that they exist. You are probably calling str_split with an empty string as argument, in which case a one-element array containing the empty string will be returned. This explains why you don't get an error when accessing $splittedGSM[0].
As you require a valid result to contain 11 elements, you can solve the problem by only accessing the array elements if the length test is satisfied, using elseif instead of if:
$splittedGSM = str_split($gsm);
$correctGSM = TRUE;
if(!is_array($splittedGSM) || count($splittedGSM ) != 11){
$correctGSM = FALSE;
}elseif($splittedGSM[0] != 0){
$correctGSM = FALSE;
}elseif($splittedGSM[1] != 7 && $splittedGSM[1] != 8 && $splittedGSM[1] != 9){
$correctGSM = FALSE;
}elseif($splittedGSM[2] != 0 && $splittedGSM[2] != 1){
$correctGSM = FALSE;
}
foreach ($splittedGSM as $realgsm) {
if(!is_numeric($realgsm)){
return FALSE;
}
}

php search within string for word?

I'm using PHP's preg_match to help determine the value of a string.
But the code only ever prints 1 or 2.
Why aren't the last two cases of my if statement ever matched?
$atype = strtolower($userData['user_type']); // let say data is :: company introducer
if ($atype == "individual introducer" || $atype == "individualintroducer" ||
(preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ) {
$atype = 1 ;
} elseif ($atype == "individual investor" || $atype == "individualinvestor" ||
(preg_match('/i/',$atype) AND preg_match('/inv/',$atype)) ) {
$atype = 2;
} elseif ($atype == "company introducer" || $atype == "companyintroducer" ||
(preg_match('/c/',$atype) AND preg_match('/int/',$atype)) ){
$atype = 3;
} elseif ($atype == "company investor" || $atype == "companyinvestor" ||
(preg_match('/c/',$atype) AND preg_match('/inv/',$atype)) ){
$atype = 4;
}
echo $atype;
You need to explain your question in a better way.
But i guess as you say the data assumed is company introducer.
So it already matches condition for the first if block.
For ex:
In company introducer
The preg_match will return true.
if($atype == "individual introducer" || $atype == "individualintroducer" || (preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ){
$atype =1 ;
}

give error if number is anything other than 1 or 0

<?php
$gender = 0;
if (($gender != 0) || ($gender != 1))
{
die('error:Must select a gender.');
}
?>
This should give a error if the gender is anything other than 1 or 0. So if i gave 5 it should die. If i gave it 1 it should not die. If i give it 0 it should not die.
I was thinking about a few work arounds
<?php
$gender = 0;
if ($gender == 0)
{
//number is okay
}
else if ($gender == 1)
{
//number is okay
}
else
{
die('error:Must select a gender.');
}
?>
Well that looks sloppy and it would work or i could create a array with 0 and 1 and check if its in it or not. Kinda overkill i think.
Not sure what i'm doing wrong.
You're using the wrong boolean operator:
if (($gender != 0) || ($gender != 1)) {
}
This will be entered if gender is 0, too, because it isn't 1, and vice versa. What you need to do is:
if (($gender != 0) && ($gender != 1)) {
}
Look at this table:
gender A (gender != 0) B (gender != 1) A || B A && B
----------------------------------------------------------------
0 false true true false
1 true false true false
5 true true true true
Also note Joshua's suggestion in the comments:
if (($gender == 0) || ($gender == 1)) {
/* number is ok */
} else {
die();
}
which is a bit longer, but more readable.
if (($gender != 0) && ($gender != 1))
^^
You want to die if both tests are true.
Change the or (||) to and (&&) and it should work, as you only want to fail, when it’s both not 0 and not 1.

Categories