How to make a PHP “IF OR” or “IF AND” statement

H

Both WordPress and Statamic content management systems are built on PHP. Just like many other programming languages, PHP’s syntax for OR is two vertical lines, || ; PHP’s syntax for AND is two ampersands &&. Alternatively, the words “and” and “or” may be used, too.

IF OR in PHP

The following PHP code used in a WordPress template reads as: “If the phone field is empty OR the claims field is empty, then output the <p> tag reading “Contact us directly.”

<?php if( empty( $phone)  ||  empty( $claims ) ) {
echo '<p>Contact us directly.</p>' ;} ?>

#alternative syntax for OR
<?php if( empty( $phone)  or empty( $claims ) ) {
echo '<p>Contact us directly.</p>' ;} ?>

IF AND in PHP

The following PHP code used in a WordPress template reads as: “If the phone field AND claims field are empty then output the <p> tag ready “Contact us directly.”

<?php if( empty( $phone)  &&  empty( $claims ) ) {
echo '<p>Contact us directly.</p>' ;} ?>

#alternative syntax for AND
<?php if( empty( $phone) and empty( $claims ) ) {
echo '<p>Contact us directly.</p>' ;} ?>

If this code snippet helped you, please let me know in the comments. You may also enjoy our other WordPress, PHP, coding logic, and Statamic articles.

Happy Coding!

About the author

Kelly Barkhurst

Designer to Fullstack is my place to geek out and share tech solutions from my day-to-day as a graphic designer, programmer, and business owner (portfolio). I also write on Arts and Bricks, a parenting blog and decal shop that embraces my family’s love of Art and LEGO bricks!

Add comment

Recent Posts

Archives

Categories