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

Link to comment
Share on other sites

$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

 

}

 

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.