If statement does not recognize the operator ||

0

I have a problem I have the following code.

$model = new Crud();
$model->select = "a.idAplicacion, a.idPaciente,  p.nombres, p.apellidos,  a.peso";

$model->from = "aplicacion a JOIN paciente p ON a.idPaciente = p.idPaciente JOIN unidad u on p.Unidad = u.Unidad";

if ($_SESSION['Unidad']!=4):
    $model->condicion ="u.Unidad = ".$_SESSION['Unidad'];
endif;

$model->orderby = "a.idAplicacion DESC";
$filas = $model->verTodos();

The operation of the code is as follows: it makes a query to the database if the Unit is different from 4 it enters and shows only the patients of that Unit, otherwise it shows me all the patients, I want to modify the if so that between when the Unit is different from 4 and 3 then it modified the if but it does not work for me I think it is something wrong in the sentence

if (($_SESSION['Unidad']!=4) || ($_SESSION['Unidad']!=3)):
    $model->condicion ="u.Unidad = ".$_SESSION['Unidad'];
endif;
    
asked by isela rojo 15.11.2018 в 18:54
source

2 answers

1

The operator || means OR logical, the IF will enter when it is:

  • ! = 4 and! = 3 (True or True = True)
  • ! = 4 y == 3 (True or False = True)
  • == 4 and! = 3 (False or True = True)
  • == 4 y == 3 (False or False = False) This is the only case where you will not enter

so only enter when != 4 and != 3 simultaneously you should use the operator AND which is && instead of || .

I hope you understand, your if should be this way:

if (($_SESSION['Unidad']!=4) && ($_SESSION['Unidad']!=3)):
    $model->condicion ="u.Unidad = ".$_SESSION['Unidad'];
endif;
    
answered by 15.11.2018 / 19:03
source
0

The problem here is that you want me to do something (if) when both conditions are met, for this you must use the AND (& amp;). It is worth mentioning that the logical operators work like this:

  • AND (& amp;): If at least one condition is FALSE, it will return FALSE.
  • OR (||): If at least one condition is TRUE, it will return TRUE.
  • NOT (!): Inverts the condition (If it is TRUE it becomes FALSE and vice versa).

That is, your code should be like this:

 //  Si es diferente a 4      Y  Si es diferente a 3
if (($_SESSION['Unidad']!=4) && ($_SESSION['Unidad']!=3)):
    $model->condicion ="u.Unidad = ".$_SESSION['Unidad'];
endif;
    
answered by 15.11.2018 в 19:21