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';
}
Related
I am attempting to ask:
If my variable $a is set
And if my variable $a is not apple or orange
Then do something
I'm attempting to do this with the following code, but true is returned.
Any ideas what I am doing wrong?
<?php
$a = 'banana';
if (isset($a) && ($a !== 'apple' | $a !== 'orange')) {
echo 'true';
} else {
echo 'false';
}
You probably meant && instead off || (not |):
And if my variable $a is not apple or orange
So if you don't want it to be either of those values, you'll need
($a !== 'apple' && $a !== 'orange')
So the script becomes
<?php
$a = 'banana';
if (isset($a) && $a !== 'apple' && $a !== 'orange') {
echo 'true';
} else {
echo 'false';
}
Now, false is shown if:
$a is not set
$a === apple
$a === orange
Try it online!
You're missing a | in your or expression
if (isset($a) && ($a !== 'apple' || $a !== 'orange')) {
echo 'true';
} else {
echo 'false';
}
You are missing a | OR operation is ||
if (isset($a) && ($a !== 'apple' || $a !== 'orange')) {
I have three condition/variable combination e.g. below called amounts:
$a = 15000; $b = 10000; $c = 5000;
or
$a = 10000; $b = 15000; $c = 0;
or
$a = 12000; $b = 0; $c = 15000;
etc.
At least each $a or $b or $c above is not 0 (zero).
If the amount of each not 0 (zero) it have its own associated array e.g. if $b == 0 then $b_array will not set/created, assume all is not 0(zero) then below arrays are created:
$a_array = array('id'=>1);
$b_array = array('id'=>2);
$c_array = array('id'=>3);
If $a / $b / $c is not 0 (zero) then if $b or $c not zero it needs to be linked to $a or $b (if not zero) as below:
if($a != 0 && $b != 0 && $c != 0){
$b_array['id_link'] = $a_array['id'];
$c_array['id_link'] = $a_array['id'];
} elseif ($a != 0 && $b != 0 && $c == 0){
$b_array['id_link'] = $a_array['id'];
} elseif ($a != 0 && $b == 0 && $c != 0){
$c_array['id_link'] = $a_array['id'];
} elseif ($a == 0 && $b != 0 && $c != 0){
$c_array['id_link'] = $b_array['id'];
}
The result for conditional statement above seems correct as you can check at the php sandbox
Is there any better idea for the conditional code and is there a missing condition (error handling). Any idea or solutions is greatly appreciated. Thanks!!
If I understand your question correctly, instead of checking for every combination possible, just get the first non zero number and assign it's id as id_link to all of them like below:
<?php
$id_link = -1;
if($a !== 0){
$id_link = $a_array['id'];
}else if($b !== 0){
$id_link = $b_array['id'];
}else if($c !== 0){
$id_link = $c_array['id'];
}
$a_array['id_link'] = ($b_array['id_link'] = ($c_array['id_link'] = $id_link));
Online Demo
(The brackets in the expression are added just for readability since they are redundant in case of PHP)
Update:
If you wish to leave the array with it's corresponding variable without the id_link key, then you can just use another 3 if conditions to take care of it.
<?php
$id_link = -1;
if($a !== 0){
$id_link = $a_array['id'];
}else if($b !== 0){
$id_link = $b_array['id'];
}else if($c !== 0){
$id_link = $c_array['id'];
}
if($a !== 0){
$a_array['id_link'] = $id_link;
}
if($b !== 0){
$b_array['id_link'] = $id_link;
}
if($c !== 0){
$c_array['id_link'] = $id_link;
}
Online Demo
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
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";
I'm ajaxing over to this php file.
$a = 'old';
$b = 'new';
if ($_POST['info-type'] == $a || $b)
{
$info = $_POST['info-type'];
$query = "SELECT * FROM `tld` WHERE type = '".$var."'";
}
$query = "SELECT * FROM `tld` ";
$result = mysqli_query($link,$query);
while($row = mysqli_fetch_assoc($result))
{
echo '<div>'.$row['something'].'</div>';
}
The data posted is either 'all' 'new' or 'old'.
If I send the data as either new or old, the script works and outputs as expected.
If the posted data is neither new or old but all instead, it fails and don't show any errors or respond anything back (I've monitored via dev tools as well).
So, I tried this:
if ($_POST['info-type'] == $a || $b)
{
$info = $_POST['info-type'];
$var = "SELECT * FROM `tld` WHERE type = '".$var."'";
} elseif ($_POST['info-type'] == 'all')
{
$query = "SELECT * FROM `tld` ";
}
But the script still fails. If i fully remove the IF statements and use the query without the WHERE clause like it is after the elseif, it works?
This statement is very odd:
if ($_POST['info-type'] == $a || $b) {
You probably intended:
if ($_POST['info-type'] === $a || $_POST['info-type'] === $b) {
Also, in this case === is good since the type of both variables is string.
$_POST['info-type'] == $a || $b will always be true if $b is truethy (which it is).
You need to compare $_POST['info-type'] with both $a and $b:
if ($_POST['info-type'] == $a || $_POST['info-type'] == $b)
The problem with what you have is that $_POST['info-type'] == $a || $b sees if $_POST['info-type'] == $a is true OR if $b is true.
Since $b is true (it is non-empty string), the if condition will always be true.
If $_POST['info-type'] can only be all new or old, then the following will be more elegant:
if ($_POST['info-type'] == 'all')
{
$query = "SELECT * FROM `tld` ";
}
else // info type will be old or new
{
$info = $_POST['info-type'];
$query = "SELECT * FROM `tld` WHERE type = '".$info."'";
}