HomeLanguagesPHP chop() Function

PHP chop() Function

The chop() in PHP is used to remove white spaces or any other specified characters from the end of a string.

Syntax:

string chop($string, $character)

Parameters: This function accepts two parameters as shown in the above syntax and are described below:

  1. $string : It is used to specify the string which is needed to be checked.
  2. $character : It specifies the character which is needed to be removed from the given string. If this parameter is not specified then NULL, tab, newline, vertical tab, carriage return and ordinary white space are removed automatically.

Return Value: The return type of the chop() function is string. It returns the string after removing the specified characters from the end.

Program 1: In the below program a string is initialized as hello neveropen!, by using chop() function the characters – ‘s’ and ‘!’ are removed from the end of the string.




<?php
  
$s= "Hello Geeks!";
echo $s. "\n";
echo chop($s, "s!");
  
?>


Output:

Hello Geeks!
Hello Geek

Program 2: In the below program, since no character parameter is mentioned. Then automatically the newlines will be removed from the given string.




<?php
  
$s= "Hello Geeks! \n best wishes \n \n";
echo $s;
echo chop($s);
echo $s;
  
?>


Output:

Hello Geeks! 
 best wishes 
 
Hello Geeks! 
 best wishesHello Geeks! 
 best wishes 
 

Reference:
http://php.net/manual/en/function.chop.php

RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS