Notice: Undefined variable: number_of_characters in

Hello,
How can I fix this error? “$number_of_characters += strlen($row[$j]);”

    $s = 0;
    while ($row = $result->fetch_row()) {

        $s++;
        $return .= '(';
        for ($j = 0; $j < $num_fields; $j++) {

            $row[$j] = addslashes($row[$j]);

            $row[$j] = str_replace(array("\n","\r"),array('\\n','\\r'),$row[$j]);

            $number_of_characters += strlen($row[$j]);

            if (!empty($row[$j]) OR $row[$j] == "0") {

            if  (preg_match($virgullu,$row[$j])) { // 12.1234 ve 0.000 
                $return .= "'" . $row[$j] . "'";
            } else if (preg_match($tamsayi,$row[$j])) {  // 123456 
                $return .= "" . $row[$j] . "";
            } else  {  // metin
                $return .= "'" . $row[$j] . "'";
            }
            
            } else { // Veri olmayan sütunlar
                
                if (is_array($null_array) && array_key_exists(( $j + 1 ), $null_array) && "YES" == $null_array[( $j + 1 )]){  // Veri olmayan sütunlarda DEFAULT NULL mu değil mi
                $return .= "NULL";
                }else{
                $return .= "''";
                }
                
            }
            if ($j < ($num_fields - 1)) {
                $return .= ', ';
            }               
        }

Set number_of_characters to 0 before the loop.

    $number_of_characters = "0";
    for ($j = 0; $j < $num_fields; $j++) {

Thank you, the problem has improved

Same error, how can I fix it? “$round”

function showSize($size_in_bytes) {
    $value = 0;        
    if ($size_in_bytes >= 1073741824) {
        $value = round($size_in_bytes/1073741824*10)/10;
        return  ($round) ? round($value) . 'GB' : "{$value} GB";
    } else if ($size_in_bytes >= 1048576) {
        $value = round($size_in_bytes/1048576*10)/10;
        return  ($round) ? round($value) . 'MB' : "{$value} MB";
    } else if ($size_in_bytes >= 1024) {
        $value = round($size_in_bytes/1024*10)/10;
        return  ($round) ? round($value) . 'KB' : "{$value} KB";
    } else {
        return "$size_in_bytes Bayt";
    }
}

The code is just wrong, there is no $round variable defined anywhere so it makes no sense to check on it.

I’m beginner, one row sample?

What are you attempting to do here?

return  ($round) ? round($value) . 'GB' : "{$value} GB";

I didn’t write the code
I bought it from the Internet
How can I fix this wrong code?

function showSize($size_in_bytes) {
    if ($size_in_bytes >= 1073741824) {
        $size_in_bytes = number_format($size_in_bytes / 1073741824, 2) . ' GB';
    } elseif ($size_in_bytes >= 1048576) {
        $size_in_bytes = number_format($size_in_bytes / 1048576, 2) . ' MB';
    } elseif ($size_in_bytes >= 1024) {
        $size_in_bytes = number_format($size_in_bytes / 1024, 2) . ' KB';
    } elseif ($size_in_bytes > 1) {
        $size_in_bytes = $size_in_bytes . ' Bayt';
    } elseif ($size_in_bytes == 1) {
        $size_in_bytes = $size_in_bytes . ' Bayt';
    } else {
        $size_in_bytes = '0 Bayt';
    }
    return $size_in_bytes;
}

It works smoothly

Here’s another example of the same function

function formatBytes($bytes, $precision = 2) { 
    $units = array('Bayt', 'KB', 'MB', 'GB', 'TB'); 

    $bytes = max($bytes, 0); 
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
    $pow = min($pow, count($units) - 1); 

    $bytes /= pow(1024, $pow);

    return round($bytes, $precision) . ' ' . $units[$pow]; 
}
2 Likes
Sponsor our Newsletter | Privacy Policy | Terms of Service