how works or operator in if statements and print - php

I have two questions
1) how can i make php to check getnext() function,if it exists or what value it has ???
$a = 1;
if($a == 1 or getnext()== 1){
echo "yeap"; //this works
}
2)i want to write condition -- if $a or $b is equal to 1 , print the variable name that has the value of 1.
Is it possible to do in php ??can i do it this way???
if($a ==1 or $b==1){
print($a or $b);
}
thanks in advance:)

You could for example do as follows :
if($a ==1 || $b==1){
print (($a == 1)? $a : $b);
}

For your first question,
your getnext() function must be return some valid integer value then only you can compare it with integer 1.
for second,
You should write
if($a == 1 && $b == 1){
echo 'both are 1';
}
else if($a == 1){
echo '$a is 1';
}
else if($b == 1){
echo '$b is 1';
}else{
// both are not 1
}
also see this links,
logical operators or vs || (double pipe) in php
PHP: return value from function and echo it directly?

You write condition? You use if and else, if no else in if you put return. Goodluck

Related

If-Else loop output

<?php
$x = 1;
if ($x == 2)
print "hi" ;
else if($x = 2)
print $x;
else
print "how are u";
?>
Apologies for this basic question as I am a beginner at php.
I was expecting the else statement to be executed and print "how are u", but it executed the elseif statement and printed '2' instead. May I ask why does $x become assigned to 2? Thanks in advance.
<?php
$x = 1;
if ($x == 2)
print "hi" ;
else if($x = 2) /* you assign $x 2 and it's true */
print $x;
else
print "how are u";
?>
In the elseif you are asigning the number 2 to $x which will always return true. Because of that it will never go to the else block.
Dont use = when doing comparisons but == or ===.
<?php
$x = 1;
if ($x == 2)
print "hi" ;
else if($x == 2)
print $x;
else
print "how are u";
?>
this is the right way to do it. Also it doesnt make sense to check both in if and elseif that $x == 2 since it will never execute the elseif block.
Let me try to explain it better, if you do:
if ($x = 2)
thats will assign 2 to $x and this is then the same as:
if ($x)
since the $x now holds the number 2 this is also the same as:
if (2)
this is always true so it will never go to further checks no matter what the number is.
You are SETTING x=2 in your else if --> else if($x = 2) -- This would be more appropriate .. Check for absolute === .. Then check for truthy == .. IE
<?php
$x = 1;
if ($x === 2)
print "hi" ;
else if($x == 2)
print $x;
else
print "how are u";
?>
Conversely .. You can play with truthy vs absolute by comparing an integer to string too .. Like:
<?php
$x = 2;
if ($x === 2) // Will return true
print "Is absolute integer" ;
else if($x == 2) // Will return true
print "Is truthy integer";
else if($x === '2') // Will return false
print "Is absolute string";
else if($x == '2') // Will return true
print "Is truthy string";
else
print "how are u";
?>
Which will never reach the else .. But you can see how operators can make or break a program ..
This is because = is assignment operator not comparison like ==. That is why $x is assigned value 2 and the else if condition is met and executed.

PHP If Statement is met display Either

I am familiar with using if else statement but my problem is how to display either if the condition is met.
if ($a == 1) {
echo 'B' OR 'C'; // just for reference
}
I have finally figure this out using nested loop
if ($a == 1) {
choiceresult = mt_rand(1,2)
if (choiceresult == 1) {
echo 'B';
}
if ( choiceresult == 2) {
echo 'C';
}
}
you have to fix your condition first :
if($a == 1)
Instead pf
if($a = 1)

How to test if $a is divisible by $b when $b is larger than $a

I'm trying to work out if one string, $a, is divisible by another, $b.
All of the examples I can find tell me to use modulus, e.g.:
if(($a %$b) == 0) : echo "Is dividible" ; endif;
However, because modulus returns the remainder of the calculation, this doesn't work if $b is larger than $a, because there's still no remainder.
How do I check divisibility where $b is sometimes (but not always) larger than $a?
why don't you do this as a function:
function isDivisible($smaller,$bigger){
//handle division by zero, and hmm.. let's cover negative numbers too
if($smaller<=0) return false;
if($smaller>$bigger) return false;
return !($bigger % $smaller);
}
The negation ! should be a working and elegant way to handle it.
How about:
echo ( ($a < $b) && (($a % $b) == 0) ) ? "Is dividible" : "Is not divisable" ;
if($a==$b)
{echo "divisible a and b are equal";
}
else if($a>$b){
if(($a %$b) == 0) : echo "Is dividible" ; endif;
}
else{
echo "\$b is either large or equal to \$a";
}
Try this it should work:
$a = 7;
$b = 14;
//echo ( ($a > $b) && ( ($a % $b) == 0) ) ? "is divisible":"no divisible";
echo ( ($a < $b) && (($b % $a) == 0) ) ? "Is dividible" : "Is not divisable" ;
You can use ternary operator as example given below
(($a%$b)==0)?echo "Is divisible": echo "not divisible";

Elegant way to shorten if statement

Any ideas how to shorten if statment in an elegant way.
My if statement:
if(getfoo1() == getfoo2() && getfoo2() == 1)
{
}
EDIT:
I'm looking for something like:
if(getfoo1() == getfoo2() ==1)
{
}
But I suppose we can't do this.
$a = getfoo1();
$b = getfoo2(); // less operations, while it not produces duplicate calls
if($a == $b && $b == 1){
// do something
}
$variable = ((getfoo1() == getfoo2() && getfoo2() == 1) ? $value1 : $value2);
More elegant, combined:
$a = getfoo1();
$b = getfoo2();
$variable = (($a == $b && $b == 1) ? $value1 : $value2);
Since we don't know the possible return values from the functions, if you assume they are integers then you can say:
$a = getfoo1();
$b = getfoo2();
if (($a * $b) === 1) { // strict equality for the win
echo 'hi';
}
The result would only be true iff both $a AND $b are 1.
Another way:
$both = array(getfoo1(), getfoo2());
// use array_diff_assoc so it checks multiple occurrences of the same value
$diffCount = count(array_diff_assoc($both, array(1, 1)));
if ($diffCount === 0) {
echo 'hi';
}
Since anyway getfoo2() == 1 must be true, a better approach is to first check whether getfoo2() is equal to 1. If it false no matter about 2nd condition. But If you first check getfoo1() == getfoo2() and and then check getfoo2() == 1 you have to check 2 conditions all the times.
Therefore go for
$a = getfoo1();
$b = getfoo2();
if($b == 1 && $a == $b)
{
// logiv
}
else
{
}
Try this.
$a = getfoo1();
$b = getfoo2();
if( intval($a && $b) === 1) {
echo 'hi';
}

How to differentiate between 0 and an empty variable?

Lets say I have a variable $a that holds an integer between 0 to 10. $a needs to be not empty/not null to run a code. It works when the integer is between 1 to 10, but when it is 0, it treats $a as empty.
$a = 5;
if (!empty($a))
{ echo "not empty"; }
else
{ echo "empty";}
This this case it echoes "not empty".
$a = 0;
if (!empty($a))
{ echo "not empty"; }
else
{ echo "empty";}
But in this case it echoes "empty".
Is there a way to differentiate between 0 and an empty variable?
check for null an a string length of 0
if(null === $a || strlen($) === 0) {
}
Instead of empty you could use isset: http://fr2.php.net/isset
if (isset($a))
{ echo "not empty"; }
else
{ echo "empty";}
You can use the strict equality operator === to test for zero:
$a = 0;
$isZero = $a === 0; // $isZero is true
There is a difference between EMPTY and NULL. For $a = 0, $a is EMPTY but not NULL. So you can use isset($a) to check if $a is NULL or not.
if (isset($a)) {
echo 'not null';
} else {
echo 'is null';
}

Categories