Home >Backend Development >PHP Tutorial >How to compare the length of two strings in php

How to compare the length of two strings in php

WBOY
WBOYOriginal
2016-07-25 08:44:101094browse

The example in this article describes the method of comparing the length of two strings in PHP. Share it with everyone for your reference. The specific implementation method is as follows:

This code calculates the length of two strings and then calculates their difference

  1. // This will return a number of how many more characters the longest string has
  2. function str_compare_length($str1, $str2){
  3. $len1 = strlen($str1);
  4. $len2 = strlen($str2);
  5. return abs($len1 - $len2);
  6. }
  7. echo str_compare_length("This is the first string", "This is the second string");
  8. ?>
Copy code

I hope what is described in this article will be helpful to everyone’s PHP programming design.

php


Statement:
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
Previous article:php simulate post requestNext article:php simulate post request