Functions in PHP
Most of the PHP you see in your WP templates will be neat little functions. I’m not going to talk about how to create a function here; if you want to do that, you should check out a more advanced tutorial. But I will talk about how to use existing functions.
There are two kinds of functions. There are functions that RETURN a value, and there are functions that PRINT (or echo) a value. The distinction is crucial. Let’s talk about each briefly.
You can use functions that return a value in conditional statements. For example, is_home() is a function that returns TRUE if you are on your blog’s main page, FALSE otherwise. If you want your template’s header.php file to do something differently on your blog’s main page, but not elsewhere, you would use this code:
<?php if (is_home()){ ?>
... html for the main page only ...
<?php } // end of the if ?>Note that is_home() by itself won’t cause anything to appear on your site. If I inserted this code into my theme files, it would not cause any noticeable difference in the HTML output:
<?php is_home(); ?>That’s because is_home() doesn’t print any HTML, it just returns a value.
By contrast, let’s look at a function that does the opposite–that is, a function that echoes but does not return anything. WP’s the_content() prints HTML, but it does not return a value. You already know that placing <?php the_content(); ?> into a theme file causes the post (or page) content to appear. However, it would be meaningless to try to use the_content() in a conditional statement. The conditional statement shown below will always evaluate to false, since the_content() has no return value:
<?php if ("Hello world!" == the_content() ){ ... } ?>Fortunately, many of WordPress’s template “tags”
(which are better called “functions,” not “tags”) have two varieties–one that returns, one that echoes. The preceding conditional statement would work as expected if we used get_the_content() (which returns) instead of the_content() (which echoes). See the difference?
For more examples, compare WordPress’s bloginfo
with get_bloginfo
.
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