TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Would you do this differently? (http://www.talkphp.com/absolute-beginners/3063-would-you-do-differently.html)

MaxS 07-03-2008 10:43 PM

Would you do this differently?
 
Just threw something together as practice. I'm wondering if you would code this differently. I know it's merely a few lines of code, but I'm sure there are people who can dissect it and code it more eloquently and efficiently than I have. I'm here to learn. Please pick it apart.

PHP Code:

<head>
    <title>Byte Calculator</title>
</head>
<body>

    <?php    
        
// Constants/Variables
        
DEFINE('BITS_IN_A_BYTE'8); // 8 bits in a byte
        
        
function convert($bytes)
        {
            
$bits $bytes BITS_IN_A_BYTE;
            for(
$x 0$x $bits$x++)
            {
                
$number += pow(2$x);
            }
            return 
$number;
        }
        
        if(!empty(
$_GET['bytes']))
        {
            
$number convert($_GET['bytes']);
            echo 
$number;
            exit();
        }
    
?>
    
    <fieldset>
        <legend>Byte Calculator</legend>
        <p>This program will indicate the maximum number a numeric MySQL field can hold as determined by its length in bytes.</p>
        <form method="GET">
            Bytes: <input type="text" size="5" name="bytes" /><br />
            <input type="submit" value="Calculate" />
        </form>
    </fieldset> 
</body>
</html>


MaxS 07-04-2008 05:17 PM

Not one person would have done this differently? Surely it's not perfect code.

Rizza 07-04-2008 05:17 PM

There is a lot of code that you were using unnecessarily. No reason to use a constant, a function and definitely shouldn't be exiting an application for no reason (additionally, if you're using "exit" and don't have a message to send with it, no need to use exit(). :)

PHP Code:

<?php

if ( isset($_GET['bytes']) )
{
    
$bits = (int) $_GET['bytes'] * 8;
    for ( 
$x 0$x $bits$x++ )
    {
        
$number += pow(2$x);
    }
    echo 
$number;
}
?>

That will suit you just fine and is quite shorter.

Tanax 07-04-2008 10:58 PM

Eh, if he wants to stop the script without saying any message, it's not useless to have exit....

Rizza 07-05-2008 06:58 AM

Yeah, while that's right, why would you want to exit before outputting the form again--poor usability.

Tanax 07-05-2008 09:28 PM

Doesn't it only exit the PHP part? The other is just plain HTML, and should be outputted.. but this I'm not sure of.

Rizza 07-05-2008 10:43 PM

Exit stops the current php script. Since he's using procedural and not an object-oriented approach, it will not display anything after the exit. Otherwise you would be correct. Never the less an exit there would be kind of useless since nothing PHP follows it anyway.


All times are GMT. The time now is 08:52 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0