You down with PHP?

Part 2

Robert M Ricci
May 9 · 2 min read
Photo by KOBU Agency on Unsplash

So in my last post, I gave a little history of PHP. We dove into variables, assignment operators, and contacting with the (.) dot operator. You can read that article here. In this one, we will keep getting into syntax. We will discuss functions and go over some of the built-in functions that PHP provides out of the box.

Like in all other languages a function is just a set of instructions that need to be executed. A function starts with the keyword function. It is then followed by a camel-cased name. Then by a pair of parenthesis that would contain parameters. That is followed by curly braces. Within the braces is the code block. The return keyword will return the final output, thus exiting the function. Below is an example of a function.

   function ultimateQuestion() {             return 42;
}

To call a function you simply type it in followed by a pair of parenthesis.

    function ultimateQuestion() {      return 42;    }
ultimateQuestion();
//returns 42. Which is the answer to the Ulitimate question about
life, the Universe, and Everything

In this section, I’m going to go over a few built-in functions. I will give the syntax and an example.

The gettype and var_dump are both similar so I will go over them together. They both take in a variable and return what data type it is. The main difference is that var_dump gives a little more information past that. With var_dump you get the data type, how many characters, and also what the variable is holding. Below are examples.

$name="Zaphod Beeblebrox";
$number=42;
gettype($name); //returns string
gettype($number); //returns integer
var_dump($name); //returns string(17) "Zaphod Beeblebrox"
var_dump($number); //returns int(42)

Next, we will talk about some string functions. Again like most other languages, PHP has built-in functions that can lower or upper case a string, they also have a function that can reverse a word, and so on. Let’s talk about these three.

strrev ( string $string ) : string
echo strrev("Vogon");
//Prints nogoV
strtolower ( string $string ) : string
echo strtolower("Vogon");
//Prints vogon
strtoupper ( string $string ) : string
echo strtoupper("vogon");
//Prints VOGON

For the final set, we will talk about math built-in functions. One returns the absolute value of an integer, the other rounds a number.

abs ( mixed $number ) : int|floatabs(-42); //returns 42round ( float $val , int $precision = 0 , int $mode = PHP_ROUND_HALF_UP ) : floatrounds(41.5); //Prints 42rounds(42.2); //Prints 42

So that's a little about PHP functions and built-in functions. I will link the PHP docs below so you can look at the many other available functions.

Geek Culture

Proud to geek out. Follow to join our +1.5M monthly readers.