ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

Latest Blog Entries rss.png


Messing with the output buffer

Monday, November 28th, 2005

I love messing with the output buffer, doing weird things (and a lot is possible). Let's look at this really simple example first to see what I mean:


function mycallback ($buffer) {
        $buffer = 'BOOYAAH!';

        return $buffer;
}

ob_start('mycallback');

echo 'This will never appear';

?>

What the above code does is enable output buffering, with our own custom buffer handler, which replaces the buffer with a new string. A neat application of this would be post replacing. Imagine you could let your script output a lot of html, and later wanted to delete that html again. Without output buffering this wouldn't be possible at all, but even with output buffering you can't discard parts of the buffer, and you can only discard the whole buffer. What if we only want to replace part of the buffer?

With a custom buffer handler that's possible, and it's actually something I use in one of my scripts to combat a buffering bug in PHP. Consider the following code:

class Buffer {
        var $replacements = array();

        function replace ($replace, $with) {
                // Add to replacements array
                array_push ($this->replacements, array($replace, $with));
                return true;
        }

        function do_replace ($buffer) {
                // Do all replacements on the buffer
                foreach ($this->replacements as $r) {
                        $buffer = str_replace ($r['0'], $r['1'], $buffer);
                }

                return $buffer;
        }
}

$buffer = New Buffer;
ob_start(array(&$buffer, 'do_replace'));

echo 'My name is Henry';

// Do some other logic here
// and come to the conclusion that
// my name isn't Henry, but Dennis

$buffer->replace ('Henry', 'Dennis');

?>

Great isn't it, and in some cases it really makes it easier to do certain things. The above code is probably very "filthy", and should be cleaned up, but it's about the principle behind it. It's even possible to add new content before old content, e.g.

class Buffer {
        var $replacements = array();
        var $addbefore = array();

        function replace ($replace, $with) {
                // Add to replacements array
                array_push ($this->replacements, array($replace, $with));
                return true;
        }

        function add_before ($str) {
                array_push ($this->addbefore, $str);
                return true;
        }

        function do_replace ($buffer) {
                // Do all replacements on the buffer
                foreach ($this->replacements as $r) {
                        $buffer = str_replace ($r['0'], $r['1'], $buffer);
                }

                // Add new stuff before
                foreach ($this->addbefore as $a) {
                        $buffer = $a . $buffer;
                }

                return $buffer;
        }
}

$buffer = New Buffer;
ob_start(array(&$buffer, 'do_replace'));

echo 'My name is Henry';

// Do some other logic here
// and come to the conclusion that
// my name isn't Henry, but Dennis

$buffer->replace ('Henry', 'Dennis');

$buffer->add_before ('

The page now starts with this!

'
);

?>

And these are just simple examples of all the stuff that's possible. Messing with the output buffer is really a great thing, and I love doing it, just because you can do so much funky things with it. There are a few caveats though, and the biggest one is that it contains a few bugs here there (e.g. the one I linked above). Also, output buffering can make your website appear to be downloading slower. You should also watch out when using a custom output handler and ob_gzhandler together as it gives problems (I'll talk more about that in a future entry).

For now, start messing with that output buffer, and come up with creative applications. If you find anything good, don't forget to e-mail me, because I'm always interested!

PHPit Forums Launched

Saturday, November 12th, 2005

I went out and bought vBulletin a few days ago, and I'm happy to announce today that PHPit Forums is officially open. I hope everyone will join the forums, and talk about anything PHP related. Also, if you have any suggestions or comments about the forums, I'm happy to hear them. Just e-mail me or leave a comment on this entry.

I'm thinking about doing some kind of launch contest with prizes. Any suggestions? Maybe offer a few PHP books for posting xx amount of posts? Something like that maybe? I definitely want to do something to get lots of acitivity going fast. There's nothing worse than a bulletin board that's inactive, so that's something we need to prevent.

Yet another framework: Symfony

Thursday, November 10th, 2005

The Farm mentions a new PHP framework: Symfony, which is described as the Ruby on Rails for PHP. I haven't actually played with it myself, but I did have a quick look at the ten minutes screencast and a ajax tutorial.

It seems fairly simple to me, although it's a shame that it needs to be installed on your server. That'll stop most people, including me, from actually being to use it on a production website. I'm on shared hosting, so I won't be able to install the framework. The same thing goes for Ruby on Rails.

It does boast a very extensive list of features, including:

  • simple templating and helpers
  • cache management
  • multiple environments support
  • deployment management
  • scaffolding
  • smart URLs
  • multilingual and I18N support
  • object model and MVC separation
  • Ajax support

which makes it even more of a shame that I can't run it on my web host. Check out the live demo as well, where you can use an app created with Symfony. Seems pretty neat, doesn't it?

Use PHP to create Windows Apps

Monday, November 7th, 2005

Just came across WinBinder, via The Farm. What exactly is WinBinder? The ability to create native Windows applications, using PHP. Yeah, you heard me correctly, you can use PHP to create programs. How cool is that?

I haven't really played with it myself, and only browsed through the examples, but it seems you can create pretty much any type of windows application, using PHP. I might just create a 'PHP Notepad', just for the heck of it. Or create a PHP editor, with PHP, heh.

Surprisingly, the code seems rather simple as well. To create a window, all you need is:

include "../include/winbinder.php";
wb_create_window(NULL, PopupWindow, "Hello world!", 480, 320);
wb_main_loop();
                             
?>

That's all! In C++ you'd need more than that (though it does resemble the above somewhat).

Have a look at the examples for some more serious applications, and try it yourself!

Internationalising your PHP scripts?

Thursday, October 27th, 2005

Jim Plush has posted some excellent tips about language files and international PHP scripts on his blog. In his entry, which can be viewed by clicking here he gives nine pointers on what you should look out for when creating a PHP script that needs to support different languages. And it's a lot harder than I thought. Who knew that the Arial or Verdana font doesn't support Japanese characters? Or that German causes most text to be 40% longer? These are all things you must take into account.

Thankfully, Jim does provide a good workaround for all the problems, and he's even done some testing on the best way to store the text strings. This entry is definitely a must-read if you're looking to release your PHP script in multiple languages!

PHPit now running on WordPress

Sunday, October 23rd, 2005

I've ditched my old, custom written, Content Management Tool for PHPit, and decided to use WordPress. It's excellent for what PHPit needs, and it even supports multi-paged entries (this is necessary for the articles on PHPit, which are multiple pages).

It wasn't easy, and I had to hack it quite a lot, but the cool thing is that I've never actually touched the code of WordPress. I was able to use strictly plugins and templates to change it to my liking. This is really amazing, and shows how powerful WordPress is.

Anyway, with the update also comes a few new features. First of all, PHPit now has an integrated blog, where I will post stuff about PHP (news, information, tips, security alerts, etc) every couple days. Secondly, it's now possible to comment on all the content. If you want to say something about a specific article or code snippet, you are now able to. Just leave your comment, and I'm very happy with every comment I receive.

Thirdly, PHPit is now running a bit smoother as well. WordPress just gives it a better feel, I think. It feels good.

I'm thinking about wrapping up the update experience in a new article, giving tips on WordPress and what things you should watch out for. Also expect a new article to be added tomorrow.

If you come across any bugs or problems, feel free to contact me and I will fix it. Thanks!

Now THIS is some rapid development

Sunday, August 14th, 2005

I'm creating a new script for myself (a simple CMS for a new website), and I'm going extremely fast. It's just so much easier to develop something when you've got all kinds of tools at your disposal.

I'm of course using my PHP components which really speed things up. Creating forms and doing validation is just so easy now! I think that's really my best PHP:Component. I'm going write an introduction article later this week to introduce it to more people, 'cause I feel like more developers should use it. It's just so useful.

I'm really doing some fast development now. It may not be up to scratch when it comes to best coding standards (and the OOP purists will puke when they see my code), but it's working, and I'm going fast.

The thing is, I want to release, and do it as fast as possible. Personal projects don't have to be perfect; after all, the visitors won't see the code anyway. They want to see something working. And that's what I'm creating. A working script, in as little as time as possible.

PHP:DataGrid Examination Part 1

Saturday, August 13th, 2005

Today I've written and published An Extensive Examination of the PHP:DataGrid Component: Part 1.

The article looks at the, recently released, PHP:DataGrid component. The article only really scratches the surface, and it talks about what exactly PHP:DataGrid is, and how to use it.

I'm pretty thrilled about the DataGrid series. I think it'll be pretty popular, as it's very similar to the DataGrid Examination (of the ASP.NET DataGrid) by Scott Mitchell (from 4GuysFromRolla.com). That has 18 parts. Maybe my series will have 18 parts as well? :P

If only PHP5 was more wide spread

Tuesday, August 9th, 2005

I'm discovering a few really neat things about PHP5 today. Some of these things are really logical, and makes you wonder why it wasn't there in the first place, e.g. consider the following

get_obj ('my_obj')->do_something();

In PHP5 that works perfectly, but not in PHP4. No, in PHP4 you have to do this

$my_obj = get_obj ('my_obj');
$my_obj->do_something();

I'm doing exactly the same thing, except it took me two lines. Crazy!

Running PHP4 and PHP5 together

Tuesday, August 9th, 2005

I wanted to test my scripts in PHP5, but I didn't have access to any PHP5 dev machine, so I needed to install PHP5 on my own computer. However, I didn't want to install it over PHP4, because I need that as well (most web hosts still only support PHP4).

So I installed PHP5 along side with PHP4, using only one Apache. If you want to do this as well, then follow these instructions (they're easy).

  1. Download and install PHP5, and make sure you install PHP5 in a new directory, e.g. C:\PHP5
  2. Copy the php.ini-dist from the PHP5 directory to C:\Windows and rename it to php.ini. You can remove the previous php.ini that was there already.
  3. Next, you have to edit the http.conf file, and create a new virtual host, that looks like this:

    Listen 83

    ServerName localhost
    ServerAdmin me@localhost
    DirectoryIndex index.html index.php
    ErrorLog logs/error.log
    # http://httpd.apache.org/docs-2.1/mod/core.html.en#limit


    Order allow,deny
    Allow from all

    DocumentRoot "C:/www/project-a/"
    ScriptAlias /cgi-bin/ "C:/php5/"
    Action php5-script /cgi-bin/php-cgi.exe
    AddHandler php5-script .php .html

    I've high-lighted all the important bits.

  4. If everything went correctly, you can now go to http://localhost:83 and use PHP5. If you want PHP4, just go to http://localhost. That's all :)

It's working perfectly for me, and I've already uncovered a few (small) bugs in my scripts and components.