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

String Formatting

Snippets below have been condensed from their original sources for brevity. See references for original articles.

All examples are in C# .Net.

My own little function:

   private string DynamicFormat(string dbformatstring, string contentstring)
        {
            foreach (string item in dbformatstring.Split(',')) {
            string propname = item.Split(':')[0];
                string propvalue = item.Split(':')[1];
                contentstring = contentstring.Replace("{"+propname+"}",propvalue);
            }

            return contentstring;
        }

Format With extension method by James Newton-King:

public static string FormatWith(this string format, params object[] args)
{
  if (format == null)
    throw new ArgumentNullException("format");
 
  return string.Format(format, args);
}
 
public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
{
  if (format == null)
    throw new ArgumentNullException("format");
 
  return string.Format(provider, format, args);
}

References
James Newton-King, “FormatWith”,http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension-method.aspx