HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

PHP and Custom Functions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
andphpfunctionscustom

Problem

I am new to the world of coding as well as XHTML and PHP. As a way to gain more experience as well as to put theory into practice including a way to challenge myself, I put together the following code that calculates values a user enters into a form.

This was to test my knowledge regarding PHP's own functions but also creating custom functions. I am sure the code can be improved and would appreciate any advise.

The one thing I was unable to do was to only display the result and to hide the form once it has been posted preferably on the same page i.e. not redirecting the user to another page.

I have also tried to keep business logic and presentation as much as possible.

Appreciate any advise as well as constructive criticism.

Main Page Code - Calculator.php


    Calculator

    Calculator Instructions
    Below are instructions on using the calculator
    
        You must specify 2 values in the fields below
        You must select an arithmetic operator which includes the list below
            
                Multiply
                Divide
                Modulus
                Add
                Subtract
            
        
        Simply submit the form once you have specified 2 values and selected arithmetic operator
    

    Calculator

    

    

    

    
        
            Field 1: 
            " />
        

        
            Field 2: 
            " />
        

        

            Select an arithmetic operator: 
            
                Please select an option

                

            
        

        

        

        
            
            
        
    


Define and Set directory and path - Base.php



Business Logic - Functions.php

```
$selectvalue) {

if($_POST['field3'] == $selectvalue) {

$status = 'selected';
}

else {

$status = '';
}

echo ''.$selectvalue.'';
}
}

//Validate if form has been submitted
if(isset($_POST['su

Solution

This is going to have a lot of extensibility concerns. What if I want to add exponentials or scientific notation? What if I want to chain operations? What if I want to ensure someone doesn't put unsafe code that could bork my webserver in the POST variable?

https://stackoverflow.com/questions/2401706/where-to-sanitize-php-post-input

All that said, I'm impressed by how good a job you did of separating your presentation from your business logic. I've seen a lot uglier PHP. The main page code is nicely mainly HTML, and the business logic is separated out.

From here, POA:

  • Refactor for an Object Oriented Approach



  • Sanitize User input



-
Consider breaking apart pieces of the UI into separate chunks that are added dynamically, which reduced the maintenance burden. For example, you may want to put

Select an arithmetic operator: 
    
        Please select an option

        

    


In an operations.php that you include. This allows for easy modification/adding of stuff later.

Also, you'll probably want to look into AJAX.

Code Snippets

Select an arithmetic operator: <br />
    <select name="field3">
        <option value="">Please select an option</option>

        <?php

        echo myselect();

        ?>

    </select>
</div>

Context

StackExchange Code Review Q#2097, answer score: 2

Revisions (0)

No revisions yet.