Home >Backend Development >PHP Problem >what does substr mean in php
substr means "intercepting a string" in PHP; the substr() function can return a part of the string, the syntax is "substr(string,start,length)", and the parameter string indicates that a part of it is to be returned string, the parameter start is used to specify where the string to be intercepted starts, and the parameter length indicates the length to be intercepted.
The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer
substr() The function returns a portion of a string.
Note: If the start parameter is negative and length is less than or equal to start, length is 0.
Syntax
substr(string,start,length)
Parameter Description
string Required. Specifies a part of the string to be returned.
start Required. Specifies where in the string to begin.
Positive number - starts at the specified position in the string
Negative number - starts at the specified position from the end of the string
0 - starts at the specified position in the string Starts at the first character in
#length optional. Specifies the length of the string to be returned. The default is until the end of the string.
Positive number - Return from the position of the start parameter
Negative number - Return from the end of the string
Returned result:
Returns the extracted part of the string, returns FALSE if it fails, or returns an empty string.
The example is as follows:
<?php // Positive numbers: echo substr("Hello world",10)."<br>"; echo substr("Hello world",1)."<br>"; echo substr("Hello world",3)."<br>"; echo substr("Hello world",7)."<br>"; echo "<br>"; // Negative numbers: echo substr("Hello world",-1)."<br>"; echo substr("Hello world",-10)."<br>"; echo substr("Hello world",-8)."<br>"; echo substr("Hello world",-4)."<br>"; ?>
Output result:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of what does substr mean in php. For more information, please follow other related articles on the PHP Chinese website!