這篇文章主要介紹了php比較兩個字符串長度的方法,涉及php中strlen與abs函數計算字符串與數學運算的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了php比較兩個字符串長度的方法。分享給大家供大家參考。具體實現方法如下:
這段代碼計算兩個字符串的長度,然后計算其差值
- <?php
- // This will return a number of how many more characters the longest string has
- function str_compare_length($str1, $str2){
- $len1 = strlen($str1);
- $len2 = strlen($str2);
- return abs($len1 - $len2);
- }
- echo str_compare_length("This is the first string", "This is the second string");
- ?>
希望本文所述對大家的php程序設計有所幫助。
新聞熱點
疑難解答