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

Check if a string starts with a given word in PHP

PHPz
Release: 2023-08-20 15:30:02
forward
1431 people have browsed it

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)
Copy after login

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!";
?>
Copy after login

输出

以下是输出 −

True! It begins with p!
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template