LoginSignup
1
1

More than 5 years have passed since last update.

PHPで文字列をリバースする

Last updated at Posted at 2013-03-22
reverse.php
function reverse1($str1) {
    $count = strlen($str1);
    for ($i = $count - 1; $i >= 0; $i--) {
        echo $str1[$i];
    }
}

$str1 = "abcdef";
reverse1($str1);
reverse2.php
function reverse2($str2) {
    $count = strlen($str2);
    $num   = $count - 1;
    for ($i = 0; $i < $count / 2; $i++) {
        $last = $str2[$i];
        $str2[$i] = $str2[$num];
        $str2[$num] = $last;
        $num--;
    }
    echo $str2;
}

$str2 = "abcdef";
reverse2($str2);
reverse3.php
function reverse3($str3) {
    $count = strlen($str3);
    for($i = 0; $i < --$count; $i++) {
        $last = $str3[$i];
        $str3[$i] = $str3[$count];
        $str3[$count] = $last;
    }
    echo $str3;
}

$str3 = "abcdef";
reverse3($str3);
1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1