[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 508
  • Last Modified:

Need to create function similar to ISNUMERIC()

Need to make a function that is similar to ISNUMERIC() but does not see numerical signs ( + - * / ) as valid.

Is there such a function in php?

ex - variable $m comes back true if == "12" & comes back false if == "/2"
0
drews77
Asked:
drews77
  • 5
  • 4
1 Solution
 
steelseth12Commented:
function my_numeric($str) {

      if(preg_match("/^\d+(\.\d+)*(e\d+)*$/",$str)) {
      
            return true;
            
      }else{
      
            return false;
      }
}
0
 
drews77Author Commented:
thanks for the function.   Can a function be placed in an IF statement?

ex-

if (my_numeric("$m") && my_numeric("$d")) {
   echo "This will print";  }
0
 
steelseth12Commented:
Yes it can.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
drews77Author Commented:
Fatal error: Cannot redeclare my_numeric() (previously declared in C:\Program Files\xampp\htdocs\convert_date.php:6) in C:\Program Files\xampp\htdocs\convert_date.php on line 6

I'm pulling dates that are incorrectly formated from a database and converting them to yyyy-mm-dd format... however I get an error msg after it try to convert the second date (see above).  Not sure why I cant you a function twice....   Here's what the query looks like:
 
 include("convert_date.php");  // convert date

  $table = "rolist";
  include("db_fnc.php");  // connecting to MYSQL database

  $sql = @mysql_query("SELECT ro, indate, outdate, open FROM $table_name
  WHERE open = 'T'
 ", $db);

  if ($myrow = mysql_fetch_row($sql)) {
 
  do {

  convert_date($myrow['1']);

  } while ($myrow = mysql_fetch_array($sql));

} else { echo "Sorry, no records were found!";      }
0
 
steelseth12Commented:
You can not declare a function more than 1 time ...
I cant tell from your code where you include it twice but you can avoid this kind of error by declaring your function like this

if(!function_exists(my_numeric)) {

      function my_numeric($str) {
      
            if(preg_match("/^\d+(\.\d+)*(e\d+)*$/",$str)) {
            
                  return true;
                  
            }else{
            
                  return false;
            }
      }      
}
0
 
drews77Author Commented:
Each time it pulls a row it runs the date through convert_date() function.  Within the convert_date() is the my_numeric() ....odd that it doesnt give an error with the convert_date function.
0
 
drews77Author Commented:
Looks like I declared a function within a function....  that's it...  
Here is part of the function in a function...

 <?

function convert_date($indate) {


function my_numeric($str) {

      if(preg_match("/^\d+(\.\d+)*(e\d+)*$/",$str)) {

            return true;

      }else{

            return false;
      }
}

function my_numeric_false($str) {

      if(preg_match("/^\d+(\.\d+)*(e\d+)*$/",$str)) {

            return false;

      }else{

            return true;
      }
}
......................
}

thanks
0
 
drews77Author Commented:
Can  I refer to a function in a function?  Having trouble...

Seems like it not recognizing the function my_numeric().  
0
 
steelseth12Commented:
Yes function have the global scope . they can be called inside other function even if they were defined outside.
e.g

a();

function a() {

      print b();

}


function b() {

      return "hello world";
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now