Blog Archives

PHP Quick Reference

Escape sequences for print output:

\" - double quote
\' - single quote
\n - new line
\t - tab
\r - carriage return
\$ - dollar sign
\\ - backslash

Max integer and float size (overflow):

//on a 32-bit system
$large_number = 2147483647;
var_dump($large_number);                     // int(2147483647)

$large_number = 2147483648;
var_dump($large_number);                     // float(2147483648)

$million = 1000000;
$large_number =  50000 * $million;
var_dump($large_number);                     // float(50000000000)

//on a 64-bit system
$large_number = 9223372036854775807;
var_dump($large_number);                     // int(9223372036854775807)

$large_number = 9223372036854775808;
var_dump($large_number);                     // float(9.2233720368548E+18)

$million = 1000000;
$large_number =  50000000000000 * $million;
var_dump($large_number);                     // float(5.0E+19)

generate random number:

echo rand() . "\n";
echo rand() . "\n";

echo rand(5, 15);

Get Current Page Name:

function curPageName() {
 return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}

Came across the following on stackoverflow while looking for the PHP equivalent of string.format.

Sprintf (similar to php printf, in c# string.format):

$filter = "content:%1$s title:%1$s^4.0 path.title:%1$s^4.0 description:%1$s ...";
$filter = sprintf($filter, "Cheese");

//OR

function format() {
    $args = func_get_args();
    if (count($args) == 0) {
        return;
    }
    if (count($args) == 1) {
        return $args[0];
    }
    $str = array_shift($args);
    $str = preg_replace_callback('/\\{(0|[1-9]\\d*)\\}/', create_function('$match', '$args = '.var_export($args, true).'; return isset($args[$match[1]]) ? $args[$match[1]] : $match[0];'), $str);
    return $str;
}

References
StackOverflow, “C# String.Format() Equivalent in PHP?”,
WebCheatSheet, http://www.webcheatsheet.com/PHP/get_current_page_url.php
PHP.Net, http://php.net

Advertisement

Get Current Page Name in ASP .NET

The code snippet below is a quick, easy to use function which allows you to get the current active page in an asp .net website/application.

There are similar functions for getting header information about a page, however, after some research I found the direct info I was looking for was much easier to retrieve using this method.

Enoy. 😉

VB .NET

Public Function GetCurrentPageName() As String
	Dim strPath As String = System.Web.HttpContext.Current.Request.Url.AbsolutePath
	Dim oInfo As New System.IO.FileInfo(strPath)
	Dim strPageName As String = oInfo.strPageName 
	Return strPageName
End Function

C#

public string GetCurrentPageName() 
{ 
    string spath = System.Web.HttpContext.Current.Request.Url.AbsolutePath; 
    System.IO.FileInfo oInfo = new System.IO.FileInfo(spath); 
    string spagename = oInfo.Name;
    return spagename;
}