Conditional Statements in PHP
Templates and plugins make frequent use of a handful of statements in PHP. The difference between statements and functions is obscure, so I won’t try to explain any more than this: Statements are “special” functions that tell PHP to check a condition before proceeding or to repeat a block of code multiple times. The statements you’ll see the most are if, elseif, else, for, foreach, while.
If you write a plugin, you’ll probably need all these statements. But most WordPress users need only to know how to use the conditional statements, so that’s all I’ll write about for now.
if, elseif, else are used for conditional statements. These tell PHP to execute a block of code only if something is true. There are two “correct” ways to write these. In general:
<?php
// syntax #1: curly braces
if ( condition to check ){
// code to execute if the condition is true
}
// syntax #2: colon and endif
if ( condition to check ):
// code to execute if the condition is true
endif;
?>These two syntaxes can be expanded as necessary using else and elseif, as shown:
<?php
// syntax #1: curly braces
if ( first condition to check ){
// code to execute if the first condition is true
}elseif ( second condition to check ){
// code to execute if the second condition is true
}elseif ( third condition to check ){
// code to execute if the third condition is true
}else{
// code to execute if none of the conditions is ture
}
// syntax #2: colon and endif
if ( first condition to check ):
// code to execute if the first condition is true
elseif ( second condition to check ):
// code to execute if the second condition is true
else:
// code to execute if none of the conditions is ture
endif;
}
?>You can use as many elseif statements as necessary, or none at all. If you want to display different HTML depending on a conditional statement, you can simply close the PHP tags between if and elseif. This example uses WordPress’s is_home() and is_archive()
functions:
<?php if ( is_home() ): ?>
<h3>Main Page</h3>
<?php elseif( is_archive() ): ?>
<h3>Archives Page</h3>
<?php else: ?>
<h3>Welcome to my blog!!</h3>
<?php endif; ?>
9 comments »
Can this be used to display a different chunk of sidebar content depending on what page number you are on?
Yes, PHP can be used for that. See conditional tags.
I know I need to add the PHP file to the same directory as the page containing the PHP script but which FTP directory does Wordpress store the posts in pls?
@gadget: For general WP support, please use the WP support forums. The short answer: WP uses MySQL, not PHP, to store data.
Hello,
If the directory I am accessing from the site contains both index.php and index.htm ,What happens?
That’s not really a PHP question. It depends on your server configuration. But why ask? Just try it and see what happens.
Hello,
If I use single quote to assign a string to a variable in the PHP,Can I define another variable with a double quote? I mean, can I mix and match single and double quotes according to my convince in the same PHP snippet or should I stick to only one type of delimiters throughout the snippet?
Thanks a lot for the quick reply.
Hello,
Nice tutorial! simple,easy, neat, to the point.And the best part is it inspired me to learn a new web language.Thanks a lot
Thanks. Regarding your question about quotes, you can switch back and forth all you want. But again… why ask? Just try it and see what happens. That’s the way I learned PHP.
Leave a comment