Php if equals this or that

PHP: If a equals b or c or d [duplicate]

I want to see if «Company A» in column A who was in the Negotiation stage in Column B now exists as «Company A» in column C (a subset of Column A) that is now in a Closed Won stage in column D. And if you have many variables to compare against, you could probably use this: There’s one more way of doing it, that is in case you are willing to do a little more typing: or may be you could have a function that returns the object version of the array: and then: If you are fine with jQuery, you could use jQuery’s way: Solution 4:

PHP: If a equals b or c or d [duplicate]

Possible Duplicate:
Short hand to do something like: if($variable == 1 || $variable == “whatever” || $variable == ‘492’) .

the shortest way to describe this logic. I am thinking about something like

but that is not a valid code. Any suggestions?

That is valid code but not logically correct.

If you have lots of values then you could do something like this.

Читайте также:  Блоки по порядку css

That both are not same as || is boolean operator and will always return true or false . So in second statement, you are comparing if $a is true or false .

You can use in_array to compare if $a exists in array($b, $c, $d)

don’t know why You want to do some sort of thing, but You can put b,c,d in array and call in_array function to search for elements. Still I can’t understand why You want to short and simple code make short and not simple.

If statement — PHP: If a equals b or c or d, PHP: If a equals b or c or d [duplicate] Ask Question Asked 9 years, 11 months ago. Modified 9 years, 11 months ago. Viewed 12k times but You … Code sampleif( in_array($a, array($b,$c,$d)) )Feedback

If column A equals column B then return value in column C

I have a spreadsheet with different database cuts of data as it existed in a past stage and a current stage.

I want to see if «Company A» in column A who was in the Negotiation stage in Column B now exists as «Company A» in column C (a subset of Column A) that is now in a Closed Won stage in column D. If this is the case, I want to return a $ amount value in Column E.

Ignoring my specific scenario, the core logic I need is if column A equals column B then return the value in column C.

=IF(condition, value if true, value if false)

If column A equals column B then return value in column C, I want to see if «Company A» in column A who was in the Negotiation stage in Column B now exists as «Company A» in column C (a subset …

Checking if value equals either a or b

I’m trying to have the user be able to control when they want to exit the while loop, and also i was wondering on how to exit the program when done

 cout>userInput; if (userInput='y'|'Y') < cin.clear(); cin.ignore(); input(); userInput=0; >else < exit(0); >return 0; 

The expression userInput=’y’|’Y’ suffers from three fundamental problems and a compounding problem.

  1. ‘y’|’Y’ is not a logical OR operation. It is a bitwise OR operation.
  2. It does not compare the value of userInput against ‘y’ or ‘Y’ .
  3. It assigns the value of the subexpression ‘y’|’Y’ , which evaluates to the integral value 121 in a system that uses ASCII encoding, to userInput .
  4. As a consequence, the conditional of the if statement always evaluates to true .
if ( userInput == 'y' || userInput == 'Y' ) 

If A = B, then B = A Not Always True? Definition of, Xlea. 11 1. Add a comment. 1. ‘=’ does not implies an equivalence relationship. If a = b then it does not mean that b = a. This is only true when it is a …

Javascript: what’s the most elegant way to check whether A is equal to B or C?

I need to evaluate a variable and check if it’s equal to one of two other variables.

I do it like this: if (a == b || a == c)

I feel there’s got to be a better way to evaluate variables in this manner. Is there?

If you’re just comparing one value to 2 others, this is the most efficient and elegant solution. No need to change anything!

If you want to see whether one variable is equal to any of a larger collection of other variables you can do something like:

The indexOf method may not be available in older browsers so be sure to check for it! In legacy browsers you can use write your own method to search through the array or utilize any popular general-use library. most will have this functionality.

You can also use Array.prototype.includes (ES7).

.. And if you have many variables to compare against, you could probably use this:

if ([a,b,c,d,e,f,g,h].indexOf(yourValue) >= 0) 

There’s one more way of doing it, that is in case you are willing to do a little more typing:

or may be you could have a function that returns the object version of the array:

var wrap = function()< var obj = <>; for (var i=0; i; 
if (yourValue in wrap(a,b,c,d,e,f,g)) 

If you are fine with jQuery, you could use jQuery’s way:

var values = [b,c,d,e,f]; for (var i=0,n=values.length; i > 

C++ — Checking if value equals either a or b, 1 Answer. The expression userInput=’y’|’Y’ suffers from three fundamental problems and a compounding problem. ‘y’|’Y’ is not a logical OR …

Источник

PHP If a variable equals this or this

For example, use the following query: But for How to check whether a text variable is equal to an Array and based on your original code, the normal way will be to pass the user input data (I believe is $(‘#book_name’).val()) thru ajax to a PHP file to check whether this data is in the array , then return the result back (or do further processing) For the HTML For the PHP (checkdata.php) Solution 2: You can first get the list of books once, then write a Javascript array from which to search for the entered book name. PHP, on the server, gets the books and stores the list in a PHP array.

PHP If a variable equals this or this

I have this if statement in my PHP:

But i want it to be something like this:

if($_SESSION['usrName']!='test1' or 'user')

But i cant figure out how to do it in PHP code. I have tried this:

if($_SESSION['usrName']!='test1','user')
if(($_SESSION['usrName']!='test1')||($_SESSION['usrName']!='user'))

You have to replace the || with && . Because you only want to redirect when both conditions are true.

if(($_SESSION['usrName']!='test1') && ($_SESSION['usrName']!='user'))
if (!in_array($_SESSION['usrName'], array('test1', 'user'))

This checks if variable $_SESSION[‘usrName’] is not in list of strings to simplify additional allowed user.

As others have said, you need to be careful with your boolean logic — (NOT X) || (NOT Y) is equivalent to NOT (X AND Y) , whereas what you want is NOT (X OR Y) which is equivalent to (NOT X) AND (NOT Y) .

For this particular situation, there are also a couple of other options, although none as neat as the invalid syntaxes you tried.

First, there is in_array() , which is easy to read, but not very efficient if you use it a lot with long lists (for a simple case like this, it’s not worth worrying about performance, though):

$allowed_users = array('test1', 'user'); if ( ! in_array($_SESSION['usrName'], $allowed_users )

Or, you can build a hash with the usernames as keys ; this is more efficient as the list grows, because PHP can check for a key without looping through the whole list:

$allowed_users = array('test1' => true, 'user' => true); if ( ! array_key_exists($_SESSION['usrName'], $allowed_users) ) < . >// Or, if you don't mind PHP raising a few notices about accessing undefined keys if ( ! $allowed_user[ $_SESSION['usrName'] ] )

Finally, you can use a switch statement, with the labels falling through, and a default case acting as the «else»:

Which, if any, of these you choose to use will depend on how you expect the code to grow in future, but they’re useful tricks to know.

Your last attempt is almost correct.

if (var != something || var != something-else) 

. will always be true, because one of those conditions will always match. Even if it’s equal to one side, it won’t be equal to the other.

When you’re testing two negatives like that, you need to use AND ( && ) instead of OR ( || ).

if (($_SESSION['usrName']!='test1') && ($_SESSION['usrName']!='user')) 

This will match if it’s not equal to one, and also not equal to the other.

PHP: Check if variable exist but also if has a value equal to something, Now each required element at least has a default value. You can now use if($_GET[‘myvar’] == ‘something’) without worrying that the key isn’t set. Update. One

How to check if the text in a textbox matches a variable in PHP

I am trying to check to see if the text the user has entered in my textbox called ‘valid’ is equal to a variable containing a randomly generated 6 digit number.

$validkey = rand(100000,999999); 

My question is; in an ‘if’ statement how can I check to see if the user has entered the same numbers in the textbox as the $validkey variable? It would be embedded within an if that checks if a button called ‘next’ has been clicked. So when the button is clicked it will do the check.

My current code is as follows:

"> E-mail: 


'; > > > if (isset($_POST["next"])) < if ($_POST['valid'] == $validkey) < echo 'I apologise if I worded this question wrong or I have not provided enough information, I am a bit of a PHP newb.

Thanks for any help in advance.

Use the intval function to parse the input value, then compare it to your generated value.

Is this the sort of thing?

if isset($_REQUEST['next']) < // next has been pressed if($_REQUEST['textentry'] === $validkey) < // entered text identical to the valid key // handle correct entry >else < // handle incorrect entry >> else < // handle some other button being pressed >

I don't know if it help or I understood your question correctly

did you try if ( $_POST['valid'] === $validkey)<>

Or you could also use jquery Ajax request, No need to submit next button when the user will leave the textbox ajax request will verify, you could display the message based on that.

Try this code. This is very basic code. Make necessary changes you need, like adding form and all and use it for your need.

    

Hope this is what you were looking for.

How to check whether a text variable is equal to an Array, Usually, you need to get the user input then use the input in your SQL query when fetching data from database (client to server) "SELECT count

How to check whether a text variable is equal to an Array

My requirement is to check whether a text variable is equal or not to an mysql output array.

The mysql output array I have taken as follows,

$connect = mysqli_connect("localhost", "root", "", "newbooks"); $query = "SELECT book_name FROM takenbooks order by ID DESC"; $result = mysqli_query($connect, $query); while( $row = mysqli_fetch_assoc( $result)) < $avail_books[] = $row['book_name']; // Inside while loop >

Now I need to check whether user have entered any book from which included in above array.So I have implemented as below.

$(document).ready(function() < $('#insert_form').on("submit", function(event)< event.preventDefault(); $('#book_name').val()=$book_required; if(in_array($book_required,$avail_books)) < alert("Not Available"); >else< $.ajax(< url:"books.php", method:"POST", data:$('#insert_form').serialize(), beforeSend:function()< $('#insert').val("Inserting"); >, success:function(data) < $('#insert_form')[0].reset(); $('#add_data_Modal').modal('hide'); $('#employee_table').html(data); >>); > > > 

But this is not working. Can someone show where I have messed this?

There can be other ways to accomplish what you want.

For example, use the following query:

SELECT count(*) FROM takenbooks where book_name = ? 

But for How to check whether a text variable is equal to an Array and based on your original code, the normal way will be to pass the user input data (I believe is $('#book_name').val()) thru ajax to a PHP file to check whether this data is in the array , then return the result back (or do further processing)

  
 if(in_array($_POST["data1"],$avail_books)) < echo "Not Available"; >else < // Place insert query here echo "New Record inserted"; >> ?> 

You can first get the list of books once, then write a Javascript array from which to search for the entered book name. (This may not be practical if the list of books changes quite often, or the list is extremely long.)

 ?>    ; document.querySelector('#insert_form').addEventListener(function (evt) < evt.preventDefault(); let book_name = evt.target.book_name.value; let not_available = (-1 === avail_books.indexOf(book_name))? 'not': ''; document.querySelector('#available').innerHTML = book_name + " is " + not_available + " available."; >);   

PHP, on the server, gets the books and stores the list in a PHP array. And when writing out HTML and Javascript use PHP to write out a Javascript avail_books array containing the book names retrieved from the database.

Now the server can send the client the HTML/Javascript code for rendering. Once loaded in the browser, and if you "View Source", the Javascript code will look something like this:

const avail_books = ["To Kill a Mockingbird", "Animal Farm", "Atlas Shrugged"]; 

With that the user can check the list of books without having to send a query to the server with every inquiry. It's faster and uses less resources.

It might have some Syntax error but thats the basic concept of what you are trying to achieve. Someones enters text, script searches the database and returns the results.

   

Results:

prepare("SELECT ID, book_name FROM takenbooks WHERE book_name LIKE ? ORDER BY ID DESC;"); $stmt->bind_param("s", "%" + $_POST['book'] + "%"); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) < echo '

Book \"' . $row['book_name'] . '\" was found.

'; > > ?>

PHP variables look the same but are not equal (I'm confused), 5 Answers 5 ; echo gettype · $psusername ; var_dump · $psusername · var_dump ; $psusername · preg_replace · "/[[:^print:]]/" ; $psusername · trim · $

Источник

Оцените статью