Comparison and logical operators in PHP

When we state a condition that must be evaluated by a conditional, we know that it must be Boolean, that is, it can only be evaluated as true or false. We cannot put as a condition What time is it?, But if we can put as a condition it's five o'clock.

Comparison and logical operators in PHP | Learn PHP & MySQL | When we state a condition that must be evaluated by a conditional, we know that it must be Boolean, that is, it can only be evaluated as true or false. We cannot put as a condition What time is it?, But if we can put as a condition it's five o'clock

Comparison operators in PHP

When we state a condition that must be evaluated by a conditional, we know that it must be Boolean, that is, it can only be evaluated as "true" or "false". We cannot put as a condition "What time is it? ", But if we can put as a condition" it's five "(in PHP it would be something like: $ hour == 5).

A condition is an affirmation. And that statement, generally, takes the form of a comparison. Compare a variable, a known data, against an expected "probable" value, which is possibly the value that variable has taken. In this way, the condition is no more than a comparison between the real value that was given to a variable, and one of the "assumed" or "possible" values of that variable imagined by us, the programmers.

But to be able to compare in a truly useful and complete way, we must know the comparison operators that we have in PHP. So far, we have only used the simplest comparison operator, == (equal to), but, in many cases, we will need to state the condition not in terms of equality, but we will need to know if one number is greater than another, if it is less, if one text is different from another. Etc. So, depending on whether this condition is true or not, one or another code block is executed. Let's see, then, the list of possible comparison operators, so that we can use them when elaborating complex conditions:

Comparison operators Comparison operators PHP

Let's see some examples of these operators used in real conditions. If we want to compare if a value entered is less than a specific number, we have two ways: using the operator <or using the operator <=:


<?php
	if($_POST["age"] <18){
	print ("Es menor a 18 a&ntilde;os");
	}
	if($_POST["edad"] <=17){
	print ("Es menor a 18 a&ntilde;os");
	}
?>

If we want to know if something is different from something else, we also have two ways of comparing it: with the operator <> or with! =:


<?php
	if($_POST["nombre"] <> "Pepe"){
		print ("No es Pepe");
	}
	if($_POST["nombre"]!="Pepe"){
		print ("No es Pepe");
	}
?>

🤖

ChatGPT Free
Ask questions on any topic

CITE ARTICLE


For homework, research, thesis, books, magazines, blogs or academic articles

APA Format Reference:

Delgado, Hugo. (2019).
Comparison and logical operators in PHP.
Retrieved Dec 13, 2024, from
https://disenowebakus.net/en/php-comparison-operators

Participates!

Share it with your friends in Social Networks!

Professor at the University of Guadalajara

Hugo Delgado Desarrollador y Diseñador Web en Puerto Vallarta

Professional in Web Development and SEO Positioning for more than 10 continuous years.
We have more than 200 certificates and recognitions in the Academic and Professional trajectory, including diploma certificates certified by Google.

CONTINUE LEARNING

PREVIOUS


IT ALSO DESERVES TO PAY TO VISIT:

Not finding what you need?

Use our internal search to discover more information
Related content:

Would you like to learn more about Web Design?

Meet all the courses and tutorials that we have for you completely free
Learn Web Design
 

Leave your Comment

SPONSOR

Your business can also appear here. More information

Your browser has blocked advertising.
Please 🙏 Allows you to view the ads to access, thank you.