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

Useful PHP built-in functions

February 7th, 2017 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

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

Related

Facebook Twitter Google+ LinkedIn Pinterest
Next article Working with APIs in PHP
Previous article PHP Basics

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 PDFs from HTML with PHP and Dompdf HTML
June 29th, 2022

How to Create PDFs from HTML with PHP and Dompdf

Is PHP dead in 2021? Is PHP still relevant or worth the effort? PHP
May 22nd, 2022

Is PHP dead in 2021? Is PHP still relevant or worth the effort?

How to Extract Text from PDF using PHP PHP
May 17th, 2022

How to Extract Text from PDF using PHP

Leave a Reply Cancel reply

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

    Advertisement
    Like us
    Recent Posts
    • How to Create PDFs from HTML with PHP and Dompdf
    • How to create a screen recorder in JavaScript
    • Best 10 Programming Languages that will rule in 2022
    • Top 7 Websites To Get Your First Paid Internship
    • How to Create Bar Chart Race in JavaScript
    Categories
    • API
    • Bootstrap
    • Bot
    • CSS
    • CSS 3
    • Database
    • Designing
    • Framework
    • Guide
    • HTML
    • HTML 5
    • JavaScript
    • jQuery
    • MySQL
    • Node.js
    • oAuth
    • Payment
    • PHP
    • Python
    • Social
    • Tips
    • WordPress
    Weekly Tags
    • PHP
    • How to
    • javascript
    • api
    • MYSQL
    • jQuery
    • HTML to PDF
    • PHP Basics
    • Programming Habits
    • HTML5
    • About
    • Privacy Policy
    • Back to top
    © PHPLift 2021. All rights reserved.