Solved

Need to create function similar to ISNUMERIC()

Posted on 2007-11-16
9
467 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 125 total points
ID: 20300424
Yes it can.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Industry Leaders: 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!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

724 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