My current code is as follows:
if ( ( $status == 'active' ) ||
( $status == 'full' ) ) {
I need to also include an AND statement. So if $status is either full or active AND $position matches 'need photo' or 'completed' then it displays. How do I include an AND statement?
I tried the following but it didn't seem to work:
if ( ( $status == 'active' ) ||
( $status == 'full' ) &&
( $position == 'need photo' ) ||
( ( $position == 'completed' ) ) {
Any help? Thank you! :-) I'm fairly new to all of this. I tried Google but couldn't find a clear answer.
&& has higher precedence than || so the code you tried is the same as:
if ($status == 'active' || ($status == 'full' && $position == 'need photo') || $position == 'completed') {
Which in plain English means, if either status is active, or both status is full and position is need photo, or position is completed.
But you want:
if (($status == 'active' || $status == 'full') && ($position == 'need photo' || $position == 'completed')) {
Which means, if either status is active or status is full, and either position is need photo or position is completed.
According to the PHP documentation on operator precedence, AND takes precedence over OR, so you need to group the OR expressions with parentheses:
if ( ($status == 'active || $status == 'full) && ($position == 'need photo' || $position == 'completed') ) {
...
I think you are just missing some brackets. What you want is if ((A) && (B)), where A and B are complex expressions (an expression containing two sub-expressions).
In your case: A = ( $status == 'active' ) || ( $status == 'full' ) and B = ( $position == 'need photo' ) || ( $position == 'completed' )
So, try this:
if ( **(** ( $status == 'active' ) || ( $status == 'full' ) **)** && **(** ( $position == 'need photo' ) || ( $position == 'completed' ) **)** ) {
Related
So I have a working redirect that currently sends customers to my homepage if they select a different city from a drop-down select. This changes some custom user meta data and uses that to determine when a redirect should occur. Simple enough.
if ( is_user_logged_in() && $metalocation_display !== 'chicago' && strpos($url,'chicago') == true) {
header('Location: example.com');
}
if ( is_user_logged_in() && $metalocation_display == 'chicago' && strpos($url,'chicago') == false) {
header('Location: example.com/chicago/');
}
What I would like to achieve is, if the customer is on the shop page, the I can redirect them to the CURRENT page they are on (not the homepage), but simply replace the city name with strpos or something similar... basically call the current URL and only replace the name of the city, regardless of where in the URL that string is.
Something like:
if ( is_user_logged_in() && $metalocation_display !== 'chicago' && strpos($url,'chicago') == true) {
header('Location: example.com');
}
if ( is_user_logged_in() && $metalocation_display == 'chicago' && strpos($url,'chicago') == false) {
header('Location: GET CURRENT URL & REPLACE CURRENT CITY (using a list of cities) OR ADD 'CHICAGO' AFTER DOMAIN IF ONE DOESN'T EXIST ');
}
Any advice?
Something like this should work (remove type declarations if using < PHP 7+):
function get_current_url( array $cities, string $target_city ): string {
// Get protocol.
$url = ( $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://' );
// Get URL parts.
$port = $_SERVER['SERVER_PORT'];
$name = $_SERVER['SERVER_NAME'];
$uri = $_SERVER['REQUEST_URI'];
// Check all cities in the array and replace if found.
foreach ( $cities as $city ) {
$uri = str_replace( $city, $target_city, $uri );
}
// Build URL with matching port.
$url .= ( $port !== '80' ? $name . ':' . $port . $uri : $name . $uri );
// Return escaped URL.
return esc_url( $url );
}
And then in your code:
if ( is_user_logged_in() && $metalocation_display !== 'chicago' && strpos($url,'chicago') == true) {
header('Location: example.com');
}
// Fill array with possible cities.
$cities = array( 'london', 'new-york', );
if ( is_user_logged_in() && $metalocation_display == 'chicago' && strpos($url,'chicago') == false) {
header('Location: ' . get_current_url( $cities, 'chicago' ) );
}
Obviously be sure to prefix the function though.
I have two functions:
$shipmethod = $wpdb->get_results( "SELECT * FROM wpck_rg_lead_detail WHERE
lead_id = $user_id and field_number = 111" );
$shipmethodo=$shipmethod[0]->value;
and If statement:
if( isset( $entry['103'] )) {
return number_format (($entry['103'] * 0.9),0, ".", ",") ;
}
I need to add an else if statement with one given value for $shipmethodo, being this one false or not true, so for that I'm using: !$ = 'value', however this is not working:
else if( isset( $entry['103'] ) && !$shipmethodo = 'value') {
return number_format (($entry['103'] * 0.8),0, ".", ",") ;
}
What I need to get is that if $shipmethodo 'value' is different, then condition works.
How can I make it?
Thank you!
In your else if statement:
else if( isset( $entry['103'] ) && !$shipmethodo = 'value') {
It should actually be
else if( isset( $entry['103'] ) && $shipmethodo != 'value') {
!= means "does not equal".
Are these two PHP comparisons exactly the same? By that I mean, will the result be the same for both statements; with and without brackets?
Without brackets:
if ( $params['isAjax'] == '1' && $isEnabled == '1' ) {
...
}
And with brackets enclosing each statement:
if ( ($params['isAjax'] == '1') && ($isEnabled == '1') ) {
...
}
Additionally, which is the better method. Is one superior to another? Thanks.
They are exactly the same. Order of operations dictates that == evaluates before &&.
http://php.net/manual/en/language.operators.precedence.php
Yes they are logically equal
if ( (true) && (true) )
is the same as
if ( true && true )
grouping statements using () is effective when you want to group logical statements
if ( (true && true) || false ) //true
if ( (true && false) || false ) //false
if ( (true && false) || true ) //true
if ( (true || false) && true ) //true
if ( (true || false) && false ) //false
without grouping these statements would be
if ( true && true || false ) //true
if ( true && false || false ) //false
if ( true && false || true ) //true
if ( true || false && true ) //true
if ( true || false && false ) //true <-- different
Grouping statements make it more readable as well as removes unwanted results (i didnt know ( true || false && false ) was true).
I can't figure out why this script isn't working.
<?php
if (($_GET['p'] != 'index') &&
($_GET['p'] != 'reg') &&
($_GET['p'] != 'login') &&
($_GET['p'] != 'ad') &&
(!isset($_GET['p']))):
?>
<?php endif; ?>
I want to not display the error page if the $_GET is not set, which in my experience (!isset($_GET['p'])) should do.
If $_GET['p'] is not set, you can't check $_GET['p'] != 'index' and all the others. You'll have to check if it's set first:
<?php if(
! isset( $_GET['p'] ) ||
($_GET['p'] != 'index' &&
$_GET['p'] != 'reg' &&
$_GET['p'] != 'login' &&
$_GET['p'] != 'ad')
): ?>
A better solution would be to put all those values in an array, and check if $_GET['p'] is in the array:
<?php if(
! isset( $_GET['p'] ) ||
! in_array(
$_GET['p'],
array('index', 'reg', 'login', 'ad')
)
): ?>
EDIT:
Now that you provided some more info, here's what you should do:
if ( ! isset($_GET['p']) )
{
// We're at the index page, so don't display anything
}
else
{
if ( in_array( $_GET['p'], array('index', 'reg', 'login', 'ad') ) )
{
// Display your content window
}
else
{
// $_GET['p'] is not a valid value, display error
}
}
Your condition makes no sense. You're checking for 3 possible values of $_GET['p'] and then checking if $_GET['p'] is even set. Reverse your logic:
<?php
if(isset($_GET['p']))
{
// display error page
}
else
{
// do something else
}
You can check if $_GET['p'] is set by
if(isset($_GET['p']) {...}
If it's set and not empty then you can check for values that you need to check.
Try this:
<?php
$defaultvalue=0; // for example 0
$p=isset($_GET["p"]) ? $_GET["p"] : $defaultvalue;
if(($p != 'index') && ($p != 'reg') && ($p != 'login') && ($p != 'ad')):
?>
I am performing some validation on three different drop down menus. If all three variables are NULL, I am wanting to direct the user to the specific error. I am asking the visitor to choose one of the dropdowns, not all three. Here is my line of code that I cant see to get working:
if ( $dropdown1 == 'NULL', $dropdown2 == 'NULL', $dropdown3 == 'NULL' ) {
echo '<h1>Error Edited For the Sake of Brevity</h1>';
}
else
Apreciate any thoughts anyone could share with me.
Thanks Again,
--Matt
Well, you're probably getting all of the values from $_REQUEST/$_GET/$_POST (I'm using $_REQUEST below because it works for all), for that you'd use isset.
if( !isset( $_REQUEST[ 'dropdown1' ] ) &&
!isset( $_REQUEST[ 'dropdown2' ] ) &&
!isset( $_REQUEST[ 'dropdown3' ] ) )
{
// none of them are set.
}
If you're actually looking to see if the variables themselves are set to null, then you should use is_null
if( is_null( $dropdown1 ) &&
is_null( $dropdown2 ) &&
is_null( $dropdown3 ) )
{
// none of them are set.
}
Finally, if you have reason to believe that they will be the STRING value 'NULL' then you can use == (this is a comparably rare circumstance):
if( 'NULL' == $dropdown1 &&
'NULL' == $dropdown2 &&
'NULL' == $dropdown3 )
{
// none of them are set.
}
You can change any of the above to test to see if any of them are not set by using || (or) instead of && (and):
if( 'NULL' == $dropdown1 ||
'NULL' == $dropdown2 ||
'NULL' == $dropdown3 )
{
// one of them is not set.
}
I assume you're trying to do a boolean AND comparison, which in PHP is:
if ( $dropdown1 == 'NULL' && $dropdown2 == 'NULL' && $dropdown3 == 'NULL' )...
if ( $dropdown1 == 'NULL', $dropdown2 == 'NULL', $dropdown3 == 'NULL' ) {
should be
if ( $dropdown1 == 'NULL' || $dropdown2 == 'NULL' || $dropdown3 == 'NULL' ) {
In this case if any of the variables is null, the it will display your error message
You shouldn't be using commas to separate the conditions
if ( $dropdown1 == 'NULL' || $dropdown2 == 'NULL' || $dropdown3 == 'NULL' )
if you want to give error message when all of the dropdowns are null then you should use this condition
if($dropdown1 == 'NULL' && $dropdown2 == 'NULL' && $dropdown3 == 'NULL' ){
echo $error_msg;
}