Home  >  Article  >  Backend Development  >  Check if a string starts with a given word in PHP

Check if a string starts with a given word in PHP

PHPz
PHPzforward
2023-08-20 15:29:101396browse

Check if a string starts with a given word in PHP

Create a function to check whether a string begins with the specified string or not. The function should return TRUE on success or FALSE on failure.

The following is the syntax −

begnWith(str, begnStr)

Consider the following parameters in it to check −

  • str − The string to be tested

  • begnStr − The text to be searched in the beginning of the specified string.

Example

The following is an example −

 Live Demo

<?php
   function begnWith($str, $begnString) {
      $len = strlen($begnString);
      return (substr($str, 0, $len) = = = $begnString);
   }
   if(begnWith("pqrstuvwxyz","p"))
      echo "True! It begins with p!";
   else
      echo "False! It isn&#39;t beginning with p!";
?>

输出

以下是输出 −

True! It begins with p!

The above is the detailed content of Check if a string starts with a given word in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete