I have conditional PHP working fine, the condition is if the url ends with url-1:
if ($currentpage == '/url-1') {
How can I change this so the url can be either url-1 or url-2? I cant get the syntax right.
Thanks
if ($currentpage == '/url-1' || $currentpage == '/url-2')
You need the || (logical OR) operator. More information on logical operators.
You can use it like so:
if ($currentpage == '/url-1' || $currentpage == '/url-2') {
Be sure not to use:
if ($currentpage == '/url-1' || '/url-2') {
as this is valid but does not do what you would expect it to do.
Fella...that was hard to believe....
if ($currentpage == '/url-1' || $currentpage == '/url-2') {
Have you tried REGEX?
$pattern = '/^\/url-[12]+/';
if (preg_match($pattern, $currentpage, $matches, PREG_OFFSET_CAPTURE)) {
}
That way, if you have lots of pages (1,2,...), you don't need a special case for each.
if ($currentpage == '/url-1' || $currentpage == '/url-2') {
try
if ($currentpage == '/url-1' || $currentpage == '/url-2') {
Related
In php the following code gives a warning of undefined variable $x:
if($x = 1 && $x == 1)
I thought it was equivalent to if( ($x = 1) && ($x == 1) ), but that's not the case. I've been told, it's because && has higher precedence than =, which causes the expression to get converted to:
if($x = (1 && ($x == 1)))
So far so good, but now consider:
$x=1; if($x == 1 && $x = 2)
This doesn't throw error. Why doesn't it get converted to:
$x=1; if( (($x == 1) && $x) = 2 )
I've been told thats due to = being right assosiative, but https://www.php.net/manual/en/language.operators.precedence.php says When operators have equal precedence their associativity decides how the operators are grouped.. Here we have =, && and == all being of different precedence.
P.S; My actual code is if($result != false && $res = $stmt->get_result()), which has been copied from some other reputable source, so seems like not using unneeded parenthesis is common in php.
I've played with several conditions and below is what I've got.
First, let's consider that we init $x before if statements to avoid undefined variable notice.
Second, let's confirm the precedence for operators:
== is applied 1st
&& is applied 2nd
= is applied 3rd
This returns true:
$x = 1;
if ($x = 1 && $x == 1) {
echo 'true';
} else {
echo 'false';
}
It goes like ($x = (1 && ($x == 1))) -> ($x = (1 && true)) -> ($x = true) -> true.
If we compare $x to another value than the assigned one we will get false:
$x = 1;
if ($x = 2 && $x == 2) {
echo 'true';
} else {
echo 'false';
}
It goes like ($x = (2 && ($x == 2))) -> ($x = (2 && false)) -> ($x = false) -> false.
The last one returns true:
$x = 1;
if ($x == 1 && $x = 2) {
echo 'true';
} else {
echo 'false';
}
It goes like ((($x == 1) && $x) = 2) -> ((true && $x) = 2) -> (true = 2) -> true.
The last comparison can't be interpreted by PHP so it's an approximate view.
It looks like the last action (true = 2) totally depends on the left operand. If we put $x = 2; we will get (false = 2) -> false.
I'm not sure about the last one and here is the only place were some mistakes can happen.
Otherwise, it looks like precedence works as expected.
Anyway, I always put parenthesis for an assignment action inside if operator (especially inside ternary if) to be sure that I will get what I expect.
I don't think this affects performance or readability too much, but it may prevent some logical errors.
UPDATE:
Concering your code if($result != false && $res = $stmt->get_result()) it's not correct to compare it to if($x == 1 && $x = 2) because in your code are two different variables.
In this case logical operator will not call the second part at all if the fisrt one is false, see the 1st example here
UPDATE-2:
After the discussion under this answer we can see that the last conditions ($x == 1 && $x = 2) work like like (($x == 1) && ($x = 2)) -> ((1 == 1) && 2) -> true and $x becomes 2 after it.
Disclaimer: I'm new to programming, this is a simple exercise I'm doing so go easy on me.
The following code is not reaching (printing) the else statement. Any insight is much appreciated.
$number = '1120011';
// Decimal check
if($number[0] == '+' && $number[1] == '0'){
echo "Error: Can't assign a plus sign on a leading zero!";
// Leading zero check
} elseif($number[0] == '+' || $number[0] == '-'){
echo "Number is decimal";
// Binary check
} elseif (preg_match('~^[01]+$~', $number)) {
echo "Number is binary";
// Code not executing here. For e.g. $number = 1120011
} else {
"Number is non binary";
}
Another question:
Why is the following 'if' not working properly (if I replace it in the code above). I guess it has something to do with bad usage of operators.
if($number[0] == '+' || $number[0] == '-' && $number[1] == '0')
Thanks in advance! :)
For the second question:
if($number[0] == '+' || $number[0] == '-' && $number[1] == '0')
try
if(($number[0] == '+')||(($number[0] == '-')&&($number[1] == '0')))
I am stuck on some php coding, that seemed initially easy to accomplish. Here is what i would like to do :
<?php
$amountOfDigits = 1;
$numbers = range(1,3);
shuffle($numbers);
for($i = 0;$i < $amountOfDigits;$i++)
$digits .= $numbers[$i];
while ( have_posts() ) : the_post();
static $count = 0;
if ($digits == '1') {
//Do this if statement
if ($count == "2") }
elseif ($digits == '2') {
//Do this if statement
if ($count == "2" || $count == "3") }
elseif ($digits == '3') {
//Do this if statement
if ($count == "2" || $count == "3" || $count == "4") }
{ //here the rest of the code
?>
So depending on the $digits variable, the if statement is formatted to be used on the line above //the rest of the code
How to put this in PHP properly?
Thanks.
Robbert
If I understand your question properly, you want something like this:
if ($digits == '1')
$mycond = ($count == "2");
elseif ($digits == '2')
$mycond = ($count == "2" || $count == "3")
elseif ($digits == '3')
$mycond = ($count == "2" || $count == "3" || $count == "4")
then you can further use
if($mycond){
// blahblahblah
}
Well if you want the same execution block on each case, there is a very simple solution for that.
You should simply use a function that check status of "count" depending on "digits".
<?php
function checkCountAgainstDigits($count, $digits){
switch($digits){
case 1:
return $count === 1;
case 2:
return $count === 2 || $count === 3;
case 3:
return $count === 2 || $count === 3 || $count === 4;
default:
// The default case "ELSE"
return FALSE;
}
}
if(checkCountAgainstDigits($count, $digits)){
// do
}
?>
If you want a different one, your solution is correct.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I set one variable to true based on other conditions. Here, instead of doing
if ($vara && $varb && $varc)
I'm something like below. Problem is, I'm just not getting something right. Can you please help me?
<?php
$onward = false;
$vara = 11;
$varb = 21;
$varc = 3;
if ($vara == 1)
{$onward = true;}else{$onward = false;}
if ($varb == 2)
{$onward = true;}else{$onward = false;}
if ($varc == 3)
{$onward = true;}else{$onward = false;}
if ($onward)
{
echo "Ok";
}else {echo "Not ok";}
?>
Each of your conditions ignores the result of the previous condition. You need to include the previous state of $onward in each subsequent test:
if ($vara == 1)
{$onward = true;}else{$onward = false;}
if ($varb == 2 && $onward)
{$onward = true;}else{$onward = false;}
if ($varc == 3 && $onward)
{$onward = true;}else{$onward = false;}
This way, varb and varc are only tested if $onward is still true after the previous test.
This is a particularly ugly way of writing code. If you have three large conditions and you don't simply want to join them on one line as in your $vara && $varb && $varc, you should be writing it this way:
$onward = ($vara == 1)
$onward = $onward && ($varb == 2);
$onward = $onward && ($varb == 3);
Any time you're simply returning/setting something to true/false in the branches if your if statement, you should just be returning/setting the condition itself.
That is, this:
if (condition) {
return true;
} else {
return false;
}
should always be written:
return condition;
The problem is, you are overwriting the value of $onward in every if-statement. Just use
$onward = $vara == 1 && $varb == 2 && $varc == 3;
I think you are looking for:
$onward = $vara == 1 || $varb == 2 || $varc ==3;
or
$onward = $vara == 1 && $varb == 2 && $varc == 3;
depending on your goal.
With this:
if ($varc == 3)
{$onward = true;}else{$onward = false;}
$onward will always be false if $varc is not 3.
I'm sure you mean
if (($vara == 1) && ($varb == 2) && ($varc == 3))
{$onward = true;}else{$onward = false;}
or even
$onward = (($vara == 1) && ($varb == 2) && ($varc == 3));
A better way of doing this:
if ($vara == 1) {$onward = true;} else {$onward = false;}
is this:
$onward = ($vara == 1);
You're overwriting the same variable each time, so the first two conditions are actually pointless.
I don't know why you'd want to do this over the first, more succinct approach, which will have the same logical effect:
if ($varc ==== 1 || $varc === 2 || $varc === 3)
$onward = true;
else
$onward = false;
Or even just:
$onward = $varc === 1 || $varc === 2 || $varc === 3;
Beware also of ever doing == 1. In comparisons, data are coerced to their truthy/falsy equivalents, so any truthy value will resolve to true in the comparison == 1. To test that something is literally 1, use ===. (This is a good rule generally unless you know you explicitly want to test for value and not type.)
I need a little bit of help, being a newb with php.
if($catname != 'used-cars' && $currentpage IS NOT 1,2,3.....100){
}
How can I write this corectly?
Maybe put the numbers inside an array?
Ty
Use ! and in_array()
$array = array(1, 2, 3... , 100);
if($catname != 'used-cars' && !in_array($currentpage, $array)){
}
http://php.net/manual/en/function.in-array.php
if($catname != 'used-cars' && !in_array($currentpage, range(1, 100))
Or:
if($catname != 'used-cars' && ($currentpage < 1 || $currentpage > 100))
if($catname != 'used-cars' && !in_array($currentPage, array(1, 2, 3, ..., 100)))
{}
Assuming you want all numbers withing the range of 1-100, you can use in_array() and range() like so:
if (($catname != "used-cars") && (!in_array($currentPage, range(1,100))) {
//Do Stuff
}
Maybe something like this might work?
if($catname != 'used-cars' && $currentpage > 100)
{
//Code here
}