PHP is an open source scripting language which is used widely. The code is executed on the server and is free of cost. It is easy to learn for beginners and at the same time powerful enough to run Facebook. With PHP, one can generate dynamic page content, collect form data, receive and send cookies, encrypt data, handle data in database and files on the server. PHP offers a range of functions that make it easy to work with different data types. In this tutorial, we are going to explore substr in PHP.

What is substr in PHP?

substr in PHP is a built-in function used to extract a part of the given string. The function returns the substring specified by the start and length parameter. It is supported by PHP 4 and above. Let us see how we can use substr() to cut a portion of the string.

Syntax

The substr in php has the following syntax:

 substr(string, starting_position, length_of_substring)

The function returns an extracted part of the string if the starting_position is found in the string. Otherwise, an empty string or False is returned.  [1] 

Example:

We want the function to return “in php” from the string.

<!DOCTYPE html>

<html>

<body>

<?php

echo substr("substr in php",6);

?>

</body>

</html>

Output:

substr_in_PHP_1

Stand Out From Your Peers this Appraisal Season

Start Learning With Our FREE CoursesEnroll Now
Stand Out From Your Peers this Appraisal Season

Parameter Values

The substr in PHP function has three arguments or parameters. One parameter is optional, and the other two are mandatory. 

1. string: This parameter includes the original string, i.e., the string which needs to be modified or cut. It is a required parameter. The input string must at least be one character. 

2. starting_position: This parameter includes the position, which is the index from where the part of the string needs to be extracted. The input is an integer, and it is a required parameter. 

  • If the integer is positive, the returned string starts from that position in the string. The first character is counted as zero.
  • If the integer is negative, the returned string starts from that position from the end of the string. The counting begins from 1 at the end of the string. 
  • If the starting_position is greater than the string size, then the function returns FALSE.

3. length_of_substring: This is an optional parameter, and it is an integer value referring to the length of the string cut from the original one.

  • If the length is positive, then the substring includes the number of characters passed in this parameter beginning from the starting_position.
  • If the length is negative, then the substring begins from the starting_position and extracts the length from the string's end. Many characters are omitted from the end if the length passed is negative. 
  • If the length parameter is zero, null or False will be returned
  • If the parameter is not passed, then the function returns a string starting from starting_position till the end. 

Here are some examples to explain the use of these parameters:

1. Positive Starting Position 

<!DOCTYPE html>

<html>

<body>

<?php   

    $str = "substr in php";

    $sub_str = substr($str, 10);

    echo "<h3> Original:  $str </h3>";

    echo "<h3> Susbtring:  $sub_str </h3>";

?>

</body>

</html>

Output: 

substr_in_PHP_2.

2. Negative Starting Position

<!DOCTYPE html>

<html>

<body>

<?php   

    $str = "substr in php";    

    $sub_str = substr($str, -10);

    echo "<h3> Original:  $str </h3>";

    echo "<h3> Susbtring:  $sub_str </h3>";

?>

</body>

</html>

Output:

substr_in_PHP_3.

3. Positive Starting Position and Length

<!DOCTYPE html>

<html>

<body>

<?php

    $str = "substr in php";    

    $sub_str = substr($str, 3, 3);   

    echo "<h3> Original:  $str </h3>";

    echo "<h3> Susbtring:  $sub_str </h3>";

?>

</body>

</html>

Output:

substr_in_PHP_4

4. Negative Starting Position and Length

<!DOCTYPE html>

<html>

<body>

<?php    

    $str = "substr in php";   

    $sub_str = substr($str, -6, -3);   

    echo "<h3> Original:  $str </h3>";

    echo "<h3> Susbtring:  $sub_str </h3>";

?>

</body>

</html>

Output:

substr_in_PHP_5.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

5. Positive Length and Negative Starting Position

<!DOCTYPE html>

<html>

<body>

<?php

    $str = "substr in php";    

    $sub_str = substr($str, -6, 6);    

    echo "<h3> Original:  $str </h3>";

    echo "<h3> Substring:  $sub_str </h3>";

?>

</body>

</html>

Output:

substr_in_PHP_6

6. Negative Length and Positive Starting Position

<!DOCTYPE html>

<html>

<body>

<?php   

    $str = "substr in php";    

    $sub_str = substr($str, 0, -6);   

    echo "<h3> Original:  $str </h3>";

    echo "<h3> Substring:  $sub_str </h3>";

?>

</body>

</html>

Output:

substr_in_PHP_7

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion 

In this substr in PHP tutorial, we learned about substr function, its use, and syntax.  If you are interested in learning more about PHP and web development, do not forget to check out Simplilearn’s Full-stack web development certification. On the other hand, with Simplilearn’s free learning platform SkillUp, you can learn the most in demand skills in software development too. Enroll today and get ahead in your PHP career now!