PHP Comparison With Or Without Brackets - php

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).

Related

PHP making difference between get with and without value

Good day,
I would like to know if there is any function for making the difference between this:
?param=
and this:
?param
Because I would like my script to detect if value is empty (first example), or if it's not given (second example).
I made a test with the following functions but I could not find what I want:
if( isset( $_GET['param'] ) ) {
echo '<div>isset</div>';
if( empty( $_GET['param'] ) ) {
echo '<div>empty</div>';
} else {
echo '<div>not empty</div>';
}
if( is_null( $_GET['param'] ) ) {
echo '<div>null</div>';
} else {
echo '<div>not null</div>';
}
if( defined( $_GET['param'] ) ) {
echo '<div>defined</div>';
} else {
echo '<div>undefined</div>';
}
} else {
echo '<div>not set</div>';
}
Any idea? Thank you in advance.
EDIT
Solution:
( strpos( '?'.$_SERVER['QUERY_STRING'], '?get=' ) !== false ) || ( strpos( '?'.$_SERVER['QUERY_STRING'], '&get=' ) !== false )
You can test with something like if( $_GET['param'] ) which will give true if Param is declared (even if it is an empty string) and false if not declared.

PHP 7.2 Warning count(): Parameter must be an array or an object that implements Countable

i've a problem with Wordpress. This is the error message: PHP 7.2 Warning count(): Parameter must be an array or an object that implements Countable /web/htdocs/www.firenzeflowershow.com/home/wp-content/themes/wpex-elegant/functions/meta/init.php on line 750
> elseif ( is_array( $meta_box['pages'] ) && count( $meta_box['pages']
> === 1 )) $type = is_string( end( $meta_box['pages'] ) ) ? end( $meta_box['pages'] ) : false;
The closing parenthesis of count is in the wrong position. You are actually passing a boolean to the function, because "$meta_box['pages'] === 1" will return true or false. Your code should be:
count($meta_box['pages']) === 1
Please try if this works:
elseif ( is_array( $meta_box['pages'] ) && count($meta_box['pages'])
>= 1 )
Try:
elseif ( is_array( $meta_box['pages'] ) && count( $meta_box['pages'] )
=== 1) $type = is_string( end( $meta_box['pages'] ) ) ? end( $meta_box['pages'] ) : false;
Your code did not work because === 1 was inside the count() function call: count($meta_box['pages'] === 1), and a comparison returns a bool. Here, I have changed it to count($meta_box['pages']) === 1 which gets the number of elements in the array, and checks if it returns 1.

If value not true or false then.. in PHP

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".

Very basic PHP - IF AND IF...?

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' ) **)** ) {

PHP Multiple Drop Down Variables Empty

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;
}

Categories