Project Store Project Store
  • HOME
  • BLOG
  • PROJECTS
    • PHP
    • CodeIgniter
    • Premium PHP
    • Advanced PHP
    • Laravel
  • FREE PROJECTS
  • Tutorial/ Project Request
  • SHOP
    • Product Store
    • My account
    • Checkout
    • Cart
0
CONTACT
Project Store
  • HOME
  • BLOG
  • PROJECTS
    • PHP
    • CodeIgniter
    • Premium PHP
    • Advanced PHP
    • Laravel
  • FREE PROJECTS
  • Tutorial/ Project Request
  • SHOP
    • Product Store
    • My account
    • Checkout
    • Cart
find with me
PHP
40

How to create a calculator in PHP

How to create a calculator in PHP

LIKE THIS 40

In this tutorial, we will learn How to create a calculator in PHP. In calculator, we will perform simple operations (add, subtract, multiply, divide) with two numbers.

The HTML Code

<div class="signup-form">
    <form  method="post">
		<h2>Calculator</h2>
		<p>Developed using PHP</p>
		<hr>
 <div class="form-group">
 
<div class="row">
ass="col"><input type="text" class="form-control" name="first_number" placeholder="First Number" required="required" value="<?php echo isset($fn)?$fn:''; ?>" pattern="[0-9]+" title="only numbers"></div>
 
<div class="col"><input type="text" class="form-control" name="second_number" placeholder="Second Number" required="required" value="<?php echo isset($sn)?$sn:''; ?>" pattern="[0-9]+" title="only numbers"></div>
 
</div>        	
</div>
 
<div class="form-group">
<?php if(isset($result) && is_numeric($result)){?>
<h4 style="color:blue; font-weight:bold;"> Result: <?php echo $result; ?></h4>
<?php } if(isset($error)){?>
<h5 style="color:red; font-weight:bold;">Error: <?php echo $error; ?></h5>
<?php } ?>
</div>
 
<div class="form-group">
<input class="btn btn-primary" type="submit" name="calculate" value="+">
<input class="btn btn-primary" type="submit" name="calculate" value="-">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="calculate" value="x">
<input class="btn btn-primary" type="submit" name="calculate" value="/">
        </div>
    </form>
</div>

PHP Code to perform calculator operations

<?php
if (isset($_POST['Calculate'])) {
	$fn=$_POST['first_number'];
	$sn=$_POST['second_number'];
	$operation=$_POST['calculate'];

	if($operation=="/" && ($fn == 0 || $sn == 0)){
		$error = "Never divide any number by zero";
	}
	else{
		if($operation=="+")
			$result=$fn+$sn;
		else if($operation=="-")
			$result=$fn -$sn;
		else if($operation=="x")
			$result=$fn*$sn;
		else if($operation=="/")
			$result=$fn/$sn;
	}
} ?>

Here is the full code that we have written for this calculator

<?php
if (isset($_POST['Calculate'])) {
	$fn=$_POST['first_number'];
	$sn=$_POST['second_number'];
	$operation=$_POST['calculate'];
 
	if($operation=="/" && ($fn == 0 || $sn == 0)){
		$error = "Never divide any number by zero";
	}
	else{
		if($operation=="+")
			$result=$fn+$sn;
		else if($operation=="-")
			$result=$fn -$sn;
		else if($operation=="x")
			$result=$fn*$sn;
		else if($operation=="/")
			$result=$fn/$sn;
	}
} ?>
 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:400,700">
<title>Calculator in PHP</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script> 
<style>
body {
	color: #fff;
	background: #3598dc;
	font-family: 'Roboto', sans-serif;
}
.form-control {
	height: 41px;
	background: #f2f2f2;
	box-shadow: none !important;
	border: none;
}
.form-control:focus {
	background: #e2e2e2;
}
.form-control, .btn {        
	border-radius: 3px;
}
.signup-form {
	width: 600px;
	margin: 30px auto;
}
.signup-form form {
	color: #999;
	border-radius: 3px;
	margin-bottom: 15px;
	background: #fff;
	box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
	padding: 30px;
}
.signup-form h2  {
	color: #333;
	font-weight: bold;
	margin-top: 0;
}
.signup-form hr  {
	margin: 0 -30px 20px;
}    
.signup-form .form-group {
	margin-bottom: 20px;
}
.signup-form input[type="checkbox"] {
	margin-top: 3px;
}
.signup-form .row div:first-child {
	padding-right: 10px;
}
.signup-form .row div:last-child {
	padding-left: 10px;
}
.signup-form .btn {        
	font-size: 16px;
	font-weight: bold;
	background: #3598dc;
	border: none;
	min-width: 140px;
}
.signup-form .btn:hover, .signup-form .btn:focus {
	background: #2389cd !important;
	outline: none;
}
.signup-form a {
	color: #fff;
	text-decoration: underline;
}
.signup-form a:hover {
	text-decoration: none;
}
.signup-form form a {
	color: #3598dc;
	text-decoration: none;
}	
.signup-form form a:hover {
	text-decoration: underline;
}
.signup-form .hint-text  {
	padding-bottom: 15px;
	text-align: center;
}
</style>
</head>
<body>
<div class="signup-form">
    <form  method="post">
		<h2>Calculator</h2>
		<p>Developed using PHP</p>
		<hr>
        <div class="form-group">
 
<div class="row">
<div class="col"><input type="text" class="form-control" name="first_number" placeholder="First Number" required="required" value="<?php echo isset($fn)?$fn:''; ?>" pattern="[0-9]+" title="only numbers"></div>
				
<div class="col"><input type="text" class="form-control" name="second_number" placeholder="Second Number" required="required" value="<?php echo isset($sn)?$sn:''; ?>" pattern="[0-9]+" title="only numbers"></div>
</div>        	
</div>
 
<div class="form-group">
 
<?php if(isset($result) && is_numeric($result)){?>
 <h4 style="color:blue; font-weight:bold;">Result: <?php echo $result; ?></h4>
 <?php } if(isset($error)){?>
 <h5 style="color:red; font-weight:bold;"> 	Error: <?php echo $error; ?></h5>
 <?php } ?>
 </div>
		
<div class="form-group">
 <input class="btn btn-primary" type="submit" name="calculate" value="+">
<input class="btn btn-primary" type="submit" name="calculate" value="-">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="calculate" value="x">
<input class="btn btn-primary" type="submit" name="calculate" value="/">
        </div>
    </form>
</div>
</body>
</html>
C
33

Car Game in C Program

Car Game in C Program

LIKE THIS 33

Text Based Car Racing Game

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define TRACK_LENGTH 20

void print_track(int player_position, int opponent_position);
int get_move();

int main() {
    srand(time(NULL));
    int player_position = 0;
    int opponent_position = 0;
    printf("Welcome to the Text-based Car Racing Game!\n");
    printf("Your car: [P]\nOpponent car: [O]\n");
    while (1) {
        print_track(player_position, opponent_position);
        int move = get_move();
        player_position += move;
        opponent_position += rand() % 3 + 1; // opponent moves randomly
        if (player_position >= TRACK_LENGTH) {
            printf("You win!\n");
            break;
        }
        else if (opponent_position >= TRACK_LENGTH) {
            printf("You lose!\n");
            break;
        }
    }
    return 0;
}

void print_track(int player_position, int opponent_position) {
    printf("\n");
    for (int i = 0; i < TRACK_LENGTH; i++) {
        if (i == player_position) {
            printf("[P]");
        }
        else if (i == opponent_position) {
            printf("[O]");
        }
        else {
            printf("[ ]");
        }
    }
    printf("\n");
}

int get_move() {
    int move;
    while (1) {
        printf("Enter a move (1-3): ");
        scanf("%d", &move);
        if (move >= 1 && move <= 3) {
            break;
        }
        printf("Invalid move. Please enter a move between 1 and 3.\n");
    }
    return move;
}
C
34

Rock-Paper-Scissor in C

Rock-Paper-Scissor in C

LIKE THIS 34

Cli Based Rock Paper Scissor Game


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROCK 1
#define PAPER 2
#define SCISSORS 3

int main() {
    int playerScore = 0;
    int computerScore = 0;
    int rounds = 0;

    printf("Welcome to Rock-Paper-Scissors game!\n\n");

    while(1) {
        int playerMove;
        printf("Enter your move (1 = Rock, 2 = Paper, 3 = Scissors, 0 = Quit): ");
        scanf("%d", &playerMove);

        if (playerMove == 0) {
            break;
        }

        if (playerMove < 1 || playerMove > 3) {
            printf("Invalid move! Please try again.\n");
            continue;
        }

        int computerMove = rand() % 3 + 1;

        printf("You played: ");
        switch(playerMove) {
            case ROCK:
                printf("Rock\n");
                break;
            case PAPER:
                printf("Paper\n");
                break;
            case SCISSORS:
                printf("Scissors\n");
                break;
        }

        printf("Computer played: ");
        switch(computerMove) {
            case ROCK:
                printf("Rock\n");
                break;
            case PAPER:
                printf("Paper\n");
                break;
            case SCISSORS:
                printf("Scissors\n");
                break;
        }

        if (playerMove == computerMove) {
            printf("It's a tie!\n");
        } else if (playerMove == ROCK && computerMove == SCISSORS ||
                   playerMove == PAPER && computerMove == ROCK ||
                   playerMove == SCISSORS && computerMove == PAPER) {
            printf("You win this round!\n");
            playerScore++;
        } else {
            printf("Computer wins this round!\n");
            computerScore++;
        }

        rounds++;
        printf("Current score: You %d - %d Computer\n\n", playerScore, computerScore);
    }

    printf("\nFinal score: You %d - %d Computer\n", playerScore, computerScore);
    printf("Total rounds played: %d\n", rounds);

    return 0;
}

Cli Based Rock Paper Scissor Game

HTML
33

Login Form with Light Button

Login Form with Light Button

LIKE THIS 33

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
  
<div class="login-box">
  <h2>Login</h2>
  <form>
    <div class="user-box">
      <input type="text" name="" required="">
      <label>Username</label>
    </div>
    <div class="user-box">
      <input type="password" name="" required="">
      <label>Password</label>
    </div>
    <a href="#">
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      Submit
    </a>
  </form>
</div>
  </body>

</html>

style.css

html {
  height: 100%;
}
body {
  margin:0;
  padding:0;
  font-family: sans-serif;
  background: linear-gradient(#141e30, #243b55);
}

.login-box {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 400px;
  padding: 40px;
  transform: translate(-50%, -50%);
  background: rgba(0,0,0,.5);
  box-sizing: border-box;
  box-shadow: 0 15px 25px rgba(0,0,0,.6);
  border-radius: 10px;
}

.login-box h2 {
  margin: 0 0 30px;
  padding: 0;
  color: #fff;
  text-align: center;
}

.login-box .user-box {
  position: relative;
}

.login-box .user-box input {
  width: 100%;
  padding: 10px 0;
  font-size: 16px;
  color: #fff;
  margin-bottom: 30px;
  border: none;
  border-bottom: 1px solid #fff;
  outline: none;
  background: transparent;
}
.login-box .user-box label {
  position: absolute;
  top:0;
  left: 0;
  padding: 10px 0;
  font-size: 16px;
  color: #fff;
  pointer-events: none;
  transition: .5s;
}

.login-box .user-box input:focus ~ label,
.login-box .user-box input:valid ~ label {
  top: -20px;
  left: 0;
  color: #03e9f4;
  font-size: 12px;
}

.login-box form a {
  position: relative;
  display: inline-block;
  padding: 10px 20px;
  color: #03e9f4;
  font-size: 16px;
  text-decoration: none;
  text-transform: uppercase;
  overflow: hidden;
  transition: .5s;
  margin-top: 40px;
  letter-spacing: 4px
}

.login-box a:hover {
  background: #03e9f4;
  color: #fff;
  border-radius: 5px;
  box-shadow: 0 0 5px #03e9f4,
              0 0 25px #03e9f4,
              0 0 50px #03e9f4,
              0 0 100px #03e9f4;
}

.login-box a span {
  position: absolute;
  display: block;
}

.login-box a span:nth-child(1) {
  top: 0;
  left: -100%;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, transparent, #03e9f4);
  animation: btn-anim1 1s linear infinite;
}

@keyframes btn-anim1 {
  0% {
    left: -100%;
  }
  50%,100% {
    left: 100%;
  }
}

.login-box a span:nth-child(2) {
  top: -100%;
  right: 0;
  width: 2px;
  height: 100%;
  background: linear-gradient(180deg, transparent, #03e9f4);
  animation: btn-anim2 1s linear infinite;
  animation-delay: .25s
}

@keyframes btn-anim2 {
  0% {
    top: -100%;
  }
  50%,100% {
    top: 100%;
  }
}

.login-box a span:nth-child(3) {
  bottom: 0;
  right: -100%;
  width: 100%;
  height: 2px;
  background: linear-gradient(270deg, transparent, #03e9f4);
  animation: btn-anim3 1s linear infinite;
  animation-delay: .5s
}

@keyframes btn-anim3 {
  0% {
    right: -100%;
  }
  50%,100% {
    right: 100%;
  }
}

.login-box a span:nth-child(4) {
  bottom: -100%;
  left: 0;
  width: 2px;
  height: 100%;
  background: linear-gradient(360deg, transparent, #03e9f4);
  animation: btn-anim4 1s linear infinite;
  animation-delay: .75s
}

@keyframes btn-anim4 {
  0% {
    bottom: -100%;
  }
  50%,100% {
    bottom: 100%;
  }
}
See More
Quick Link
  • Services
  • Need Help
  • Blog
  • Refund and Returns Policy
  • Terms of Service
  • Contact
Paid Projects
  • PHP
  • CodeIgniter
  • Advanced PHP
  • Laravel
Free Projects
  • PHP
  • C
  • HTML

© 2025. All rights reserved by Leo Gilson