9
Feb/10
2

PHP: Recursive Functions

A recursive function is a function that calls itself. This is useful for certain applications. This short tutorial will show an example of a recursive function in action.

Let's say we have the following array of categories. Normally this might be stored in a database, but we'll use an array here for simplicity.

/* Example category hierarchy:
    Tutorials
    - PHP
    -- OOP
    -- Tips
    - JavaScript
    -- Basics
    -- Frameworks
    --- jQuery
    --- MooTools
    News
    - PHP
    - Wordpress
*/
$cats = array();
$cats[1] = array('parent' => 0, 'title' => 'Tutorials');
  $cats[2] = array('parent' => 1, 'title' => 'PHP');
    $cats[3] = array('parent' => 2, 'title' => 'OOP');
    $cats[4] = array('parent' => 2, 'title' => 'Tips');
  $cats[5] = array('parent' => 1, 'title' => 'JavaScript');
    $cats[6] = array('parent' => 5, 'title' => 'Basics');
    $cats[7] = array('parent' => 5, 'title' => 'Frameworks');
      $cats[8] = array('parent' => 7, 'title' => 'jQuery');
      $cats[9] = array('parent' => 7, 'title' => 'MooTools');
$cats[10] = array('parent' => 0, 'title' => 'News');
  $cats[11] = array('parent' => 10, 'title' => 'PHP');
  $cats[12] = array('parent' => 10, 'title' => 'Wordpress');

In this case, a good application of a recursive function would be to display a breadcrumbs display of a particular category. In the example, we use the key 'parent' to identify the category that a subcategory belongs to, or 0 for the main categories.

 
2
Feb/10
0

Using a PHP Class to Store Configuration

In a comment on my post listing 5 Tips for Writing Cleaner PHP Code, some good arguments were made against using constants to store configuration variables. The main arguments is that it pollutes the global namespace, which can lead to collisions when implementing other code, and the way it handles typos. This article demonstrates some of the ways constants can fail, and shows an alternative.

So here is how to store these constants in a class to avoid these problems. This puts the constants in their own namespace and prevent mistakes later on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
 
// Declaring your config class constants
class Config {
    const DB_HOST     = 'localhost',
          DB_USER     = 'username',
          DB_PASS     = 'password',
          ANOTHER_VAR = true;
}
 
echo Config::DB_HOST; // outputs localhost
 
echo Config::USER; // PHP Fatal error
 
if(Config::ANOTHER_VAR) {
    // do something
}
 
?>

That's all there is to it. Now all your constants are consolidated under one namespace and any typos will result in a fatal error. You can name the class whatever you want to be unique and avoid any collisions.

 
19
Jan/10
1

Using SSH in PHP

This tutorial will show you how to use the SSH2 functions in PHP to execute commands over SSH. This requires the SSH2 PECL extension to be installed on your server (installation instructions). Keep in mind that as of this time, the extension is in a beta state, so stability is not guaranteed.

Sending commands is fairly straightforward. You just connect, authenticate, then execute commands. Authentication can be done using a password or public key. Executing commands is a little tricky since it returns a stream that you have to handle.

 
29
Dec/09
3

Snippet: Fetch Twitter Feed (With Retweets) in PHP

Here is a PHP that fetches a Twitter stream for a specified user. It also combines retweets with the regular updates. This puts them all in an array to make it easy to output them however you want.

This snippet uses APC to cache the data, but you can use any caching method such as memcached or database. It should be fairly easy to edit in your own caching.

 
22
Dec/09
0

TempServers Finally Out of Beta

After almost a year since being announced, TempServers is finally out of beta. The system has been stable for months now, and it is ready to go live. The price is set at just 99 cents per hour for all games.

No major changes have been made since the previous announcement, other than some back-end optimizations. However, some major improvements are planned for the future.

Those that provided feedback during the beta have been given 12 hours of credit. More opportunities for free credits will be made available in the near future, so look out for those!

 
15
Dec/09
1

PHP in the Shell

PHP may be most commonly used within a web server to produce web pages, but it is a powerful scripting engine by itself. PHP is an amazingly useful multipurpose tool when used from the command line. This post will show you how to use the PHP Command Line Interface (CLI). Some of the information here is Linux specific, but there are equivalents for Windows.

Accessing the PHP CLI

Obviously the first thing you need to know is how to run PHP from the command line. There are a number of ways to do this, and one way that I prefer.

The method I find easiest is to write your script as a shell script. This basically means taking a regular PHP script and adding a line to the beginning declaring the php binary to interpret the script. Here's an example:

#!/usr/bin/php
< ?php
echo 'Hello World';
?>

Assuming the file name 'test.php' you would make it executable with chmod +x test.php and run it simply with ./test.php. Note that the file does not need to end with .php to function.

Other options for accessing the PHP CLI are to pass scripts or code to php as parameters, pass code to php through standard input, or enter code manually in interactive mode.

File as parameter:

$ php test.php

Code as parameter:

$ php -r "echo 'Hello World';"

Code through standard input (generate_php_code outputs code):

$ generate_php_code | php

Interactive mode:

$ php -a
Interactive shell
 
php > echo 'Hello World';
Hello World
php >
 

Page optimized by WP Minify WordPress Plugin