TalkPHP
 
 
Account Login
Latest Articles
» How to keep your forms from double posting data
» cURL Basics
» Securing your PHP applications Part 1
» The way the function rolls
» Database Abstraction with Zend_Db - Part 2
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 07-03-2008, 10:43 PM   #1 (permalink)
The Wanderer
 
Join Date: Apr 2008
Posts: 8
Thanks: 0
MaxS is on a distinguished road
Default 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 is offline  
Reply With Quote
Old 07-04-2008, 05:17 PM   #2 (permalink)
The Wanderer
 
Join Date: Apr 2008
Posts: 8
Thanks: 0
MaxS is on a distinguished road
Default

Not one person would have done this differently? Surely it's not perfect code.
MaxS is offline  
Reply With Quote
Old 07-04-2008, 05:17 PM   #3 (permalink)
The Wanderer
 
Rizza's Avatar
 
Join Date: Dec 2007
Location: Orlando, FL
Posts: 23
Thanks: 0
Rizza is on a distinguished road
Default

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.
Rizza is offline  
Reply With Quote
Old 07-04-2008, 10:58 PM   #4 (permalink)
The Gregarious
Upcoming Programmer Inquisitive 
 
Join Date: Sep 2007
Posts: 540
Thanks: 64
Tanax is on a distinguished road
Default

Eh, if he wants to stop the script without saying any message, it's not useless to have exit....
Tanax is offline  
Reply With Quote
Old 07-05-2008, 06:58 AM   #5 (permalink)
The Wanderer
 
Rizza's Avatar
 
Join Date: Dec 2007
Location: Orlando, FL
Posts: 23
Thanks: 0
Rizza is on a distinguished road
Default

Yeah, while that's right, why would you want to exit before outputting the form again--poor usability.
__________________
HYDRA Studio!
Rizza is offline  
Reply With Quote
Old 07-05-2008, 09:28 PM   #6 (permalink)
The Gregarious
Upcoming Programmer Inquisitive 
 
Join Date: Sep 2007
Posts: 540
Thanks: 64
Tanax is on a distinguished road
Default

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.
Tanax is offline  
Reply With Quote
Old 07-05-2008, 10:43 PM   #7 (permalink)
The Wanderer
 
Rizza's Avatar
 
Join Date: Dec 2007
Location: Orlando, FL
Posts: 23
Thanks: 0
Rizza is on a distinguished road
Default

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.
__________________
HYDRA Studio!
Rizza is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:42 PM.

 
     

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