Jump to content

[SOLVED] Using && and || Operator in same line??


GregL83

Recommended Posts

Simple questions for the noob...I have an if statement where I want true to be returned dependent on three things.  The first must always be true and the second and third must have one of the two true...I am not sure how the operation is conducted...

if(isset($_POST['submit']) && $item = "width" or $item = "height") {
$update["$item"] = $_POST[$item]."px";
}

 

Thanks for the help..  ;D

Link to comment
https://forums.phpfreaks.com/topic/48711-solved-using-and-operator-in-same-line/
Share on other sites

You can use brackets to enforce the order of evaluation.  Another method is to create temporary variables to hold intermediate results, and then use those temporary variables in the "if".  Temporary variables often results in MUCH more readable code.

 

Example:

 

$submitted = isset($_POST['submit'];
$item_valid = ($item == "width" or $item == "height");
if ($submitted && $item_valid) {
}

 

Also note that you must use == or === when testing values, NEVER =.  "=" will set the variable to a new value.

$submitted = isset($_POST['submit']); * :)

 

You can also do this:

 

$submit = (isset($_POST['submit'])) ? $_POST['submit'] : NULL;

 

// If submit is set, then $submit is = $submit, else its NULL.

 

if($submit){

 

if($item == "width" OR $item == "height"){

 

# do this

 

}

 

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.