My function isn't returning the value I'm expecting.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

My function isn't returning the value I'm expecting.

Post by Crashin »

Hey all. I've got a form in which EU's can enter the version number of our app that they are currently using. The version numbers are in the form:

1.2.3

On my subsequent processing page I want to make sure the EU entered a numeric-only version number, with the exception of the periods. So, I've got the following function in an include file:

Code: Select all

<?php
function validate_version($version) {
	$versArray = split('[.]', $version);
	$num = count($versArray);
	for ($i=0; $i<$num; $i++) {
		return is_numeric($versArray[$i]);
	}
}
?>
I'm calling the function like so:

Code: Select all

<?php
if (!validate_version($_POST['currentVersion'])) {
	$error_code = "invalid version number";
}
?>
Yet, the processing page is letting anything through. If I enter

1.2.3

it allows it (which is correct). If I enter

1.2.b

it also allows it (which is incorrect). What am I doing wrong?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<?php
function validate_version($version)
{ 
$split = explode(".", $version);
for($x = 0; $x<=count($split); $x++)
{
if(!empty($split[$x]))
{
if(is_numeric($split[$x]))
{
$msg = 1;
}
else
{
$msg = "Not A Number";
}
}
}
return $msg;
}
?>
a quick way of doing it, didn't check, might be a error somewhere :roll:
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

There is a logic error in the function. It returns a value (and exits the function) on the first loop and so it will test true for any string which begins with an integer regardless of the other values.

The revised version posted by qads is getting there but also has a logic error - kind of the reverse of the first fn. $msg is reset on each loop and so, in effect, it only tests the final character in the string.

I didn't test this but should work:

Code: Select all

<?php

// returns true if $version validates successfully, false if not
function validate_version($version) 
{
   $versArray = split('[.]', $version);
   $num = count($versArray);
   $errors = TRUE;
   
   for ($i=0; $i<$num; $i++) 
   {
      if(!is_numeric($versArray[$i]))
      {
          $errors = FALSE;
      }
   }
   return $errors;
} 
?>
Last edited by McGruff on Wed Aug 10, 2005 8:32 pm, edited 1 time in total.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

um..mine works fine McGruff 8O, just tested it.

Code: Select all

<?php
function validate_version($version)
{ 
$split = explode(".", $version);
for($x = 0; $x<=count($split); $x++)
{
if(!empty($split[$x]))
{
if(is_numeric($split[$x]))
{
$msg = 1;
}
else
{
$msg = "Not A Number";
}
}
}
return $msg;
}
echo validate_version($_POST[num]);
?>
<form action="" method="post">
<input type="text" name="num">
<input type="submit" value="Check Value" name="x">
</form>
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

Code: Select all

<?php
function validate_verstion($version){
     if( is_numeric(str_replace(".", "", $version)) && strchr($version, ".") ){
          return true;
     }
      else { return false; }
}
?>
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

Thanks for the replies, everyone. Sorry I didn't get back to the post yesterday. Went to the Jack Johnson/Ben Harper show at Red Rocks...it was AMAZING! :D I'm running on only a couple hours of sleep right now, but it's all good! (I think I'm still feeling the "effects" of the show... :wink: )

Hey qads...your function works partly. If the last character in the entry is not numeric, the function will return the "error" (i.e. "Not A Number"), which is correct. Likewise, if the string is correct the function returns the "true" value (i.e. 1). However, if you enter something like

a.1.2

the function will still return "1", even though there is an "a" in the string.

McGruff's function is working correctly. :) I haven't had time to try/test liljester's, but it looks like it will also do the trick.

Thanks again for your help everyone!
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

:lol: i only tested it with your example before so no wonder its not working :P
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Just one minor point about the fn I posted. This assumes the result is TRUE and only changes the TRUE state if it finds an error. That doesn't really matter in a simple fn like this - the for loop will always run - but in a more complex system (ie a whole bunch of fns or objects all interacting with each other) it's probably safer to work the other way around, ie assume the result is FALSE just in case something doesn't run for some reason.
Post Reply