Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
Pages: [1]
Send this topic | Print
Topic: Variable question  (Read 1464 times)
« on: March 11, 2005, 06:55:46 PM »
jojonaloha Offline
Junior Member
Newbie

View Profile WWW
*
Posts: 38



I have a question about how to test whether a variable (or string) has letters in it. I want to make this if statement so only a numerical value will "pass the test"
<?php
$article
=$_GET["article"];
$test=$article;
//$tarticles is grabbed from text file.
if($test<|| $test>$tarticles || $test=="")
{
$article="articles";
}; 
?>
So when it is a value that is less than zero or greater then $tarticles or when there is no value set, it will set $article, so that it returns the list of articles instead of a certain article. Right now the if function works for everything, except if the value contains a number first and then any combination of letters or numbers. If it is all numbers or a letter first then whatever, it works. Just not when there is a number first. So, my question is... what can I add to make sure there are no letters in it?
Report to moderator   Logged

 
« Reply #1 on: March 11, 2005, 07:12:13 PM »
adragons
Guest

a quick example:
<?
$string = "123";
if(!is_numeric($string)){             //  test to see if its numeric
   echo "it is a alphanumeric.";   //if it isn't (eg "abs" "a12A" "foobar")
}else{
   echo $string;                   //if it is
}
?>
Any thing with letters in it will return as false.
whole numbers / intigers return as true.

0.5 = true BUT .5 = false
Report to moderator   Logged
« Reply #2 on: March 11, 2005, 08:37:30 PM »
jojonaloha Offline
Junior Member
Newbie

View Profile WWW
*
Posts: 38



Cool! Thanks I added that and a couple more and it works just like I want. Cheesy
Report to moderator   Logged
 
Pages: [1]
Send this topic | Print
Jump to:  

Powered by SMF | SMF © 2006-2008, Simple Machines LLC