PHP Function Reference
Intro
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 php.net documentation. There is also a great document on how to read the definitions provided on php.net here: PHP.net: About Prototypes
Built in Functions
String Functions
A full list of string functions: PHP.net: Strings.
String Length – strlen()
This function returns an integer.
<?php
// get the length of a string and
// print it to the screen
$length = strlen("david");
print $length;
?>
Substring – substr()
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"
?></span>
Uppercase – strtoupper()
Converts the target string to uppercase.
<?php
$myname = "Nick";
$uppercase = strtoupper($myname);
print $uppercase;
// prints "NICK"
?>
Lowercase – strtolower()
Converts the target string to lowercase.
<?php
$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.
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 php.net documentation.
Array Functions
A full list of array functions can be found in the php.net documentation.
Array – array()
Array is itself a function. The array must be named with a variable so that it can be referenced later.
<?php
$fruit = array("apple", "pear", "orange", "kiwi");
?>