PHP Operatör ile Dört İşlem

R

Rooster

only business
Super Moderator
Joined
Aug 10, 2019
Messages
3,215
Reaction score
4,728
Location
Milano
Klavyeden iki sayı ve bir operatör değeri girilerek işlemleri gerçekleştiriyoruz.


Tasarımsal olarak çok iyi olmasada radio butonların işaretlerini gizleyip yazılara tıklayınca hangi işlemi yapacağımızı seçmek için biraz CSS düzenlemesi yaptım. Ayrıca PHP kodları için ayrı bir sayfaya göndermek yerine SELF olarak sayfa içinde yönlendirdim.


PHP Kodu:

Code:
<?php
$sayi1      =   $_POST['sayi1'] ?? '';
$sayi2      =    $_POST['sayi2'] ?? '';
$op         =   $_POST['op'] ?? '';
$sonuc      =   0;
 
if(isset($_POST['islem-yap']))
{
    if($op=='+')
        $sonuc  =   $sayi1 + $sayi2;
    else if($op=='-')
        $sonuc  =   $sayi1 - $sayi2;
    else if($op=='*')
        $sonuc  =   $sayi1 * $sayi2;
    else if($op=='/')
        $sonuc  =   $sayi1 / $sayi2;
}
 
?>
 
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>TasarimKodlama</title>
    <style>
    label{
        display:block;
    }
 
    label input[type='radio']{
        display:none;
    }
    label input[type='radio']+span{
        color:grey;
    }
    label input[type='radio']:checked+span{
        color:green;
        font-weight:bold;
    }
    </style>
</head>
<body>
    <form method="post" action="<?=$_SERVER['PHP_SELF']?>">
    <label for="sayi1">Sayı 1:</label>
    <input name="sayi1" type="text" value="<?=$sayi1?>">
    <label for="sayi2">Sayı 1:</label>
    <input name="sayi2" type="text" value="<?=$sayi2?>">
    <label>
        <input type="radio" name="op" value="+" <?=$op=='+'?'checked':''?>><span>TOPLA</span>
    </label>
    <label>
        <input type="radio" name="op" value="-" <?=$op=='-'?'checked':''?>><span>ÇIKAR</span>
    </label>
    <label>
        <input type="radio" name="op" value="*" <?=$op=='*'?'checked':''?>><span>ÇARP</span>
    </label>
    <label>
        <input type="radio" name="op" value="/" <?=$op=='/'?'checked':''?>><span>BÖL</span>
    </label>
    <button type="submit" name="islem-yap">İşlem Yap</button>
    </form>
    <h2>İşlem Sonucu: <?=$sonuc?></h2>
</body>
</html>
 
SPAM IS FORBIDDEN!
  • SPAMMERS ARE BANNED FROM THE FORUM AND CANNOT USE ANY OF THE CHEATS
  • For example: thanks, thx, very good, asdqwe, working, ty and so on!
  • For example: Writing the same message over and over. thanks, thx and so on!
  • Copying and copying someone else's message is prohibited.
  • It is forbidden to send messages to increase the number of comments on threads that you have no knowledge of.
  • Write your own opinion when commenting!
  • If you see spam message, please let us know with the REPORT button!

Tema düzenleyici

Top Bottom