PHP: Current URL

'http' . ($_SERVER['HTTPS']) ? 's' : '') . ':// . 
((in_array($_SERVER['SERVER_PORT'], array(80, 443)) ? '' : ':' . $_SERVER['SERVER_PORT']) . 

$_SERVER[’HTTPS’] will be non-empty value if it’s running over SSL or using HTTPS protocol.

$_SERVER[’SERVER_PORT’] 80 is standard port and 443 is standard ssl port. The port number will be appended to the URL if not using these 2 standard ports.

PHP: Parsing XML

In PHP5, parsing and processing xml can be easily done with SimpleXML extension.

To parse xml file:

$obj = simplexml_load_file($file);

To parse xml string:

$obj = simplexml_load_string($string);


    <tag>value 1<tag>
    <tag attribute="attrbute value">value 2<tag>

After loading xml into $obj, access tag values as class method:

$obj->tag[$i] //if more than 1 tag

Access tag attribute as array:

$obj->tag[$i]['attribute'] // if more than 1 tag

JavaScript: Convert input text to upper or lower case

To convert to upper case, add onChange attribute to <input> and call toUpperCase() function:

<input type="text" onchange="javascript:this.value=this.value.toUpperCase();" />

to convert to lower case:

<input type="text" onchange="javascript:this.value=this.value.toLowerCase();" />

HTML: Definition List

I always forget the syntax for <dl>, <dt> and <dd> tags, even though it’s obvious – list, terms and definition / description.

Here’s the correct syntax:

    <dt>the term</dt>

PHP: Display errors

For security reasons, display_errors should be turned off in php.ini. This will not display the error on the browser.

However it’s inconvenient during development not being able to see the error and have to access error logs. This can be turned on with this code:

ini_set("display_errors", 1); 
error_reporting(E_ALL &&; ~E_NOTICE && E_WARNING);

This code should be removed / commented when put in production.