Php advanced, mysql database, php examples, php reference, php operators.
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
- Conditional assignment operators
PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
PHP Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a variable.
The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.
PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
PHP Increment / Decrement Operators
The PHP increment operators are used to increment a variable's value.
The PHP decrement operators are used to decrement a variable's value.
PHP Logical Operators
The PHP logical operators are used to combine conditional statements.
PHP String Operators
PHP has two operators that are specially designed for strings.
PHP Array Operators
The PHP array operators are used to compare arrays.
PHP Conditional Assignment Operators
The PHP conditional assignment operators are used to set a value depending on conditions:
Test yourself with exercises.
Multiply 10 with 5 , and output the result.
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top references, top examples, get certified.
- Language Reference
The reason for the two different variations of "and" and "or" operators is that they operate at different precedences. (See Operator Precedence .)
Example #1 Logical operators illustrated
The above example will output something similar to:
User Contributed Notes 21 notes
- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- Labs The future of collective knowledge sharing
- About the company
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
If condition not working on operator 'equal' and working on assignment operator
Hello I have a strange problem in which my If statement is not working. I am doing a simple user registration and would like to check if the variable 'sendmail' is equal to 1, to send an e-mail if not to not send e-mail. I have shorten the mail function code for better understanding:
So the problem is that the condition in this case if ($POST['sendmail'] == 1) is not working, and it is not sending the e-mail. In case if I write it like this if ($POST['sendmail'] = 1) this way is working no matter what argument the variable sendmail have inside it, it is always sending the e-mail.
this is from where sendmail is coming
I've checked the post array and the variable sendmail is inside so this can't be the problem. Any help will be very welcome. Thank you.
- $POST['sendmail'] should be $_POST['sendmail'] . – gen_Eric Apr 16, 2014 at 16:12
- 2 What @RocketHazmat said. Enable error reporting to discover such errors easily. – Amal Murali Apr 16, 2014 at 16:13
- @AmalMurali Excellent point! (: – Skewled Apr 16, 2014 at 16:14
2 Answers 2
Doing assignment instead of comparison is not what you want.
is truthy because the value being assigned ( 1 ) is truthy. If you did if ($var = 0) instead it would never pass.
You should also consider using === . This works if you know the type of both variables. $_POST input is always strings, so:
- " Doing assignment instead of comparison is not what you want. " — Where in this code does he assign a value? – Amal Murali Apr 16, 2014 at 16:13
- Read his original question. He mentions changing the comparison for assignment. – Halcyon Apr 16, 2014 at 16:14
- okay I fixed the $_POST, if I use == it is sending every time the e-mails no mater what is choosen if I use === it is not sending anything never. – John Siniger Apr 16, 2014 at 16:24
- That makes no sense. Is the value of $_POST["sendmail"] "1" , "0" or something else? – Halcyon Apr 16, 2014 at 16:27
- this is the problem, this is the result of the post array in case If I choose no: ["sendmail"]=> string(1) "0" – John Siniger Apr 16, 2014 at 16:29
Sign up or log in, post as a guest.
Required, but never shown
Not the answer you're looking for? Browse other questions tagged php or ask your own question .
- The Overflow Blog
- What it’s like being a professional workplace bestie (Ep. 603)
- Journey to the cloud part I: Migrating Stack Overflow Teams to Azure
- Featured on Meta
- Moderation strike: Results of negotiations
- Our Design Vision for Stack Overflow and the Stack Exchange network
- Temporary policy: Generative AI (e.g., ChatGPT) is banned
- Discussions experiment launching on NLP Collective
- Call for volunteer reviewers for an updated search experience: OverflowAI Search
Hot Network Questions
- Damaged spokes behind the cassette
- Can you steal a complete color set from a player that uses a wild card with the deal breaker action card? - Monopoly Deal
- Can a 650 nm 250 mW diode laser remove the first layer of a PCB?
- Getting child to eat (preschooler)
- Has Arnold Schwarzenegger's accent ever been acknowledged in-universe in a movie?
- Prefix code generator
- Why do the Nordics have a low birth rate despite their government providing all parents with free childcare and parental leave?
- Feasibility of “invisible” orbital bombs
- Why are Hollywood studios permitted to collectively bargain with employees, and what are the limits of this apparent antitrust exemption?
- Why are my newlines turned into \Omega?
- Did "Joe the Plumber" perform plumbing work for money between when he left the Air Force and October 2008?
- The role of the Elements in the development of mathematics
- How to slow down while maintaining altitude
- Meaning of "How you get any work done is beyond me"
- Stretching left, "inside" and right delimiters
- Drawing a maths protractor
- Word for when a life event (like a vacation) is coming to an end and you feel an immense feeling of sadness due to not wanting to leave
- Did Einstein say "Do not worry about your difficulties in mathematics, I assure you that mine are greater"?
- Why did 1990s-2000s LCD all use 60 Hz refresh?
- What does the clown mean by saying that brothels in the cities "shall stand for seed" in the second scene of "Measure for Measure"?
- Why do people say 'topless' but not 'topful'?
- What do Americans say instead of “can’t be bothered”?
- How much monthly costs can I expect if I get a kitten or two?
- Prism: full spectrum including UV + IR
Code Translation Project
Don't lose in a world of programming languages
PHP - Not equal to: !=
- par1 - Any number
- par2 - Any number
- Result - Logical value Returns a true, if the first number is Not equal to the second, otherwise false.
- Loose and Strict Equality Checks in PHP
the Loose Equality Check in PHP
The loose not equal check in php, the strict equality check in php, the strict not equal check in php.
This article will explain how to perform loose and strict equality checks in PHP.
For the loose equality checks, you’ll use some built-in check operators like the equality operator == and the not equal operator != .
Also, for the strict equality checks, you’ll use the strict versions of these operators.
The loose equality check in PHP uses the double equality sign == and not the equal sign != sign. Both operators are considered loose.
That is because they do not consider the data type of the compare variables. As a result, one in integer form will equal one in a string format.
We have three checks all will return true. The first check compares zero in string format and as a number. With the equality operator, PHP will return true.
The equality operator considers the values and not the data type. False and zero will return true for the second equality check because false means zero in computing terms.
But here, if you need to consider the data types of false and zero, an equality check will not suffice.
The final check between NULL and false should return false since NULL means an absence of value.
While false means the value is there, it’s empty or zero-value. But, the equality check returns true.
Like loose equality check, loose not equal check does not consider the data type during comparison. As a result, you can get unexpected results.
Our next code example is based on the previous code example. But, here, we use the loose, not equal check to perform the comparison.
All checks will return false. This is because the loose, not equal operator considers the value and not the data types of the variables.
The following outputs are from the else block of each comparison:
As the name implies, the strict equality check is strict during comparisons. This means strict equality checks that the variables are equal in value and data types.
One string format will never equal one in number with a strict equality check. Also, Boolean false will not equal zero.
All checks will return the appropriate information about the variables during our next code block comparisons.
The strict, not equal check will return the correct results when the comparison data and variables are not equal .
When you run the next code block, you get the correct results about the comparisons.
Habdul Hazeez is a technical writer with amazing research skills. He can connect the dots, and make sense of data that are scattered across different media.
Related Article - PHP Operator
- PHP Spaceship Operator
- PHP Spread Operator
- Null Coalescing vs Elvis Operator in PHP
- Assignment by Reference Operator in PHP
- Question Mark Symbol in PHP
PHP Comparison Operators
Php tutorial index.
Use of PHP Comparison Operators is comparing two values (number or string).
Home » PHP Tutorial » PHP Comparison Operators
PHP Comparison Operators
Summary : in this tutorial, you will learn how to use PHP comparison operators to compare two values.
Introduction to PHP comparison operators
A comparison operator allows you to compare two values and returns true if the comparison is truthful and false otherwise.
The following table illustrates the comparison operators in PHP:
Equality Operator (==)
The equality returns true if both values are equal; otherwise, it returns false . The following example returns true because 10 is equal 10:
The following example returns false because 10 is not equal 20 :
The following example compares the number 20 with a string '20' , it also returns true .
If you want to compare two values with the consideration of type, you can use the identical operator ( === ).
Not equal to operator (!=, <>)
The not equal to (!=, <>) operator returns true if the lefthand value is not equal to the righthand value; otherwise, it returns false . For example:
Identical operator (===)
The identical operator returns true if both values are equal and have the same type; otherwise returns false .
The following example uses the identical operator to compare a string and a number. It returns false because these values have different types:
Not identical operator (!==)
The not identical operator (!==) returns true if the values are not equal or they do not have the same type; otherwise, it return false . For example:
Greater than (>)
The greater than return true if the lefthand value is greater than the righthand value; otherwise, it returns false :
Greater than or equal to (>=)
The greater than or equal to operator returns true if the lefthand value is greater than or equal to the righthand value; otherwise, it returns false. For example:
Less than (<)
The less than operator returns true if the lefthand value is less than the righthand value; otherwise, it returns false. For example:
Less than or equal to (<=)
If the lefthand value is less than or equal to the righthand value, the less than or equal to operator returns true; otherwise, it returns false. For example:
In this tutorial, you have learned how to use the PHP comparison operators to compare two values of the same or different types.