PHP developers sometimes need to measure textual similarity between strings. This comes up when preparing data, displaying results, or integrating information with other services.
The similar_text() operation handles this particular task. The following examples show its result, its limitations, and a practical way to apply it.
What Is similar_text() in PHP?
Measure textual similarity between strings.
Its syntax is:
similar_text(string $string1, string $string2, float &$percent = null): int
The syntax shows the expected parameters and return type. Arguments marked with an ampersand are passed by reference and may be modified. Optional parameters control additional behavior; values must match the declared types.
Important: The percentage is optional output by reference; this is a byte-oriented heuristic and should not be used for authorization.
Parameters:
$string1— A named input or configuration argument; use the declared type and optional default shown above.$string2— A named input or configuration argument; use the declared type and optional default shown above.&$percent— May be updated by the function.
Example 1: Basic Usage
This first example uses a small input to demonstrate the main result of similar_text().
<?php
echo similar_text("hello", "hallo");
Output:
4
The output demonstrates the primary operation: measure textual similarity between strings.
Example 2: Understanding Another Case
This second example changes the input or option to reveal a different behavior of similar_text().
<?php
$percent = 0.0; similar_text("color", "colour", $percent); printf("%.1f%%", $percent);
Output:
90.9%
Compared with the first example, this one illustrates the same operation with a different input shape or option. The percentage is optional output by reference; this is a byte-oriented heuristic and should not be used for authorization.
Practical Example: A Real Application Scenario
Here is a practical use of similar_text() with realistic sample information. Adapt the values to match your application.
<?php
$candidate = "notebook"; $entered = "noteook"; similar_text($candidate, $entered, $percent); echo "Similarity: " . round($percent, 1) . "%";
Output:
Similarity: 93.3%
This is a useful starting point, but production code should check errors and validate values before performing the operation. The percentage is optional output by reference; this is a byte-oriented heuristic and should not be used for authorization.
Important Behavior and Edge Cases
The percentage is optional output by reference; this is a byte-oriented heuristic and should not be used for authorization.
- Input: Use the expected PHP types and validate user-provided values.
- Result: Check the return value separately from any modifications to the input.
- Environment: For extension, database, or filesystem examples, verify availability and handle failures before accessing results.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
similar_text() | Measure textual similarity between strings. |
| Related functions | levenshtein() measures edit distance; strcmp() tests lexical comparison. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Confusing bytes with characters | The percentage is optional output by reference; this is a byte-oriented heuristic and should not be used for authorization. |
| Assuming a similarity score proves identity | Similarity estimates are not reliable for authorization or exact matching. |
| Using a different text operation without checking its semantics | levenshtein() measures edit distance; strcmp() tests lexical comparison. |
Key Takeaways
similar_text()lets you measure textual similarity between strings.- The percentage is optional output by reference; this is a byte-oriented heuristic and should not be used for authorization.
- levenshtein() measures edit distance; strcmp() tests lexical comparison.
- Check input, return values, and environmental requirements when adapting the examples.
similar_text() provides a focused PHP operation for measure textual similarity between strings. Start with the simple examples, then adapt the practical example with the correct checks for your project.