Оcenka-BEL.com - безплатни онлайн тестове, пробни изпити, видео уроци и помощ при писане на задания от литературно и хуманитарно естество

Useful PHP built-in functions

Публикувано: 2018-02-18 19:31:28

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:

 

1

2

3

4

5

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:

 

1

2

3

4

5

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

 

1

2

3

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

 

1

2

3

4

5

6

7

8

9

10

11

12

13

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

 

1

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

 

1

2

3

4

5

$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

 

1

2

3

4

5

$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

 

1

2

3

4

5

$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:

Related

PHP Basics

PHP Basics

Examining variable types in PHP

Examining variable types in PHP

Working with dates in PHP and JavaScript

Working with dates in PHP and JavaScript

Facebook Twitter Google+ LinkedIn Pinterest

NEXT ARTICLEWorking with APIs in PHP

PREVIOUS ARTICLEPHP 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

-->