Web Development & WordPress

Sorting Arrays in PHP: rsort() and arsort() Functions Explained

This PHP script provides an example of how to use rsort() for reverse sorting and arsort() for reverse sorting while preserving the array keys.

<?php
echo "<pre>";
$reverseSort= array("a","d","e","b","c");
echo " <br> Main array <br>";
print_r($reverseSort);
rsort($reverseSort);
echo"<br>After rsort():<br>";
print_r($reverseSort);

$reverseSort1= array("a","d","e","b","c");
echo " <br> Main array <br>";
print_r($reverseSort1);

echo"<br>After rasort() which preserve keys :<br>";
arsort($reverseSort1);
print_r($reverseSort1);

echo "</pre>";
?>

OUTPUT

 Main array 
Array
(
    [0] => a
    [1] => d
    [2] => e
    [3] => b
    [4] => c
)

After rsort():
Array
(
    [0] => e
    [1] => d
    [2] => c
    [3] => b
    [4] => a
)
 
 Main array 
Array
(
    [0] => a
    [1] => d
    [2] => e
    [3] => b
    [4] => c
)

After rasort() which preserve keys :
Array
(
    [2] => e
    [1] => d
    [4] => c
    [3] => b
    [0] => a
)
Web Development & WordPress

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”
Web Development & WordPress

Mastering PHP String Search: The Differences Between strpos, stripos, and strripos Explained

  • stripos searches for the first occurrence of a substring in a string, ignoring case.
  • strripos searches 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";    
?>
Continue reading “Mastering PHP String Search: The Differences Between strpos, stripos, and strripos Explained”