w3resource

PHP: strlen() function

Description

The strlen() function is used to return the length of a string.

Version:

(PHP 4 and above)

Syntax:

strlen(string_name)

Parameter:

Name Description Required /
Optional
Type
string_name The specified string. Required String

Return value:

The length of the string on success, and 0 for an empty string.

Value Type: Integer.

Example:

<?php 
$string_name = 'Welcome to w3resource.com';
echo strlen($string_name)."<br>";
$string_name = ' Welcome to w3resource.com  ';
echo strlen($string_name)."<br>";
?>

Output:

25
28

View the example in the browser

See also

PHP Function Reference

Previous: stripos
Next: strnatcmp



PHP: Tips of the Day

Float

Example:

$float = 0.123;

For historical reasons "double" is returned by gettype() in case of a float, and not simply "float"

Floats are floating point numbers, which allow more output precision than plain integers.

Floats and integers can be used together due to PHP's loose casting of variable types:

$sum = 3 + 0.14;
echo $sum;

Output:

3.14

php does not show float as float number like other languages, for example:

$var = 1;
echo ((float) $var);

Output:

1