PHP if statement strpos === false with an or || - php

I'm trying to get the if statement to echo equals true but it equals false. What am I doing wrong?
sometimes $line-2 will be 5% and sometimes it will be 0%. So it varies like that. How would I write the if statement for both cases?
So basically, I want it to echo equal true if the variable line_2 is 0% or is 5%. I only want it to echo equals false if the variable is anything other than 0% or 5%.
$line_2 = '5%'; // this will be random. Can be 0%. Can be 5%. Can be 25%.
if (strpos($line_2, '5') === false || strpos($line_2, '0') === false) {
echo 'equals false';
} else {
echo 'equals true';
}
more clarification.
Where it says echo 'equals false' there is actually a command there to execute another php script. I need that script to be executed only if line_2 does not equal 5% or it does not equal 0%.
Hope that sums it up.

Your problem stems from a misunderstanding of the way you negate conditional statements.
You mention that you want to echo true if $line_2 contains a 0 OR a 5; however, your if statement checks the opposite condition to echo false and will echo true if the condition fails. So we need to change the condition from if line contains 0 or line contains 5 to if not (line contains 0 or line contains 5).
The way you accomplish this is by considering De Morgan's laws. Specifically, when you distribute negation across logical OR or logical AND, the following holds true:
not (A OR B) = (not A) AND (not B)
not (A AND B) = (not A) OR (not B)
That is, you distribute the negation and change the logical operator to its opposite.
In this case, since you want to accomplish not (line contains 0 or line contains 5), you should distribute it such that you obtain (not line contains 0) and (not line contains 5), which looks like this:
$line_2 = '5%'; // this will be random. Can be 0%. Can be 5%. Can be 25%.
if (strpos($line_2, '5') === false && strpos($line_2, '0') === false) {
echo 'equals false';
} else {
echo 'equals true';
}
The problem with your current code is that you instead have a solution resembling if (not line contains 0) or (not line contains 5). The difference between the two is only in the logical operator connecting the two conditions, where you're using or but should really be using and.

Based on OP's edit, you should reverse your if condition check logic. Do the following:
$line_2 = '5%'; // Or it can be 0%
if (strpos($line_2, '5') !== false || strpos($line_2, '0') !== false) {
echo 'equals true'; // 0 or 5 is found in the string
} else {
echo 'equals false';
}

You don't really need strpos() for this unless $line_2 might sometimes contain more strings.
<?php
$line_2 = '5%';
$statement = 'equals false'; //Set the default message
if ($line_2 === '5%' || $line_2 === '%0') { //Check if $line_2 equals 5% or 2%. Note the strong comparison since $line_2 is a string.
$statement = 'equals true';
}
echo $statement;

Use && instead
Using the OR operator in that if statement will return true if that character is '5' OR '0'
So, becausestrpos($line_2, '0') === false will return true, your statement will become
if(false || true)
thus echoing 'equals false', whereas using AND
if(false && true)
will be false, giving you 'equals true'

Related

Why does this do while loop not end when it's supposed to?

I'm trying on PHP 7.4
<?php
function test(){
do{
$val=(int)readline("Insert a number in the range of 1-5 :");
print_r(($val>5 || $val!==0)."\n");
}while ($val>5 || $val!==0);
}
test();
But it just doesn't work as expected. It just leaves the loop when I insert 0, but not when I insert a number less than or equal to 5.
This condition is incorrect for what you're trying to do.
while ($val>5 || $val!==0)
None of the numbers you want to cause the loop to end are equal to zero, so the $val!==0 part of the condition will always be true unless $val is zero.
If either part of an or expression like $val>5 || $val!==0 is true, then the entire expression is true.
You need this instead:
while ($val > 5 || $val < 1)

Why does this conditional statement not work?

I have this function in PHP to check if a number is 1 or 0, and if it isn't to die no access. This is for a lightweight security system implemented in one of my games to help prevent most cheating.
$number = 1;
if ($number<>0 || $number<>1){
die("nope");
}
However, when I run this code above, nope is echoed. Why?
The <> comparison operator is the same as != (not equal). The || is an OR comparison operator. In the conditional statement above if either expression is not true, the code block will execute. since 1 != 0, the code block will execute.
You are checking if $number is greater/less than 0 OR greater/less than 1. Since 1 is greater than 0 the first condition is true and the statement is true. So you get the message 'nope'.
Change it to this:
if ($number <> 0 && $number <> 1){
Your code always pass, it checks if $number is NOT one or is NOT zero - try proper solution:
$number=1;
if (!($number==0 || $number==1)){
die("nope");
}
your first condition $number<>0 is true. <> means not equal to(!=). 1 != 0 is true.
so it is always inside if condition.
Your condition $number<>0 || $number<>1 will be true when $number is 1 because 1 is not equal to 0. Since you are using ||, it will short-circuit since true || <anything> is true.
You want to use && here instead. To check whether $number is not equal to either 0 or 1.
$number=1;
if ($number<>0 && $number<>1){
die("nope");
}
Following De Morgan's laws, you can also do:
$number=1;
if (!($number==0 || $number==1)){
die("nope");
}

PHP order of logical operators

I'm curious about how PHP handles conditional statements / order of operations with nesting. If I use the following if condition:
if(x == (1 || 2))
{
// do something
}
I would expect it to behave the same as
if(x == 1 || x == 2)
{
// do something
}
...but it doesn't. My first example seems like it would be a handy shorthand that makes pretty good sense, but it doesn't do what I expect. Can anyone shed some light on the issue? What exactly does PHP do with my first statement?
So for this piece of code:
if ($x == ( 1 || 2))
{
// do something
}
In PHP, any non-zero number is considered true. Disclaimer: This fact isn't necessarily true in other languages. So in PHP, 0 is the only number considered false. So you're asking if $x == true in the above piece of code.
Hence, whenever $x is any number other than 0 the statement inside the if will resolve as true. However, when $x = 0 then that is equivalent to saying false == true which of course will resolve as false.
This article might help: PHP: Booleans
Your shorthand is logically invalid. In almost every case you'll have to write out the full logical cases for all possibilities you want to test for.
I say 'almost' because in PHP you can do something ridiculous like:
if( in_array($x, array(1,2)) ) {
// code!
}
x == (1 || 2)
evaluates like this:
(1 (if its false) then testing for 2, if not, the expression returns true)
now it will become:
if(x==true)?
Another example taken from (PHP.NET):
// foo() will never get called as those operators are short-circuit
$b = (true || foo());
See here about the precedence of an operator
http://php.net/manual/en/language.operators.precedence.php
It will behave the same as math (think BEDMAS), with the brackets being executed first. So your example is behaving as:
if (x == ( 1 || 2)) {
//code
}
and because 1 and 2 are both non-zero values (thus both true), you get:
if (x == true) {
//code
}
Unfortunately to get what you want you'll need:
if (x == 1 || x == 2) {
//code
}
how would it make sense? you are asking the computer to execute the following logical expression:
if x == (1 || 2) which is same as x == (the result of 1 || 2)
so your expression would be x == true since 1 || 2 would return true
computers do whatever you tell them to do
if(x == (1 || 2))
{
// do something
}
OR and AND operations come back with TRUE or FALSE
you statement says - if x equals (true) - as 1 or 2 will always be true
- this just doesn't make sense...
In first if evaluate result of 1||2 and check that it equal to x
In second its like this or this or this
however in first you can see that var_dump(1 || 2) returns every time true so
$x = 3;
var_dump($x == 1 || 2);
if($x == 1 || 2){
echo 'inside if';
}
is also true so it will print inside if even $x is 3
so imo second way is way to go

Should I be casting as a boolean or fake a boolean?

If I get an integer variable (anywhere from 0+) There are a few things I can do to make sure the number is not 0(zero):
Option 1:
if($number > 0){
// number is not zero
}
Option 2:
if($number){
// number is not zero
}
Option 3:
if((bool) $number){
// number is not zero
}
Option 4:
if(!!$number){
// number is not zero
}
Etcetera....
Which one of the above is considered really the best to do?
Or is there an even better option?
Use the identical comparison operaton, which does not do any type juggling (and is faster).
if ($number !== 0) {
// ^^^
// Number is not identical to 0
}
Note: This is assuming the variable is actually a "integer variable", and not a string that happens to contain a number.
if (false == ($number === 0)) {
// ^^^
// It is false that Number is identical to 0
}
I've always used this:
if(!empty($number)){
// Number is not 0
}
I think of it as two birds with one stone.
Variable set.
Not 0
if($number != 0){
// Number is not 0
}

Explain php if statement

Can someone please explain in detail what these if statements are doing?
What does the three === signs to in the first one, and What does the single & in the second mean?
$aProfile = getProfileInfo($iId);
if($aProfile === false)
return false;
if(!((int)$aProfile['Role'] & $iRole))
return false;
=== tests for type-safe equality.
'3' == 3 will return true, but '3' === 3 will not, because one's a string and one's an integer. Similarly, null == 0 will return true, but null === 0 will not; 10.00 == 10 will return true, but 10.00 === 10 will not.
& is the bitwise AND operator. It returns a bitmask in which a bit is set if both the corresponding bits are set from the original two bitmasks.
For example:
$x = 5;
$y = 17;
echo $x & $y;
causes 1 to be echoed. $x is ...000101, $y is ...010001. The only bit that is set in both of them is the rightmost one, so you get ...000001, which is 1.
Here's a good guide to PHP operators:
http://www.tuxradar.com/practicalphp/3/12/3
See the section on bitwise operators for info on the &.

Categories