baholash yordamida satrdan matematik ifodani hisoblang

PHP da massivlar va ma'lumotlarni qanday tartiblashim mumkin?
PHP float uchun string

Ushbu yechimda biz matematik ifodalarni satrdan qanday hisoblashni o'rganamiz.

Eval dan foydalanmang, chunki eval faqat parchalarni emas, balki to'liq kod qatorlarini kutadi.

yechim 1.

$ma = "2+10";

if(preg_match('/(\d+)(?:\s*)([\+\-\*\/])(?:\s*)(\d+)/', $ma, $matches) !== FALSE){
    $operator = $matches[2];

    switch($operator){
        case '+':
            $p = $matches[1] + $matches[3];
            break;
        case '-':
            $p = $matches[1] - $matches[3];
            break;
        case '*':
            $p = $matches[1] * $matches[3];
            break;
        case '/':
            $p = $matches[1] / $matches[3];
            break;
    }

    echo $p;
}
PHP da massivlar va ma'lumotlarni qanday tartiblashim mumkin?
PHP float uchun string
yaqin

PHP haqida yangiliklardan xabardor bo'ling!

Biz spam yubormaymiz!

en English
X
Top o'ting