AND/OR not working in if/else statement [duplicate] - php

This question already has answers here:
PHP If statement always firing as true
(2 answers)
Closed 5 years ago.
I'm having issues with AND/OR in an if/else PHP statement. This is one of the codes in particular:
if(is_page_template('page-home.php') || ('zaadvokati.php')) {
$class_trans = 'class="trans-color standard-menu"';
}else{
$class_trans = 'class="not-page-home standard-menu"';
}
If it's like this - with || / OR , it loads every паге template with the first class_trans option and not just the 2 templates specified, and if you put AND instead of OR, then it loads just the first one ('page-home.php') with the first class_trans and doesn't load the second one ('zaadvokati.php') with it. I suppose that, for some reason, it doesn't recognize the second template, but I don't know why.
Can you help?
Thanks!

You need to reiterate the function in the condition. try this:
if(is_page_template('page-home.php') || is_page_template('zaadvokati.php')) {
$class_trans = 'class="trans-color standard-menu"';
}else{
$class_trans = 'class="not-page-home standard-menu"';
}

Related

Do-While loop not stopping on request [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I've been learning PHP for over a year now and it's always the little things that seem to go disastrously wrong, or I just forget what I'm doing.
I've been using Do-While loops in a few projects but recently one I created just didn't work. Once I numbed it down to almost nothing I noticed it just isn't stopping according to user input.
do {
echo "Hi there\n";
echo "Echo\n";
$userInput = readline();
} while ($userInput = 'continue');
echo "Exit";
I don't understand what's going wrong but something is. From my understanding the program will echo twice, listen for the user's input and loop through again while the user types continue - if not then will echo Exit. What am I doing wrong? This is such a simple task and it's annoying me. All the other topics I've searched for don't seem to be helping.
You need to use
while ($userInput == 'continue');
instead of
while ($userInput = 'continue');
because you have used wrong operator (= [assign] instead of == [equals])

How to check if ANY php parameter exists in url [duplicate]

This question already has answers here:
PHP check if url parameter exists
(6 answers)
PHP to check if a URL contains a query string
(4 answers)
Closed 5 years ago.
I am making a forum that accesses threads based off the category in the URL using the GET method. I want to redirect to an error page if no parameters exist in the url, but I want this to be a generic piece of code that can be used around my whole site.
For example:
The url would normally contain the category id:
localhost/myforum/threads.php?categoryid=1
I want it so that when the url is:
localhost/myforum/threads.php
it is to redirect to an error page, and that this piece of code is usable all around the website
The most reliable way is to check if the URL contains a question mark:
if (false !== strpos($_SERVER['REQUEST_URI'], '?')) {
// There is a query string (including cases when it's empty)
}
Try:
$gets = parse_url($url));
if($gets['query'] == "")
{
echo "No GET variables";
}
Just:
if (empty(array_diff($_GET, ['']))) {
header("Location: /path/to/error.php");
}
EDIT: Updated to remove empty values
You can use is_set to check if the parameter exists like this,
isset($_GET)

PHP if statement stops remainder of HTML page from loading [duplicate]

This question already has answers here:
How can I get useful error messages in PHP?
(41 answers)
Closed 7 years ago.
The following if statement does not seem to be working for me.
The two values in b1rafrica and b2rafrica are BIGINTs being pulled from a MySQL database (perhaps that might be something to do with it?).
When I run it with these values the site doesn't load:
b1rafrica = 200
b2rafrica = 150
If I remove the else then the site works...
I'm a total PHP noob so I apologise in advance if i'm doing something very stupid.
if ($b1rafrica >= $b2rafrica) {
$africastyle ="africab1";
}
} else {
$africastyle ="africab2";
}
Cheers,
Will
Please provice more info, like what errors you're getting. But try this:
if ($b1rafrica >= $b2rafrica) {
$africastyle ="africab1";
} else {
$africastyle ="africab2";
}

How to use || in function [duplicate]

This question already has answers here:
How do i make shortcuts for IF statements?
(5 answers)
Closed 8 years ago.
I have a function that checks user permissions and I want it to do this:
if(verify("O") || ("M"))
so that it checks if the user has the permission "O" or "M" but it does not work
You're close. This should work:
if(verify("O") || verify("M"))
If your function verify() only accepts one input. You need to call it again with the second input. Now, if either verify("O") or verify("M") returns true, your IF statement will return true.

conditional statements as strings [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
echo inside if loop
I trying to code a trading system and i have a list of entry and exit strategies. To lessen the number of lines in the code I planned to put all my strategies into an array for each entry and exit. My array is like this
$enter_strats = array(
array('name'=>"macd",'strat'=>"/$divergence[/$key]>0.1;"),
);
I am including the conditional statements inside the array as above.
while I am looping thru everyday prices, I have to check for each entry strategy if they they are true. My if statement is like this
foreach($divergence as $key=>$value)
{
if($trade ==0)
{
foreach($enter_strats as $k =>$v)
{
$strat = $v['strat'];
$strat = str_replace("#","$",$strat);
eval("\$strat = \"$strat\";");
if ($strat)
{
$trade =1;
$book->save($key,$close[$key],$v['name']);
}
}
}
}
The problem since it is a string its always if is always evaluating it to true. I tried to put one more eval inside if but its of no use.
Please help to solve this problem, its is very essential. Thanks a lot.
That's because you're trying to lessen the number of lines in the code.
Arrays intended to keep data, not code.
As soon as you understand it, your code will be okay.
'strat' should contain only data. An operator and a number for instance.
keeping variable name in the string makes no sense.
especially if you have this variable already.
You have already have $divergence[$key] in your code.
So, 'strat' should be just array('>',0.1)

Categories