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 php.net documentation. There is also a great document on how to read the definitions provided on php.net here.

Build in Functions

String Functions

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

String Length – strlen()

This function returns an integer.

php.net reference

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

Substring – substr()

php.net reference

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.

<?php
$myname = "David";

$partial = substr($myname, 0, 3);
print $partial;
// prints "dav"
?>

Uppercase – strtoupper()

Converts the target string to uppercase.

php.net reference

<?php
$myname = "Nick";

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

?>

Lowercase – strtolower()

Converts the target string to lowercase.

php.net reference

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

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.net reference

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

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

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 reference

$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”.

php.net reference

$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 reference

// 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 reference

// 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";
}

?>

 

+ There are no comments

Add yours