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");
?>
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.
<?php
$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.
print count($fruit); // prints the number of different fruits in the array
Sort – sort()
Sorts an array by either alphabetical order or integer value.
$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"
Print Join – join()
The first parameter is referred to as the “glue”.
$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 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.
// 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:
<?php
$flip = rand(0,1);
if ($flip = true){
echo "Heads";
}
else {
echo "Tails";
}
?>
Comments
Recent Work
Basalt
basalt.softwareFree desktop AI Chat client, designed for developers and businesses. Unlocks advanced model settings only available in the API. Includes quality of life features like custom syntax highlighting.
BidBear
bidbear.ioBidbear is a report automation tool. It downloads Amazon Seller and Advertising reports, daily, to a private database. It then merges and formats the data into beautiful, on demand, exportable performance reports.