473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Alpha/Numeric problem.

I'm having a problem with numbers. I'm getting a number from an array that
seems to have a padded space at the beginning.

IE: " 5"

What I've done was $var = ltrim($var) which removes the space but with one
problem. Now I think PHP thinks it's a sting and not a number.

Then I try $var = settype($var, "integer") to see if it will become a
numeric for addition purposes.

Next problem... $var is now 0 and gettype($var) shows its and integer.

Why would it be 0 and not 5? Perhaps there's an obvious starring right at
me and I'm not seeing it?

- Francis
Dec 4 '06 #1
3 2010
Oops... I made a typo (not a mistake in my code):

"Then I try $var = settype($var, "integer") to see if it will become a "

should have been:

"Then I try settype($var, "integer") to see if it will become a "
^
(This is what I have actually coded and should have typed in my post.)
Dec 4 '06 #2
..oO(Promextheus Xex)
>I'm having a problem with numbers. I'm getting a number from an array that
seems to have a padded space at the beginning.

IE: " 5"

What I've done was $var = ltrim($var) which removes the space but with one
problem. Now I think PHP thinks it's a sting and not a number.

Then I try $var = settype($var, "integer") to see if it will become a
numeric for addition purposes.
Usually you don't have to explictly set a type. Just use the variable
as-is (even if there's leading white space, it shouldn't matter), PHP
will automatically convert it when necessary.

$var = ' 5';
print $var+10;

outputs 15. If you still want to do an explicit typecast, just write

$var = (int)$var;

That's enough. There's need for ltrim() and settype() in this case.
>Next problem... $var is now 0 and gettype($var) shows its and integer.

Why would it be 0 and not 5? Perhaps there's an obvious starring right at
me and I'm not seeing it?
There's definitely something wrong, if you get a 0. What version of PHP
do you use?

Micha
Dec 4 '06 #3
Message-ID: <45**********************@news.astraweb.comfrom
Promextheus Xex contained the following:
>I'm having a problem with numbers. I'm getting a number from an array that
seems to have a padded space at the beginning.

IE: " 5"

What I've done was $var = ltrim($var) which removes the space but with one
problem. Now I think PHP thinks it's a sting and not a number.
PHP has loose typing and can easily deal with that.

e.g.

$string1 = " 5";
$string2 = 2;
echo $string1+$string2;

//outputs 7

Your problem is elsewhere.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Dec 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: entoone | last post by:
I have a field called pword, whenever someone enters anything but numeric, i.e. mixed alpha with numeric, or even just alpha.. the following error appears. Warning: mysql_numrows(): supplied...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
3
by: success_ny | last post by:
Does anyone have a code snippet to compare those values so I can sort the array of alpha-numeric values that include both characters and integers in it? I.e., if we have values like 4236 and...
8
by: MLH | last post by:
I have a textbox on a form into which an alpha-numeric string of data is entered. I wish to force the casual user, who would sometimes use upper case, sometimes not and sometimes MIX the case -...
7
by: Peter | last post by:
Any one have a code snippet that would show me how to: 1. Generate a 24 character 2. Random 3. Alpha Numeric data dump 4. into an array IE.
5
by: Bosconian | last post by:
I need a comma delimited regular expression pattern with the followng restrictions: no leading and trailing white space no trailing comma double quoted numeric/alpha pairs each pair on a...
8
by: .Net Sports | last post by:
I am checking for text input on a form validation in javascript that required at least one numeric character along with any number of alpha characters for a given input text box. The below is a var...
1
by: shijith | last post by:
Hi, I am using MS-SQL 2000. I had a varchar column with data formated as Numeric portion + Alpha portion. Id ---- 1 2AA 2DF 2AB
5
lotus18
by: lotus18 | last post by:
Hello World! I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters. Public Enum MyOption Alpha = 1 ...
1
by: perl9user | last post by:
Hi, Does anyone know how to get the alpha-numeric numbering scheme in perl ? For example the numberic => 0,1,2,...9,10,11,12,...99,100,... # $x = 0;$x++; lower-alpha =>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.