If you have a small server for your own use (such as your personal page) or have a busy server servicing a lot of php requests you might find it a good idea to install some goodies in order to optimize the performance of your machine. A really good start might be installing the eAccelerator.
The eAccelerator extension caches compiled PHP scripts both in shared memory and on disk. By this it reduces the time needed for a server to service a php request for a page that has been already cached.
The latest version of eAccelerator is completely compatible with PHP 5.1 and PHP 5.2, so if you run Ubuntu 7.10 you should not experience any difficulties to install the php accelerator. Before you actually start with the installation you will need to install some packages in advance which are requested by the eAccelerator. This can be really accomplished by the following commands:
If you liked this post, buy me a beer. ($3 for a beer or $7.5 for a pitcher)

If you have ever used GMail you have most probably already seen this at the attachments page, where you can dynamically add attachment slots without even refreshing the page. You can achieve this effect easily and I will just show you how to do it.
For the purpose of course, we will use JavaScript, so you should have JavaScript enabled in your browser in order to use this functionality.
If you liked this post, buy me a beer. ($3 for a beer or $7.5 for a pitcher)

This one puzzled me for some time… may be I’ve overlooked the php documentation, but I couldn’t find how to make use of the standard checkbox and return the selected values as an array from a simple form.
In the end, it turned to be easy. Consider that we have the following form:
<html>
<form action=’somewhere.php’ name=’form1′ action=’POST’>
John <input type=’checkbox’ name=’user’ value=’john’><br>
Sam <input type=’checkbox’ name=’user’ value=’sam’><br>
Marry <input type=’checkbox’ name=’user’ value=’marry’><br>
<input type=’submit’ value=’Delete User’>
You might expect that by submitting this form to the somewhere.php script, the POST variable would be user and the values would be stored in an array. That’s however not happening because what the browser is doing is sending the following as POST (I used FireFox and LiveHeaders plugin to track that down):
user=john, user=’sam’, user=’marry’ …
Great! That looks great and I would too expect to see an array, but that’s not what happens actually. Once submitted the above form will just pass: user=’marry’ and nothing else, regardless if you have checked Sam and John or not. It seems that each time user is set it overrides the previous information.
A quick glance at the php arrays and we have a solution. PHP as well as other languages uses special brackets ([] <- those one) to indicate that a variable is an array. So to achieve our goal we just need to rename the initial form variable from user to user[]. Just like that:
<input type=’checkbox’ name=’user[]‘ value=’john’>
… same for other users ….
If you submit the form now, you will see only one variable in the $_POST array called $user which is actually an array containing the selected users.
Nothing fancy, but as I said in the beginning it puzzled me for some time.
If you liked this post, buy me a beer. ($3 for a beer or $7.5 for a pitcher)

In Perl, the chop function removes the last character in a string, but in PHP it removes trailing white spaces only. To get the same functionality into PHP you can use the substr_replace() function. For example:
$string = substr_replace($string,”",-1);
will remove the last character of $string. You can play with it and replace the last character with empty space if you need such a functionality in your script.
Since most of the visits on this page had the ‘Remove last character bash’ search engine keyword, I figured that people might need that section as well, so here it is. To remove the last character from a string/file using bash scripting you need to use the echo option and ‘pipe’ it to the variable itself. For example:
var=”My String with an additional a at the end. a”
var=$(echo ${var%\a})
Now, if you do echo $var you will see that it will display “My String with an additional a at the end.” without the additional a at the end
If you liked this post, buy me a beer. ($3 for a beer or $7.5 for a pitcher)

In some cases, you might want to get the current URL of the script that is being executed. I haven’t yet found a convenient one-line function that is built-in into PHP and thus I’ve wrote the following function to do it:
/**
* Returns the current URL of the script.
* Usage: $url = cur_page_url();
*
* @access public
* @param none
* @return string
*
*/
function cur_page_url(){
$url = 'http';
if ($_SERVER["HTTPS"] == “on”) {$url .= “s”;}
$url .= "://";
if ($_SERVER["SERVER_PORT"] != “80″) {
$url .= $_SERVER["SERVER_NAME"]. “:”. $_SERVER["SERVER_PORT"]. $_SERVER["REQUEST_URI"];
} else {
$url .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $url;
}
Copy and paste the above function in your php script and when you need to get the current page, simply call it just like that:
$url = cur_page_url();
That’s all
Pretty much simple.
If you liked this post, buy me a beer. ($3 for a beer or $7.5 for a pitcher)
