10 useful PHP functions

1

PHP has a lot of built-in functions and classes — there are hundreds of them! However you should start from something smaller, so here is a list of 10 most common and useful built-in PHP functions.

echo — print a string

echo 'Hello, PHP!';

var_dump — print a typed structure and content of a given value

This function is best for debug purposes.

$array = [1, 2, 3];
var_dump($array);

count — get array elements count

$values = [1, 2, 3, 4, 5];
var_dump( count($values) ); # Prints 5

empty — check if a given array or string is empty

$values = [1, 2, 3, 4, 5];

if(empty($values))
{
    # ...
}
else
{
    # We will get here
}

isset — check if a variable with a given name is set or an array has a given key

$a = 10;
$b = ['Joe' => 15, 'Sam' => 25];

if(isset($a))
{
    # $a is defined, go here
}

if(isset($d))
{
    # $d is not defined, skip this
}

if(isset($b['Sam']))
{
    # $b has "Sam" key, go here
}

explode — split a string into an array by a given separator

$names = 'One;Two;Three;Four;Five';
$array = explode(';', $names);

foreach($array as $name)
{
    var_dump($name); # Prints each name
}

implode — join array elements into a string with a given glue

$array = ['One', 'Two', 'Three', 'Four', 'Five'];
$names = implode(';', $array);
var_dump($names); # Prints "One;Two;Three;Four;Five"

file_get_contents — read a file from a file path or a URL

$text = file_get_contents('text.txt');
$html = file_get_contents('https://google.com');

file_put_contents — write a file with a given content

file_put_contents('text.txt', $text);

strlen — get a length of a given string

$name = 'Sam';
echo 'Your name is ' . strlen($name) . ' characters length';
Lesson 8
Share this page:

Learning plan

The basement of PHP control flow structures you should learn before continue
It's time to create your own PHP functions
How reference the same PHP values with different names
9. 10 useful PHP functions
Most common and useful PHP built-in functions
The next portion of things you should learn about PHP types
The second part of basics of PHP arrays
Things you should know to configure error reporting properly