function

function

PHP Question: Pass By Reference

Tuesday, April 19, 2011 - 13:47

Question

Consider the following:

1
2
3
4
5
6
function doSomething(&$val) {
    $val++;
}
 
$a = 1;
doSomething($a);

What does the variable $a now equal?

Category: 

PHP Question: Scope Resolution

Wednesday, April 6, 2011 - 17:09

Question

What is printed after the following code has been run?

1
2
3
4
5
6
7
8
function calc() {
    $a = 1000;
    return $a;
}
 
$a = 1;
calc();
print $a;
Category: 

The Correct Way To Load A Template File In WordPress

Saturday, December 18, 2010 - 23:39

Since WordPress 3.0 there has been a funcion called get_template_part(), which has been used quite a bit in the new Twenty Ten default WordPress theme. This is an evolution of the usual way to include parts of themes by using functions like get_header() to include the header.php file. This function helps code resuse by including the same files multiple files, but also allows child themes to override parent themes.

Category: 

Get Child Pages List In Wordpress

Monday, May 10, 2010 - 22:58

It is possible to print out a list of pages in Wordpress, but these functions are designed to print from the root pages to a certain level. I often need to print out a list of pages that are children of the current page being looked at so I created the following function.

Category: 

Default Function Parameters In PHP

Tuesday, January 26, 2010 - 14:23

When creating functions in PHP it is possible to provide default parameters so that when a parameter is not passed to the function it is still available within the function with a pre-defined value. These default values can also be called optional parameters because they don't need to be passed to the function. I have seen this sort of code being used incorrectly quite often recently so I thought I would go over it in a post.

Creating a default parameter in a function is very simple and is quite like normal variable assignment. The following function has a single parameter that is set to 1 if it is not passed when calling the function.

1
2
3
4
function testFunction($a = 1)
{
    return $a;
}
Category: 

Wordpress Breadcrumbs

Sunday, December 20, 2009 - 00:38

I have been playing about with breadcrumb trails in Wordpress recently so I thought I would post the code here in case anyone else finds it useful. The basic idea is to try to find out where the current page is situated within the site. This means checking for pages and categories with their parents. Once we get down to the post level we need to try some things in order to find out where the current post is within the site.

Once the trail array has been filled with items it is looped through and converted into a string.

Category: