note 12003 deleted from function.chop by irc-html
| From: | irc-html@php.net | Date: | Wed, 16 Jan 2002 16:20:54 +0000 |
| Subject: | note 12003 deleted from function.chop by irc-html | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-24905@lists.php.net to get a copy of this message | ||
Perl's chop actually does two things:
1) Changes the value of the argument variable to what remains of it after its last character is
chopped, and
2) Returns the character chopped
The only way to really replicate perl chop is to do both of these things. Because the argument
variable value is changed in the process but not returned, this requires the variable to be passed
by reference, i.e.:
#####
# perl
my $chopme="lrep si ereh";
while (length($chopme))
{
print chop $chopme;
}
#####
approximately equals:
<?php
$chopme="php si ereh";
while (strlen($chopme))
{
echo pl_chop("chopme");
}
function pl_chop ($ref)
{
global ${$ref};
if (strlen(${$ref})==0) {return "";}
$chop=substr(${$ref}, -1);
${$ref}=substr(${$ref}, 0, -1);
return $chop;
}
?>