Fri, 17 May 2024


Logical Operators In PHP

By: Anton, NetArt Media
Wed, 1 August 2018
Logical Operators In PHP

Understanding Basic Logical Operators


We will start off with the basic 'and' operator, which can also be configured using '&&'. I personally use '&&', but this is out of sheer preference. This being said though, older versions of PHP and certain server configurations may prefer one over the other, so make sure you experiment with both to find out.

The result for this code block on my development server is...

Both values are true.

Now we can use the 'and' operator during the processing of information passed through an HTML form.


Because I don't actually have a form set up for this above code and have taken steps to define the '$brand' variable as nothing before the conditional statement re-defines it, below is the result that is returned.

Sorry, we don't have that brand in stock at the moment. Request a special order here.

Next, we take a look at the 'or' operator. This is defined using two pipes, as so - ||. No doubt you have seen this operator before, or even used it yourself and didn't really know what it does. Here are a few examples of it below. One is basic, the other being used within an 'if(isset)' statement that could validate information from an HTML form.


The result being...

Yes, we have Stratocasters and Telecasters in stock.


The above code block checks to see if either the 'brand' or a 'model' of a guitar has been set from the form. If so, a representative message is returned. If not, we see this message...

Sorry, we don't have that brand or model of guitar in stock at the moment. Request a special order here.

Now, of course a person would want to expand on this code, perhaps defining particular messages according to whatever has been set. This is something you can try on your own for some testing.

PHP logical operators are quite self-explanatory and very useful for scripting. Learning how to use them properly is paramount to becoming a decent PHP coder. There's no need for me to go into greater detail about them here, as there are plenty of articles here at PHP-Scripting.com with several examples and scenarios. And many more to come in the future. :-)


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox