• Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Pinterest
    • Youtube
    • Instagram
    • RSS
Useful PHP built-in functions
Home
PHP

Useful PHP built-in functions

February 2nd, 2026 Ivan Dimov PHP 0 comments

Some useful PHP built-in functions explained below very helpful for beginners.

substr_count

The substr_count function allows you to see how many times a substring occurs within a string. It is not useful to use it if you just want to know if the substring occurs in the string as there is a better alternative in terms of speed and efficiency.

Examples:

echo substr_count("roared", "ed"); // this prints out 1 as ed occurs 1 time within roared



echo substr_count("concrete jungle, jungle", "jungle"); // this prints out 2 as jungle occurs 2 times within the string



echo substr_count("meh", "me", 1); // this prints out 0  as we are searching for the substring me in the string meh but start our search after the first index and that is why there are no matches

strpos

Strpos takes again a haystack, a needle and optionally an offset. It searches for a substring within a string and returns the index of any matches. If there are no matches – it returns the boolean false. It could be used if you simply want to know if the substring is there.

Examples:

echo strpos("roared", "ed"); // prints out 4



echo strpos("concrete jungle, jungle", "jungle"); // prints out 9



echo strpos("meh", "me", 1); // prints out false

implode

You can use implode to join up an array of elements into a single string with a particular glue/string that repeats itself for every joined element.

Examples

echo implode(" - ",  array("who","am","i")); // prints who - am - i



echo implode("jungle", array("In the", " there was another ", "")); // prints In the jungle there was another jungle

explode

You can use explode to create an array from a string. The string is broken down on a particular substring to create elements whenever the substring is found.

Examples

echo var_dump(explode("is",  "Hemingway is a writer who is a writer"));

/*

Prints out the following array



array(3) { [0]=> string(10) "Hemingway " [1]=> string(14) " a writer who " [2]=> string(9) " a writer" }

*/



echo var_dump(explode(",", "Testing, testing, 1, 2, 3"));

/*

Prints out the following array



array(5) { [0]=> string(7) "Testing" [1]=> string(8) " testing" [2]=> string(2) " 1" [3]=> string(2) " 2" [4]=> string(2) " 3" }

*/

str_repeat

str_repeat allows you to repeat a string arbitrary number of times.

Examples

echo str_repeat("Dog. ", 3); // prints out Dog. Dog. Dog.

array_shift

Array_shift removes the first element of an array and returns it. Therefore, it changes the original array besides returning the first element.

Examples

$arr = array(1,2,3,4,5,6);



echo array_shift($arr); // prints  1



echo var_dump($arr); // an array with the numbers 2 to 6

array_pop

array_pop works like array_shift except that it removes the last element off the array and returns it.

Examples

$arr = array(1,2,3,4,5,6);



echo array_pop($arr); // prints 6



echo var_dump($arr); // prints an array with the numbers 1 to 5

array_slice

array_slice allows you to get parts of an array. You can specify from which index to start to take elements and how many elements to take, in that order.

Examples

$arr = array(1,2,3,4,5,6);



echo var_dump(array_slice($arr, 2)) ; // we take all elements starting at index 2, so it prints an array from 3 to 6.



echo var_dump(array_slice($arr, 2, 1)); // we take only 1 element, at index 2 - so it prints an array with the element 3 only

Facebook Twitter Google+ LinkedIn Pinterest
Next article 7 JavaScript array methods you must know as a programmer
Previous article 9 Best Programming languages you should learn in 2021

Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Related Posts

How to Create a Custom Options Page in WordPress? PHP
February 4th, 2026

How to Create a Custom Options Page in WordPress?

Stripe Payment Gateway Charge Credit Card with PHP Example API
February 4th, 2026

Stripe Payment Gateway Charge Credit Card with PHP Example

Choosing the Right PHP Framework for Your Web Application: A Comprehensive Guide Framework
February 2nd, 2026

Choosing the Right PHP Framework for Your Web Application: A Comprehensive Guide

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe
Get new posts by email:
Powered by follow.it
Advertisement
Like us
Recent Posts
  • Introduction to Git for version control
  • Const vs. Let vs. Var in Javascript. Which one should you use?
  • How do I send an auto-reply message on Skype?
  • Cool New JavaScript Features
  • Useful JavaScript globals
Categories
  • API
  • Bootstrap
  • Bot
  • CSS
  • CSS 3
  • Database
  • Designing
  • Framework
  • Guide
  • HTML
  • HTML 5
  • JavaScript
  • jQuery
  • Laravel
  • MySQL
  • Node.js
  • oAuth
  • Payment
  • PHP
  • Python
  • Social
  • Tips
  • Web 3.0
  • WordPress
Weekly Tags
  • PHP
  • javascript
  • How to
  • laravel
  • css
  • HTML to PDF
  • jQuery
  • api
  • Web Development
  • MYSQL
  • About
  • Privacy Policy
  • Back to top
© PHPLift.net. All rights reserved.