Category Archives: Web Development

PHP array_merge

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one, and returns the resulting array eg.

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

The above example will output:

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

Note "color" => "red" is overwritten by "color" => "green".

Reference

Using jQuery in WordPress

Loading the jQuery that comes with WordPress

Add this PHP code to header.php before wp_head()

<?php wp_enqueue_script( 'jquery' ); ?>

What not to do – Using a Google served jQuery instead

Add this PHP code to functions.php but make sure it’s the latest version.

<?php function my_scripts_method() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js');
    wp_enqueue_script( 'jquery' );
}    

add_action('wp_enqueue_scripts', 'my_scripts_method'); ?>

Adding your own jQuery

Wrap your jQuery in the following to use the default jQuery shortcut of $ instead of the jQuery that Wordpess uses to avoid conflicts. Note this must go after wp_head() and you may need to use type="text/javascript" in the script tags if you’re not using HTML5.

<script>
    jQuery(document).ready(function($) {
        $('#site-title').addClass('Testing');
    });	
</script>

$() will work as an alias for jQuery() inside the function above.

PHP continue

continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

A basic example that prints “13”, skipping over 2.

$arr = array( 1, 2, 3 );
foreach( $arr as $number ) {
    if( $number == 2 ) {
        continue;
    }
    echo $number;
}

continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. The default value is 1, thus skipping to the end of the current loop.

This code prints “outer, middle, inner. outer, middle, inner. outer, middle, inner. ”

$i = 0;
while ($i++ < 3) {
    echo "outer, ";
    while (1) {
        echo "middle, ";
        while (1) {
            echo "inner. ";
            continue 3;
        }
        echo "This never gets output.";
    }
    echo "Neither does this.";
}

Reference