PHP if-Statement with multiple conditions - php

How would the valid syntax be for the following if-statement?
if ($properties->offering_type === 'y' || $properties->offering_type === 'p' && $properties->sold != 'y') {
// echo something
} else {
}
I want to echo something when offering_type is either y or p and sold is not y

&& has higher precedence than ||, so your condition is interpreted as
if ($properties->offering_type === 'y' ||
($properties->offering_type === 'p' && $properties->sold != 'y')) {
You need to add parentheses to group the || together.
if (($properties->offering_type === 'y' || $properties->offering_type === 'p')
&& $properties->sold != 'y') {

<?php
if ( ($properties->offering_type === 'y' || $properties->offering_type === 'p') && ($properties->sold != 'y') ) {
// echo something
}
else {
}
Please note that you are using === which means the type should also be the same. And you are not doing that for the sold property (!=).

Related

undefined index but i can't understand why

Here is my code
if (isset($_POST['error']) && $_POST['error'] != 2 && $_POST['error'] != 1) {
return true;
} else if (isset($_POST['error']) && $_POST['error'] == 2 || $_POST['error'] == 1) {
return false;
} else {
return false;
}
Please help.
Thanks.
When you do && it will evaluate all conditions until something is false. When you do || it will evaluate all conditions until something is true. Since your first conditions evaluated to the false, the 2nd one was invoked but $_POST['error'] didn't exist.
You probably want to do this, notice the brackets around your two errors.
if(
isset($_POST['error']) &&
(
$_POST['error'] == 2 ||
$_POST['error'] == 1
)
)
It can also be better re-written as.
if(
isset($_POST['error']) &&
in_array($_POST['error'], array(1,2))
)
Like Augwa said:
The && operator will evaluate all conditions until any one of them is false.
The || operator will evaluate all conditions until any one of the is true.
A solution:
if(isset($_POST['error'])) {
if($_POST['error'] != 2 && $_POST['error'] !=1) {
// Do stuff here
return true;
}else if($_POST['error'] == 2 || $_POST['error'] == 1) {
return false;
} else {
return false;
}
}
you should change your code as below... always enclose your comparison with || in brackets. because || condition checks up to last piece of code to find out a 'true' value. by re-coding as ..... && (... || .... ), the executions will return from the point && and will not execute ( .... || ..... ) part
if (isset($_POST['error']) && $_POST['error'] != 2 && $_POST['error'] != 1) {
return true;
} else if (isset($_POST['error']) && ($_POST['error'] == 2 || $_POST['error'] == 1)) {
return false;
} else {
return false;
}

is there a simpler way to write this so the latest entry shows and not previous entries?

if ($row['note_1'] !== '' && $row['note_2'] == '' && $row['note_3'] == '' && $row['note_4'] == '' && $row['note_5'] == '' && $row['note_6'] == '') {
echo $row['note_1_user'].":<br />";
echo $row['note_1'];
}
if ($row['note_1'] !== '' && $row['note_2'] !== '' && $row['note_3'] == '' && $row['note_4'] == '' && $row['note_5'] == '' && $row['note_6'] == '') {
echo $row['note_2_user'].":<br />";
echo $row['note_2'];
}
if ($row['note_1'] !== '' && $row['note_2'] !== '' && $row['note_3'] !== '' && $row['note_4'] == '' && $row['note_5'] == '' && $row['note_6'] == '') {
echo $row['note_3_user'].":<br />";
echo $row['note_3'];
}
if ($row['note_1'] !== '' && $row['note_2'] !== '' && $row['note_3'] !== '' && $row['note_4'] !== '' && $row['note_5'] == '' && $row['note_6'] == '') {
echo $row['note_4_user'].":<br />";
echo $row['note_4'];
}
if ($row['note_1'] !== '' && $row['note_2'] !== '' && $row['note_3'] !== '' && $row['note_4'] !== '' && $row['note_5'] !== '' && $row['note_6'] == '') {
echo $row['note_5_user'].":<br />";
echo $row['note_5'];
}
if ($row['note_1'] !== '' && $row['note_2'] !== '' && $row['note_3'] !== '' && $row['note_4'] !== '' && $row['note_5'] !== '' && $row['note_6'] !== '') {
echo $row['note_6_user'].":<br />";
echo $row['note_6'];
}
a table with 6 note fields and 6 note user field for a total of 12 fields dealing with notes. there are other fields, but the notes fields are 12.
in the add note page it's only possible to write to one set at a time. when going to the page the first time, they add note to field note_1 and the username automatically goes into note_1_user. then the second time going to the page, the note will go into note_2, and the user automatically goes into note_2_user. previous notes do not get over-written.
i have a page to view all notes, however, on a different page i just want the latest note to show up. i can't think of a better way to make that happen other than the way i posted above.
basically every if statement is the same. they all have the same 6 checks, checking if there is data in the fields, except the first one is checking if one of the 6 is not empty, then the second one checks if two are not empty, and so forth until the last one checks if all 6 are not empty.
this seems like too much code to do this effect.
you should really consider changing database structure, but for this case you can use:
$lastNoteUser = '';
$lastNoteData = '';
for ($i=1; $i<=6; $i++)
if ( ($row['note_'.$i] != '') && ($row['note_'.$i.'_user'] != '') )
{
$lastNoteUser = $row['note_'.$i.'_user'];
$lastNoteData = $row['note_'.$i];
}
if ($lastNoteUser && $lastNoteData)
echo $lastNoteUser.':<br>'.$lastNoteData;

php IF OR format?

This works
If ($flag != 'u') { stuff.. }
This also works.
If ($id != 0) { stuff.. }
But these don't seem to work for me....
If ( ($flag != 'u') || ($id != 0) ) { stuff.. }
If ( ($flag != 'u') or ($id != 0) ) { stuff.. }
If ($flag != 'u') || ($id != 0) { stuff.. }
If ($flag != 'u') or ($id != 0) { stuff.. }
If ( $flag != 'u' || $id != 0 ) { stuff.. }
If ( $flag != 'u' or $id != 0 ) { stuff.. }
Any idea why? and what format should I use for this in PHP?
Your code actually works. I tried this
<?php
$flag= u;
$id= 0;
if ( ($flag != 'u') || ($id != 0) )
{
echo "Hi";
}
?>
The code simple says if $flag not equal to u or if $id not equals 0 then echo hi in page.
Is that your logic?
The problem might be in your logic I hope!

PHP long if statement not working

I have this if statment here and its not working
if(
($division->id == 1 || $division->id == 2) &&
in_array('member1', $memberships) ||
in_array('member2', $memberships) ||
in_array('member3', $memberships) ||
in_array('member4', $memberships) ||
($division->id != 1 || $division->id != 2)
&& in_array('member5', $memberships))
{
return FALSE;
} else {
return TRUE;
}
What I am trying to do is say if $division is 1 or 2 and if member1, member2, member3, member4 are in the array $memberships return false, if $division is not 1 or 2 and member5 is in the array return false, everything else return true.
This is not working because member5 is in the array and $division is 1, which should return true, but it returns false.
PS - member1-5 are just names I am using for here as they actually are personal information in my array.
What Am i doing wrong?
I'd do this as a series of different 'if' statements, to avoid you going around the bend trying to work it out.
I've created this piece of code according to your statement...
What I am trying to do is say if $division is 1 or 2 and if member1,
member2, member3, member4 are in the array $memberships return false,
if $division is not 1 or 2 and member5 is in the array return false,
everything else return true.
I think I've got the logic right, I'm about to double check it:
if ($division->id == 1 || $division->id == 2)
{
if (in_array("member_2", $memberships) || in_array("member_3", $memberships) || in_array('member_3', $memberships) || in_array('member_4', $memberships))
{
return false;
}
else
{
return true;
}
}
else
{
if (in_array("member_5", $memberships))
{
return false;
}
else
{
return true;
}
}
These are the logical groupings, 1 per line. Use parenthesis to adjust how php evaluates it if this isnt what you want.
($division->id == 1 || $division->id == 2) && in_array('member1', $memberships)
|| in_array('member2', $memberships)
|| in_array('member3', $memberships)
|| in_array('member4', $memberships)
|| ($division->id != 1 || $division->id != 2) && in_array('member5', $memberships))
Try:
if(
(($division->id == 1 || $division->id == 2) &&
(in_array('member1', $memberships) ||
in_array('member2', $memberships) ||
in_array('member3', $memberships) ||
in_array('member4', $memberships))) ||
(($division->id != 1 || $division->id != 2)
&& in_array('member5', $memberships)))
{
return FALSE;
} else {
return TRUE;
}
Less lines of code does not always mean it's better, break it down and make it readable, for example:
$output = true;
if($division->id == 1 || $division->id == 2)
{
if(in_array("member_2", $memberships) || in_array("member_3", $memberships) || in_array('member_4', $memberships))
{
$output = false;
}
}
else
{
if (in_array("member_5", $memberships))
{
$output = false;
}
}
return $output;

Inverse conditions

I'm trying to rephrase the a conditional to drop a { .. } statement (to make the code less indented).
Currently I have:
while($something){
if((strcasecmp($str1, $str2) === 0)
|| (isset($arr[0]) && strcasecmp($str3, $str4) === 0)){
// a lot of code here...
break;
}
}
With the inversed IF condition it should look like:
while($something){
if((strcasecmp($str1, $str2) !== 0)
&& (empty($arr[0]) && strcasecmp($str3, $str4) !== 0))
continue;
// a lot of code here...
break;
}
But it doesn't work. My code and the break statement get executed when they shouldn't.
What am I doing wrong here.
Here
(empty($arr[0]) && strcasecmp($str3, $str4) !== 0))
The && must be ||.
I for myself would keep the first variant, because it's slightly more straight-forward.
Update: OK, as I thought about it the break; makes me wonder. You want to get rid of one intendation?
if ($something
&& ((strcasecmp($str1, $str2) === 0) || (isset($arr[0]) && strcasecmp($str3, $str4)) === 0)
){
// a lot of code here...
}
And know some micro-optimization :) (!strcasecmp() means, that they are equal, except maybe the case)
if ($something && (!strcasecmp($str1, $str2) || (isset($arr[0]) && !strcasecmp($str3, $str4))) {
// a lot of code here...
}
I hope the paranthesis matches.
This should work
while($something){
if((strcasecmp($str1, $str2) !== 0)
&& (!isset($arr[0]) || strcasecmp($str3, $str4) !== 0))
continue;
// a lot of code here...
break;
}

Categories