Discover the various methods for sorting arrays in PHP, including the use of sort() and asort(). This guide provides a detailed explanation on how to sort both indexed and associative arrays while ensuring keys are preserved when necessary.
Author: allaboutbasic
Understanding Recursive Functions with and without Stepping in PHP
This PHP code demonstrates the use of recursive functions with two examples: a normal recursive function that increments a number until a specified condition is met, and a recursive function with stepping, which increments by a specified step value.
Continue reading “Understanding Recursive Functions with and without Stepping in PHP”Learning Plugins Development: Part 2
This WordPress plugin creates a shortcode [show_posts] that displays a list of the latest posts with their titles linked to the post pages. It fetches and lists up to 10 posts ordered by their ID in ascending order.

The output will be like this

Understanding PHP Array Functions: Merge, Slice, and Splice
Here I have tried to show various PHP array functions such as array_merge, array_slice, and array_splice. You can see how to merge two arrays, slice an array into parts, and splice arrays by removing and replacing elements.
CODE:
Continue reading “Understanding PHP Array Functions: Merge, Slice, and Splice”Learning Plugins Development: Part 1
Here I am going to create a very small plugins, mainly a shortcode which will be used in any page and post and that will show the message “My First Attempt of creating plugin”.

PHP String Trimming Functions
Here you can see how to remove unnecessary whitespace and specific characters from the beginning and end of strings using PHP’s trim(), ltrim(), and rtrim() functions.
Code:
Continue reading “PHP String Trimming Functions”Efficiently Replacing Multiple Words in a String Using PHP
Here I have shown how to efficiently replace multiple words in a string using PHP’s str_replace function. In this example you can see how to modify text by swapping out specified words with new ones.
CODE:
Continue reading “Efficiently Replacing Multiple Words in a String Using PHP”Understanding PHP Spaceship Operator for Comparing Values
The spaceship operator (<=>) in PHP is used for comparing two values and returns -1, 0, or 1 depending on whether the first value is less than, equal to, or greater than the second value. Introduced in PHP 7, it simplifies comparisons and is especially useful for sorting functions.
CODE:
Continue reading “Understanding PHP Spaceship Operator for Comparing Values”Mastering PHP String Search: The Differences Between strpos, stripos, and strripos Explained
stripossearches for the first occurrence of a substring in a string, ignoring case.strripossearches for the last occurrence of a substring in a string, ignoring case.
CODE:
<?php
$string = "This is a test by Tom. He is a good boy. This is another test";
echo "Main sentence is:<br>";
echo $string;
echo '<br>Search the word "test" using strpos() <br>';
echo strpos($string, "test");
echo '<br>Search the word "Test" using strpos() <br>';
echo strpos($string, "Test");
if(strpos($string, "Test")){
echo "Word Found";
}else{
echo "Word not found--as strpos() is case sensitive";
}
echo '<br>Search the word "Test" using stripos()-- case sensitive <br>';
echo stripos($string, "Test");
echo '<br>Search the word "This" using stripos()-- case sensitive <br>';
echo stripos($string, "This");
echo "<br>";
if(stripos($string, "This") !== false ){
echo " Word is found";
}else{
echo "Word not found";
}
echo '<br>Search the word "This" using strripos()-- case sensitive. <br>';
echo strripos($string, "This");
echo "<br>";
echo "Another Test<br>";
$text = "This is a test. This is only a test.";
echo $text;
echo "<br>";
$substring = "test";
$firstPos = stripos($text, $substring);
$lastPos = strripos($text, $substring);
echo "First occurrence of test: " . $firstPos . "\n";
echo "Last occurrence of test: " . $lastPos . "\n";
?>
Split String or String tokenization
CODE:
<?php
$sentence ="Hello World, My name is Jimmy";
echo "split using explode<br>";
$split1= explode(" ",$sentence);
print_r($split1);
echo "<br>Join Splitted Sentence<br>";
$join1=join(" ",$split1);
echo $join1;
echo "<br>Join splitted sentence using implode<br>";
$join2 = implode(" ",$split1);
echo $join2;
echo "<br>Split string using multiple delimeter<br>";
$split2 = strtok($sentence," ,");
while ($split2 !== false){
echo $split2."<br>";
$split2=strtok(" ,");
}
print_r($split2);
echo "<br>Split string using regular expression <br>";
$split3= preg_split("/ |,/",$sentence);
print_r($split3);
echo "<br>Split \"Hello World\" in characters<br>";
echo <<<MyText
Split "Hello WOrld" in Characters <br>
MyText;
$split4 = str_split("Hello World");
print_r($split4);
?>