isset() vs empty() vs is_null()
Dec 27th
isset(), empty(), is_null() are three usefull php functions which are primary used in testing the value of a variable. They all return a boolean value. Distinguishing the difference among these three functions are quiet important as misusing either one might introduce bug in your application.
1. isset()
This function is mostly used to test incoming variables such as form input variables. It returns true only is the variable is not null.
2. empty()
empty() returns true if the variable is not empty, meaning not one of the following: “”, “0″,0,false,array(),null.
3. is_null()
is_null() returns true if the variable is null. is_null() is the opposite of isset()
Function Comparision
| function | return true if $var is |
|---|---|
| isset($var) | anything other than null |
| empty($var) | “0″, “”, false, 0, array(), null, unassigned |
| is_null($var) | null |
Walking Is Fun
Oct 26th
I would never consider walking a fun activity to do before I really got into it. Walking is fun. I recently became kind of addictive to walking. It’s a very relaxing activity. It keeps me active and allows me to free myself from my busyness. So just grab an mp3 player and get down to the street, start walking and get yourself moving.
PHP Error logging with htaccess
Aug 23rd
With Apache’s mod_rewrite rule, php error logging becomes extremely easy. The rules are simple but yet powerful. This enables you to log all errors generated during PHP runtime. It makes debugging a pleasure thing to do while it also hide the errors from showing on your live website.
Error Logging With Apache Mod_rewrite Rules:
Before writing the rules, we need the htaccess file on the root of your website.
Creating a htaccess file on any Windows server may need a little trick since the normal right-click->create file would not work.
- In Windows, just simply open notepad and then save as “.htaccess”(without quotation mark), then you have an empty htaccess file to work with.
Add the following rules to the htaccess file.
#Beginning of suppress rules #The following rules suppress PHP errors #These rules are ideal for using on live website, #However, you may not want to suppress any errors on your development site. php_flag display_startup_errors off php_flag display_errors off php_flag html_errors off #End of suppress rules #Beginning of logging rules php_flag log_errors on php_value error_log /path/to/errors.log #End of logging rules
Note: The directory which will contain your errors log file must exist before any errors are logged, otherwise it will not create the log file automatically.
Resetting Function Keys on HP
Aug 11th
Some recent HP models feature the multimedia keys which overlaps the F1 – F12 keys. What HP does is to activate Fn key by default. What this means is that you need to press Fn + F1 to get the regular F1 key function. It turns out to be a very annoying feature for me since I frequently use Alt + F2(fire up command prompt on Ubuntu) and Alt + F4 to close applications. With the new feature in HP, I have to press Alt + Fn + F2 and Alt + Fn + F4, it’s a pain in the neck.

The solution can be found on HP’s official website
Disable Fn in the BIOS
-
Press the power button to turn on the computer.
-
Press the f10 key to open the BIOS setup window.
-
Use the right-arrow or left-arrow keys to navigate to the System Configuration option.

-
Use the up-arrow or down-arrow keys to navigate to the Action Keys Mode option, and then press the enter key to display the Enable / Disable menu.
-
Select the desired mode:
-
Disabled : Requires pressing Fn key + f1 through f12 to activate action keys
-
Enabled : Requires pressing only f1 through f12 to activate action keys
-
-
Press f10 key to save the selection and restart the computer.
File/Directory Manipulation With PHP
Aug 2nd
PHP Directory Function List
- opendir(String $path)
Opens up a directory handle for file/directory manipulation
- getcwd(void)
Returns the current working directory.
- readdir(resource $dir_handle)
Returns the filename of the next file in the directory.
- rewinddir ([ resource $dir_handle ] )
Returns the directory stream to the beginning of the directory.
- closedir([resource $dir_handle ])
Closes the directory.
Example(Indexing the directory):
<?php
$cur_dir = '/usr/download';
/** opens the directory handle */
$dir = opendir($cur_dir);
echo 'File Listing For : ';
echo getcwd() . '<br /><ul>';
/** goes through each files and output the filename */
while(($file = readdir($dir)) !== false){
/** do not output . and .. */
if($file != '.' && $file != '..'){
echo '<li>' . $file . '</li>';
}
}
echo '</ul>';
/** closes the directory handle */
closedir($dir);
?>
A more object oriented approach
<?php
$cur_dir = '/usr/download';
$dir = dir($cur_dir);
echo 'File Listing For : ';
echo $cur_dir . '<br /><ul>';
/** goes through each files and output the filename */
while(($file = $dir->read()) !== false){
/** do not output . and .. */
if($file != '.' && $file != '..'){
echo '<li>' . $file . '</li>';
}
}
echo '</ul>';
/** closes the directory handle */
$dir->close();
?>