can't understand this function from php manual example (return bytes)

121

I've seen this function in the php manual but I can't understand how it works , the function is supposed to change the value of file size into bytes. this is the whole example

<?php
/*
Our php.ini contains the following settings:

display_errors = On
register_globals = Off
post_max_size = 8M
*/

echo 'display_errors = ' . ini_get('display_errors') . "\n";
echo 'register_globals = ' . ini_get('register_globals') . "\n";
echo 'post_max_size = ' . ini_get('post_max_size') . "\n";
echo 'post_max_size+1 = ' . (ini_get('post_max_size')+1) . "\n";
echo 'post_max_size in bytes = ' . return_bytes(ini_get('post_max_size'));

function return_bytes($val) {
    $val = trim($val);
    $last = strtolower($val[strlen($val)-1]);
    switch($last) {
        // The 'G' modifier is available since PHP 5.1.0
        case 'g':
            $val *= 1024;
        case 'm':
            $val *= 1024;
        case 'k':
            $val *= 1024;
    }

    return $val;
}

?>

what I can't understand is this line

$last = strtolower($val[strlen($val)-1]);

and that what called 'G' modifier , and what is $last variable used for? Thanks in advance ,

254

Answer

Solution:

$last = strtolower($val[strlen($val)-1]);

That line of code is getting the last character in$val, converting it to lower case, then putting it in the$last variable. That variable is then used in the switch statement to apply a multiplier to the input value.

See for some background information that might be useful to you.

People are also looking for solutions to the problem: php - mysql ranking for a single user

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.