Current location: Hot Scripts Forums » Programming Languages » PHP » Is this correct


Is this correct

Reply
  #1 (permalink)  
Old 02-01-07, 06:00 PM
_matt _matt is offline
Newbie Coder
 
Join Date: Oct 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Is this correct

I am learning PHP and made this little form to see if I am actually using the code correctly. It works but is it correct?
PHP Code:

<form action="index.php" method="post">

<input type="text" value="" name="age" />
<input name="submit" type="submit" value="submit" /></form>
<?PHP
$age 
$_POST['age'];
if(
$age == ""){
    echo 
"Enter a age to test";
    }elseif(
$age >20){
    echo 
"$age is greater then 20";
    }elseif (
$age <20){
    echo 
"$age is less then 20";
    }
?>
Reply With Quote
  #2 (permalink)  
Old 02-01-07, 06:07 PM
bizzar528's Avatar
bizzar528 bizzar528 is offline
Community Liaison
 
Join Date: Sep 2004
Location: Pennsylvania, US
Posts: 1,550
Thanks: 2
Thanked 16 Times in 15 Posts
It is correct, but in a more complicated script, that won't work well.
I would start by making the form one file, and the php processing another. Yes, you can do it all in one file, but for learning, it's probably best to split them up. Make the form index.php and move the php to it's own file. Let's call it process.php. Then, change the action in your form to "process.php".

Also, there should be an else. What if I entered some word. Like "blah" into your form. blah isn't < or > 20, so what should the form do with it. Once again, this doesn't matter in a simple script, but when you learn more advanced functions, you'll be happy you took the time to deal with whatifs.
Reply With Quote
  #3 (permalink)  
Old 02-01-07, 06:58 PM
_matt _matt is offline
Newbie Coder
 
Join Date: Oct 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Agreed with using 2 files. THis is just for testing. But for restricting a-z and only allowing numbers, does this look correct?
PHP Code:

<form action="index.php" method="post">

  <input type="text" value="" name="age" />
  <input name="submit" type="submit" value="submit" />
</form>
<?PHP
$age 
$_POST['age'];
function 
check_age($age){
  if(!
preg_match("/[^0-9]+$/s",$age))
  return 
TRUE;
  else
  return 
FALSE;
}
if(!
check_age($age))
{
  echo 
"Please only enter numbers";
  
$error++; 
}
if(
$error == 0)
if(
$age == ""){
    echo 
"Enter a age to test";
    }elseif(
$age >20){
    echo 
"$age is greater then 20";
    }elseif (
$age <20){
    echo 
"$age is less then 20";
}
?>

Last edited by _matt; 02-01-07 at 07:15 PM.
Reply With Quote
  #4 (permalink)  
Old 02-01-07, 07:21 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
I'd use:

PHP Code:

if(preg_match("/^[0-9]+$/"$age)) 

Reply With Quote
  #5 (permalink)  
Old 02-01-07, 07:52 PM
_matt _matt is offline
Newbie Coder
 
Join Date: Oct 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Nico,

Why remove the
PHP Code:


I thought
PHP Code:

! = not or null 

Reply With Quote
  #6 (permalink)  
Old 02-01-07, 09:12 PM
grafman grafman is offline
Coding Addict
 
Join Date: Dec 2006
Posts: 278
Thanks: 0
Thanked 0 Times in 0 Posts
_matt,

bizzar528 makes a very good statement about handling the default condition. You can use switch statements as well to reduce the complexity that you get with lots of "if" statements.

If you find an exception either die, return, or exit right away and don't risk continued execution. If you have a function or class method that gives a return, ALWAYS handle the return. A little more code, but it saves hunting for strange results.

Don't forget to handle the condition where age is exactly 20. You could change one of the not equal cases to >= or <= as well as have the equivalence comparison.

Nico,

don't forget the perl regex predefined sequences! \d must be numeric \must be alpha.

PHP Code:



<?PHP

$age 
$_POST['age'];
// validate that your varialbe is set before
// passing it to your function.  Probably this
// would be validated on the browser side with javascript
if($age)
  
check_age($age);
else
  print(
"Enter an age to process");

function 
check_age($age)
{
  
// checks to see if there are any alpha 
  // characters in string
  
if(preg_match('/\D/',$age))
    die(
"digits only");

  
// another way to check for digits
  // will work with decimals if you cast age
  
if(preg_match('/\d/',$age))
  {
    if(
$age 20)
      print 
"Age is greater than 20";
    elseif(
$age == 20)
      print 
"Age is 20";
    else
      print 
"Age is less than 20";
  }
  else
  {
    print 
"Should never get here but its nice to have";
  }
}
?
__________________
"Things are difficult only while you don't understand them."
Reply With Quote
  #7 (permalink)  
Old 02-02-07, 03:04 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
the fastest way to check if the string only contains digits is using the ctype_digit function, like this:
PHP Code:

if(ctype_digit($_POST['age'])){

// it's a digit
}else{
// it's something else

UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks


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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to display correct time according to user timezone anhhuan ASP 2 09-25-06 03:22 AM
idle screen sigin not accepting my correct password crmpicco The Lounge 1 11-16-05 05:31 AM
New programmer here... is this program correct? kml21panther C/C++ 3 01-30-04 11:51 PM
form doesn't submit to mysql correct values, just 1's in every row HasansWeb PHP 1 01-12-04 07:40 AM
is this correct? darkcarnival PHP 6 01-02-04 09:48 AM


All times are GMT -5. The time now is 05:56 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.