Tag Archives: PHP

Checking the Status of a Minecraft Server with PHP

Have you ever needed to have a script check whether a Minecraft server was online, or retrieve its listing information, like the “Message of the Day” (server description) or the number of active players? Maybe you run your own server, and want to display the status on a community website. Or perhaps you have something more ambitious in mind.

After reading up on the protocol Minecraft uses for the in-game listing of your favorite servers, I put together a simple PHP class that makes it easy. Here’s a fancy demo that makes use of it.

The library, which you can download on GitHub, along with the aforementioned demo page, looks like this:

Continue reading →

If PHP Were British

I was browsing a popular social media site the other day, when I came across a link entitled “If PHP Were British.” I started laughing out loud (for real) just a few paragraphs in.

When Rasmus Lerdorf first put PHP together, he – quite sensibly, despite his heritage – chose not to write it in Greenlandic or Danish. Good job too – that would have been rather unpleasant to work with. He opted instead, being in Canada at the time, for the local tongue. No, not French – that bastard dialect of the Queen’s English commonly referred to as “US English”.

PHP developers in Britain have been grumpy about this ever since. What was he thinking? And more importantly, how do we undo this travesty?

The rest of the post is a series of suggested “improvements” to PHP, which give it a more “British” feel. I won’t spoil them for you, as a few of them a hilarious.

If PHP Were British [Added Bytes]

Getting Your Feet Wet with PDO and Migrating Old MySQL Code

You may have heard that the old MySQL extension for PHP is going to eventually be deprecated in favor of the newer (and potentially more secure) MySQLi and PDO extensions. You’re going to need to update your old code sooner or later, so why not make it sooner?

I’m going to recommend PDO over MySQLi simply because it’s available on more systems, and it’s syntax may be a little bit easier to learn for newbies. PDO has been bundled with the main PHP distribution since PHP 5.1, and has been in PECL even longer, while MySQLi has only been included since 5.3. Whichever you use is up to personal preference and project requirements of course, but I will be sticking with PDO for the duration of this tutorial.

Continue reading →

PHP to Deprecate MySQL Extension in Favor of MySQLi and PDO

The PHP project is working toward “softly” deprecating the mysql extension and its associated functions in favor of the newer (and more secure) mysqli and PDO extensions. They won’t be adding E_DEPRECATED errors for the extension in PHP 5.4, but will consider it in subsequent versions. Instead they will be focusing on education, marking it as deprecated in the documentation, adding new documents about the differences, as well as updating examples throughout to use the more modern functions.

The situation is certainly…interesting. There is a lot of code out there using the older database functions—to the point that I’m more surprised to see PDO or MySQLi in use—and nearly as many tutorials teaching questionable practices that enable injection attacks. It’s going to take quite a long time to move people toward the better extension, especially when you consider how long it took for web hosts to roll out PHP 5 when it’s backwards-compatibility issues were relatively minor.

Deprecating ext/mysql [news.php.net]

MacRabbit isn’t Dead!

Users of Espresso and CSSEdit have been complaining for some time about the lack of updates to the software. In a surprise announcement, MacRabbit (the developer) announced that Espresso 2 is on the way and that CSSEdit’s functionality is being rolled into it.

The long wait has grated both on our own nerves and those of our awesome users. But while we have kept quiet publicly about what we are working on, it is because privately we have been striving to transform our products into something new and even more awesome: Espresso 2. We are extremely excited to finally be able to show you what we have been working on, as this release will be of interest to both CSSEdit and Espresso users.

An early preview, the “kaboom” release, is already available for testing and upgrade path information has been posted.

The PHP Forking Has Begun

I previously mentioned my wish for an overhauled, more object-oriented PHP, a desire shared by many developers who work with PHP on a regular basis.

Well, the long journey toward such a thing might have just started. Someone has forked PHP, making quite a few syntax and performance improvements, and posted it on GitHub. Some of the improvements include:

  • Faster strlen() performance
  • A str_random() function to generate a random string of a specified length, optionally using a selection of valid characters to use.
  • A timechop() function that splits a timestamp into an array containing the various units.
  • A strcut() function that truncates a string to a specified length and optionally appends a specified character (e.g. “…”).
  • Short array syntax. e.g. $arr = [1, 2, [5 => "foo", 3.14159], 9].

I would like to see more of this, and for such enhancements to be pulled into the official PHP repository. (Also, I would like to see hosting providers keep current instead of trying to pass-off ancient versions of PHP…)

What if PHP were Completely Rewritten to be More Elegant?

PHP gets a lot of flak for some of it’s strange idiosyncrasies. It’s lack of consistency about argument order (needle then haystack in str_replace,  haystack then needle in strstr) and function names (stripslashes vs strip_tags) are particularly head-scratching examples. This partly comes from PHP’s roots; it’s basically a wrapper around existing C functions, and it pretty much mirrors them for convenience. (The MySQL functions are a good example of this.)

Now I like PHP. It’s not perfect, but it gets the job done. I like it’s C-style syntax, and it’s installed everywhere. Python and Ruby lack that kind of support, as well as having syntactical quirks that just bother me. (Python’s lack of braces is icky, and Ruby’s syntax, though interesting to study from a distance, is a bit too weird for me to wholeheartedly adopt.)

What if there was a way to fix PHP, bringing in some of the awesomeness of Python and Ruby while still retaining support for existing software? I’ve had this idea simmering for awhile that a major effort could be made to restructure the language.

I’m thinking of the “everything as an object” approach, like in Python and Ruby. Instead of strtoupper($mystring) you would have $mystring->upper(). Parameters would be better standardized, that namespace character would be replaced…

But what about backwards-compatibility? Here’s the clever part: the developers would leave dummy functions that would just return the value of the new, more object-oriented function. For example:

function strtoupper($a) {
    return $a->upper();
}

Having an alias to each of the old-style functions would make it possible to maintain support for legacy scripts, at least for a couple of major versions.

Of course, this would probably never happen. Unless someone were to fork PHP, which might create more problems than it would solve…

Add Submenus to the WordPress Admin Bar

Want to customize the Admin Bar, added in WordPress 3.1, to make it a bit more useful? Rather than removing it, like some people have sought to do, wouldn’t it make more sense to extend it with some additional submenus? If you head over  to Pro Blog Design, you can learn how to add links to the WordPress admin bar.

In this tutorial, we’ll be building a simple plugin that will let us add some handy links (e.g. a Twitter search for the current post, analytics, and ad sales), as well as remove the links we don’t want.

You can follow the tutorial, or just download the finished plugin and make some customizations to it.

Add Useful Links to WordPress Admin Bar [Pro Blog Design]

Variable-length Argument Lists in PHP with func_get_args()

Have you ever wanted to write a function in PHP that would accept a varying amount of arguments rather than requiring ones arbitrarily hard-coded? I can’t think of a reason why you would off the top of my head, but I know I’ve wished for such an ability before. There’s a PHP function called func_get_args() that will let you do that with minimal effort.

function myFunc() {

$args = func_get_args();

foreach ($args as $key => $value) {
echo "Argument {$key}: {$value} <br />";
}

}

You can call myFunc() with no arguments or like myFunc('hello world', 'lorem ipsum', 'dolor sit amet').

GluePHP — A Spartan URL Routing Framework

For larger PHP projects, it’s a good idea to use the Model-View-Controller pattern and URL routing to organize things a bit more logically and make the code easier to update in the future. Frameworks like CodeIgniter, Kohana and CakePHP do this, but they can be overkill for some projects.

Enter GluePHP.

GluePHP is a lightweight framework (really, it’s a single class!) that only does one thing: it routes URLs to PHP  classes. It “glues” (the developer’s pun, not mine) everything together by routing URLs like example.org/asdf to your class with the name “asdf.” By implementing GET() and POST() methods, you can fire off other methods and output results.

A simple example, that doesn’t use any of the more advanced features like regular expressions in URLs, would look like this (from the documentation):

require_once('glue.php');

$urls = array(
'/' => 'index'
);

class index {
function GET() {
echo "Hello, World!";
}
}

glue::stick($urls);

I would say it’s worth bookmarking if you do much PHP work. It would be great for throwing together quick websites or tech demos.