diff --git a/lib/tests/weblib_test.php b/lib/tests/weblib_test.php
index 6b93afa3f1b..8e4f5606764 100644
--- a/lib/tests/weblib_test.php
+++ b/lib/tests/weblib_test.php
@@ -23,9 +23,7 @@
  * @author     T.J.Hunt@open.ac.uk
  * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  */
-
 class weblib_test extends advanced_testcase {
-
     /**
      * @covers ::format_string
      */
@@ -985,4 +983,32 @@ EXPECTED;
     public function test_get_html_lang_attribute_value(string $langcode, string $expected): void {
         $this->assertEquals($expected, get_html_lang_attribute_value($langcode));
     }
+
+    /**
+     * Data provider for strip_querystring tests.
+     *
+     * @return array
+     */
+    public function strip_querystring_provider(): array {
+        return [
+            'Null' => [null, ''],
+            'Empty string' => ['', ''],
+            'No querystring' => ['https://example.com', 'https://example.com'],
+            'Querystring' => ['https://example.com?foo=bar', 'https://example.com'],
+            'Querystring with fragment' => ['https://example.com?foo=bar#baz', 'https://example.com'],
+            'Querystring with fragment and path' => ['https://example.com/foo/bar?foo=bar#baz', 'https://example.com/foo/bar'],
+        ];
+    }
+
+    /**
+     * Test the strip_querystring function with various exampels.
+     *
+     * @dataProvider strip_querystring_provider
+     * @param mixed $value
+     * @param mixed $expected
+     * @covers ::strip_querystring
+     */
+    public function test_strip_querystring($value, $expected): void {
+        $this->assertEquals($expected, strip_querystring($value));
+    }
 }
diff --git a/lib/weblib.php b/lib/weblib.php
index 27ac693e402..10b27d82cda 100644
--- a/lib/weblib.php
+++ b/lib/weblib.php
@@ -148,8 +148,11 @@ function addslashes_js($var) {
  * @return string The remaining URL.
  */
 function strip_querystring($url) {
+    if ($url === null || $url === '') {
+        return '';
+    }
 
-    if ($commapos = strpos($url ?? '', '?')) {
+    if ($commapos = strpos($url, '?')) {
         return substr($url, 0, $commapos);
     } else {
         return $url;