?
Solved

Need to create function similar to ISNUMERIC()

Posted on 2007-11-16
9
Medium Priority
?
494 Views
Last Modified: 2008-02-01
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
Comment
Question by:drews77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 20300245
function my_numeric($str) {

      if(preg_match("/^\d+(\.\d+)*(e\d+)*$/",$str)) {
      
            return true;
            
      }else{
      
            return false;
      }
}
0
 

Author Comment

by:drews77
ID: 20300391
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
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 20300424
Yes it can.
0
Independent Software Vendors: 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!

 

Author Comment

by:drews77
ID: 20301201
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
 
LVL 20

Expert Comment

by:steelseth12
ID: 20301224
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
 

Author Comment

by:drews77
ID: 20301357
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
 

Author Comment

by:drews77
ID: 20301407
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
 

Author Comment

by:drews77
ID: 20301583
Can  I refer to a function in a function?  Having trouble...

Seems like it not recognizing the function my_numeric().  
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20303499
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question