PHP: Built-In Functions

In PHP, or any other coding language, a function is a reusable piece of code that you can call on throughout your application or script. PHP has over 1000 built in functions.

String Functions

Common string functions are strlen (for finding the length of a string), substr (for pulling out a specific part of the string, strtoupper (for making a string upper-case), and strtolower (for making a string lower-case). I've included some simple code examples below to demonstrate

<?php
   $name = "Andrew";
   $length = strlen($name)
   print $length;
   ?>

<?php
    $name = "Andrew";
    $partial = substr($name, 0, 4);
    print $partial;
    ?>

<?php
    $uppercase = strtoupper($name);
    print $uppercase;
    ?>

<?php
    $lowercase = strtolower($name);
    print $lowercase;
    ?>

In the substr example, you'll notice that I'm using the $name variable followed by a couple numbers. the 0 indicates the starting position of what I want to pull and the 4 indicates how many characters of the string I want to pull out.

Before I forget, another useful string function is the strpos function, which finds the position of a specified substring in a given string. for example, I could find out what position the "r" in my name is.

<?php
    strpos("andrew", "r");
    ?>

The output will be 4. I can also use the strpos function to create a condition, like with the following example.

<?php
    if (strpos("Andrew", "z") === false) {
       print "Sorry, no 'z' in 'Andrew'";
    }
    ?>

Mathematical Functions

A common mathematical function in PHP is the round function, which will round numbers with decimal points up or down. Numbers with decimal points are referred to as floating point numbers. An example of the round function will look like this.

 <?php
    $round = round(M_PI);
    print $round;
    ?>
 
I created a variable called "round" and used the round function against PHP constant for Pi. Then , I simply printed the variable. The result is 3, since Pi is approximately 3.14. However, let's say that I wanted to round to a specific number of decimal places. The example below, I'm doing the same thing as the first above example, except that I'm telling the round function to round to 3 decimal places. The result is 3.142.

 <?php
    $round_decimal = round(M_PI, 3);
    print $round_decimal;  
    ?>

Another common mathematical function in PHP is the rand function, which returns a random number from a specified range.  In the below example, I'll echo a number between 1 and 100.

<?php
    $number = rand(1,100);
    echo $number;
    ?>

To further play with the rand function, the example below will pull a random letter out of my name.

<?php
    $name = "Andrew";
    $namelength = strlen($name) -1;
    echo substr($name, rand(0, $namelength),1);
    ?>

Array Functions

Ironically, when I talked about making an array, we were using a function since array() is a function itself. Aside from that, the array_push function is a common array function. Array_push takes the argument of 1) an array and 2) an element to add at the end of an array. In the below example, I create an empty array for meatloaf ingredients. Then, I use array_push to add the individual ingredients to the array. Finally, I count the number of elements, or items, in the array.

<?php
$meatloaf_ingredients = array();
array_push($meatloaf_ingredients, "ground beef");
    array_push($meatloaf_ingredients, "onion soup mix");
    array_push($meatloaf_ingredients, "bread crumbs");
    array_push($meatloaf_ingredients, "eggs");
    array_push($meatloaf_ingredients, "ketchup");
    print count($meatloaf_ingredients);
?>

When you have multiple elements in an array, counting them is fine but you'll more likely want to sort them. To do so, PHP has a built in sort function. In my below example, I have an array of odd numbers in no specific order. I'll use the sort function to sort them and then I'll print them out using echo. I'm also using the join function, which simply formats the array. The arguments to the join function is how to separate the array elements (this could be a comma, a semicolon, etc.), and then the variable where my array is stored.

<?php
$odds = array(1,5,9,3,7);
sort($odds);
echo join(",", $odds);
?>

This will order the numbers 1, 3, 5, 7, and 9. There also an rsort function that performs the reverse operation. The result of the example below is 9, 7, 5, 3, and 1.

<?php
rsort($odds);
echo join(",", $odds);
?>

This has been a quick look at some common functions baked into PHP.

Comments

Popular posts from this blog

Installing CentOS 7 on a Raspberry Pi 3

Modifying the Zebra F-701 & F-402 pens

How to fix DPM Auto-Protection failures of SQL servers