Check out bidbear.io Amazon Advertising for Humans. Now publicly available 🚀

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.net: String Length

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

Substring – substr()

PHP..net: 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"
?></span>

Uppercase – strtoupper()

Converts the target string to uppercase.

PHP.net: String to Uppercase

<?php
$myname = "Nick";

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

?>

Lowercase – strtolower()

Converts the target string to lowercase.

PHP.net: 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.

PHP.net: 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 php.net documentation.

PHP.net: Variable Functions

Array Functions

A full list of array functions can be found in the php.net documentation.

PHP.net: Array Functions

Array – array()

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

PHP.net: Array

<?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.net: Array Push

<?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.

PHP.net: 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.

PHP.net: 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”.

PHP.net: 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.

PHP.net: 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.

PHP.net: 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:

<?php

$flip = rand(0,1);

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

?>

Amazon Ad Analytics For Humans

Advertising reports automatically saved and displayed beautifully for powerful insights.

bidbear.io
portfolios page sunburst chart