PHP Function Reference


This is a partial list of built-in PHP functions that I use for my reference. A full list of functions can be found in the documentation. There is also a great document on how to read the definitions provided on here: About Prototypes

Built in Functions

String Functions

A full list of string functions: Strings.

String Length – strlen()

This function returns an integer. String Length

  // get the length of a string and
  // print it to the screen
  $length = strlen("david");
  print $length;

Substring – substr() Substring

Returns a string. Has the following format:

substr($target_string, starting character, # of characters to be returned)

Remember this is a zero indexed array so we start at 0.

<span class="variable"><?php
$myname</span> = <span class="string">"David"</span>;

<span class="variable">$partial</span> = substr(<span class="variable">$myname</span>, <span class="number">0</span>, <span class="number">3</span>);
<span class="keyword">print</span> <span class="variable">$partial</span>;
<span class="comment">// prints "dav"

Uppercase – strtoupper()

Converts the target string to uppercase. String to Uppercase

$myname = "Nick";

$uppercase = strtoupper($myname);
print $uppercase;
// prints "NICK"


Lowercase – strtolower()

Converts the target string to lowercase. String to Lowercase

$myname = "Nick";

$lowercase = strtolower($myname);
print $lowercase;
// prints "nick"


String Position – strpos()

Finds the position of the first occurrence of a substring in a string, and returns that position as an integer. Will return FALSE (0) if the substring cannot be found. String Position

strpos("nick", "n");   // 0
strpos("nick", "i");   // 1
strpos("nick", "ick"); // 1
strpos("nick", "zxc"); // false

Variable Functions

A full list of variable functions can be found in the documentation. Variable Functions

Array Functions

A full list of array functions can be found in the documentation. Array Functions

Array – array()

Array is itself a function. The array must be named with a variable so that it can be referenced later. Array


$fruit = array("apple", "pear", "orange", "kiwi");


Push – array_push()

This function adds elements to the end of the selected array. It takes two arguments. The first is the name of the array and the second is the string or integer to add. These items will be added to the end of the array. Array Push


$fruit = array("apple", "pear", "orange", "kiwi");

array_push($fruit, "strawberries");
array_push($fruit, "limes");


Count – count()

Counts the number of elements in the array. Returns an integer. Count

print count($fruit); // prints the number of different fruits in the array

Sort – sort()

Sorts an array by either alphabetical order or integer value. Sort

$fruit = array("apple", "pear", "orange", "kiwi");

sort ($fruit);

/* The fruit has been sorted but will not be displayed
*/  unless you print it.

print join(", ", $fruit);

// Will print "apple, kiwi, orange, pear"

The first parameter is referred to as the “glue”. Print Join

$fruit = array("apple", "pear", "orange", "kiwi");

print join(", ", $fruit);

// Will print "apple, pear, orange, kiwi"

Math Functions

Round – round()

Rounds number to the nearest integer, or if specified, to a set number of decimal places. Round

// Round pi down from 3.1416...
$round = round(M_PI);
print $round;  // prints 3

Where M_PI is the php representation of Pi.

Can also round to a specified decimal place.

// This time, round pi to 4 places
$round_decimal = round(M_PI, 4);
print $round_decimal; // prints 3.1416

Random – rand()

Generates a random integer between the specified range. Random

// prints a number between 1 and 10
print rand(1,10);

Also note that PHP views 0 as false and 1 as true. So if you wanted to create a 50/50 chance that a variable is true or false you could do the following:


$flip = rand(0,1);

if ($flip = true){
echo "Heads";
    else {
echo "Tails";