473,756 Members | 7,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

WTF? String = Integer in PHP? Strlen() in integers WORKS? HELP!

I'm involved in a rather nasty debate involving a strange issue
(whereby the exasperated tell me to RTFM even after my having done so),
where this is insanely possible:

[PHP]
print_r(is_int( '1')); // PRINTS NOTHING
print_r(strlen( (int)1)); // PRINTS '1'
[/PHP]

Now I understand that in PHP, everything scalar is a string and can
take on the role of an integer, or a boolean or.. whatever it's
configured to look like. Why is it that I'm so "way off" in this
thread, when it seems to me that I have it right based on my basic
understanding the definition of "types"?

Phil

http://phpbuilder.com/board/showthread.php?t=10316949

Feb 3 '06 #1
6 7001
d
"comp.lang. php" <ph************ **@gmail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
I'm involved in a rather nasty debate involving a strange issue
(whereby the exasperated tell me to RTFM even after my having done so),
where this is insanely possible:

[PHP]
print_r(is_int( '1')); // PRINTS NOTHING
print_r(strlen( (int)1)); // PRINTS '1'
[/PHP]

Now I understand that in PHP, everything scalar is a string and can
take on the role of an integer, or a boolean or.. whatever it's
configured to look like. Why is it that I'm so "way off" in this
thread, when it seems to me that I have it right based on my basic
understanding the definition of "types"?

Phil

http://phpbuilder.com/board/showthread.php?t=10316949


If you try that with var_dump, you get a false and a 1, which is what you'd
expect... Am I missing something here?? :)
Feb 3 '06 #2
comp.lang.php wrote:
I'm involved in a rather nasty debate involving a strange issue
(whereby the exasperated tell me to RTFM even after my having done so),
where this is insanely possible:

[PHP]
print_r(is_int( '1')); // PRINTS NOTHING
print_r(strlen( (int)1)); // PRINTS '1'
[/PHP]

Now I understand that in PHP, everything scalar is a string and can
take on the role of an integer, or a boolean or.. whatever it's
configured to look like. Why is it that I'm so "way off" in this
thread, when it seems to me that I have it right based on my basic
understanding the definition of "types"?

Phil

http://phpbuilder.com/board/showthread.php?t=10316949


strlen() expects a string as a parameter. because of the "typeless"
nature of PHP, when something other than a string is sent, PHP will
automatically "cast" it to a string (like using the strval() function).
Therefore, when the length is calculated, it is operating on the
equivalent to $int = strval($int) instead of the actual integer.

The same type of thing happens when using expressions:

('1' == TRUE) will result in a bool(true). This is because the lowest
common type between string and bool is bool (the simplest of all).
Therefore, the string is cast to a boolean val, then the comparison is made.

Consider var_dump('my string'==1). This returns bool(false) because the
string is cast to an integer (the simplest common type) which is 0.

In constrast, var_dump('1 string'==1) return bool(true) because the
intval('1 string') === 1
The type Juggling section of the manual is about the best reference you
can use for this kind of thing, but IMO it isn't nearly as complete as
it could be for a novice level explanation.

http://us2.php.net/manual/en/languag...e-juggling.php

--
Justin Koivisto, ZCE - ju****@koivi.co m
http://koivi.com
Feb 3 '06 #3
On 3 Feb 2006 09:03:34 -0800, "comp.lang. php"
<ph************ **@gmail.com> wrote:
I'm involved in a rather nasty debate involving a strange issue
(whereby the exasperated tell me to RTFM even after my having done so),
where this is insanely possible:

[PHP]
print_r(is_int ('1')); // PRINTS NOTHING
print_r(strlen ((int)1)); // PRINTS '1'
[/PHP]

Now I understand that in PHP, everything scalar is a string
*bzzt* wrong. This is where you are getting lost.
and can take on the role of an integer, or a boolean or..
Everything has a type -- so integers are integers and strings are
strings and booleans are booleans. However, PHP will automatically
cast scalars to different types as appropriate for a function or
operator.

So in this case:

var_dump(is_int ('1')) // prints false, it's a string
var_dump(is_int (1)) // prints true, is an int
var_dump(is_str ing('1')) // prints true, is a string
var_dump(is_str ing(false)) // prints false, is a boolean
echo strlen(144) // prints 3 because it casts the number to a string

The is_* functions return the type of the variable -- they do not care
about the contents themselves. So it doesn't care that it's a string
containing an integer.

You can use the is_numeric() function to see if a string value
contains a number.

PHP will automatically cast in cases where you do something like this:

$x = '12' + '16'; // $x will contain 28.

And you can manually cast values yourself:

$x = (integer)'12'; // $x will contain an integer of 12
$x = (string)8l // $x will contain the string '8'
when it seems to me that I have it right based on my basic
understandin g the definition of "types"?


As I said, you're basic definition is wrong. Not all scalar values in
PHP are strings.

Feb 3 '06 #4
Wayne wrote:
On 3 Feb 2006 09:03:34 -0800, "comp.lang. php"
<ph************ **@gmail.com> wrote:
I'm involved in a rather nasty debate involving a strange issue
(whereby the exasperated tell me to RTFM even after my having done so),
where this is insanely possible:

[PHP]
print_r(is_int( '1')); // PRINTS NOTHING
print_r(strlen( (int)1)); // PRINTS '1'
[/PHP]

Now I understand that in PHP, everything scalar is a string
*bzzt* wrong. This is where you are getting lost.
and can take on the role of an integer, or a boolean or..


Everything has a type -- so integers are integers and strings are
strings and booleans are booleans. However, PHP will automatically
cast scalars to different types as appropriate for a function or
operator.


That phrasing is a little confusing...
integer, float, string and boolean are *all* scalar types. (The only 4
scalar types supported by PHP.)

Supported types that are not scalar are:
* the 2 compound types array and object
* the 2 special types NULL and resource.

PHP will automatically cast to the lowest common type in expressions.
Therefore comparing an array to an object will cause both to be cast to
a string first.
So in this case:

var_dump(is_int ('1')) // prints false, it's a string
var_dump(is_int (1)) // prints true, is an int
var_dump(is_str ing('1')) // prints true, is a string
var_dump(is_str ing(false)) // prints false, is a boolean
echo strlen(144) // prints 3 because it casts the number to a string

The is_* functions return the type of the variable -- they do not care
about the contents themselves. So it doesn't care that it's a string
containing an integer.

You can use the is_numeric() function to see if a string value
contains a number.
To test for an integer from a string, use something like:

if(is_numeric(s tring) && intval($string) ==$string){
// $string contains a string representation of an integer
}else{
// not an integer (it's a float)
}
PHP will automatically cast in cases where you do something like this:

$x = '12' + '16'; // $x will contain 28.

And you can manually cast values yourself:

$x = (integer)'12'; // $x will contain an integer of 12
$x = (string)8l // $x will contain the string '8'
when it seems to me that I have it right based on my basic
understanding the definition of "types"?


As I said, you're basic definition is wrong. Not all scalar values in
PHP are strings.


....but all integer, float, string and boolean values are scalar. ;)

--
Justin Koivisto, ZCE - ju****@koivi.co m
http://koivi.com
Feb 3 '06 #5
Justin Koivisto wrote:
Wayne wrote:
On 3 Feb 2006 09:03:34 -0800, "comp.lang. php"
<ph************ **@gmail.com> wrote:
[PHP]
print_r(is_int( '1')); // PRINTS NOTHING
print_r(strlen( (int)1)); // PRINTS '1'
[/PHP]

Now I understand that in PHP, everything scalar is a string


*bzzt* wrong. This is where you are getting lost.

Everything has a type -- so integers are integers and strings are
strings and booleans are booleans. However, PHP will automatically
cast scalars to different types as appropriate for a function or
operator.


That phrasing is a little confusing...
integer, float, string and boolean are *all* scalar types. (The only 4
scalar types supported by PHP.)

Supported types that are not scalar are:
* the 2 compound types array and object
* the 2 special types NULL and resource.


*bzzt* wrong

PHP references are scalar - but in most instances dereferencing is
transparent and higher precedence than casting.

I believe that resources and NULL are also scalars but not castable.

C.
Feb 5 '06 #6
On Sun, 05 Feb 2006 21:54:03 GMT, Colin McKinnon
<co************ **********@ntlw orld.deletemeun lessURaBot.com> wrote:
Supported types that are not scalar are:
* the 2 compound types array and object
* the 2 special types NULL and resource.

*bzzt* wrong

PHP references are scalar - but in most instances dereferencing is
transparent and higher precedence than casting.


References don't really factor into this discussion.
I believe that resources and NULL are also scalars but not castable.


Maybe you should test your beliefs...

if (!is_scalar(nul l)) echo 'I believe you are wrong!';

I was a bit surprised that null was not a scalar -- seems logical that
it would be, but I guess the designers of PHP were trying to lump them
in with objects.

Feb 6 '06 #7

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

Similar topics

6
6651
by: Paul E Collins | last post by:
Given a string variable (form input), how can I determine whether it represents a valid integer? is_numeric is true for floats as well as integers, and is_int always fails on a string. P.
3
2891
by: Mike Vallely | last post by:
If anyone could help me with my problem I'd greatly appreciate it, this question probably has a quick easy answer but I've been wanting to punch the wall for the last hour because of it. I have a string IDNUM = "12345"; I need to do simple arithmetic with the integers in this string. The way I've been trying and failing is by doing something similar to this:
0
2746
by: Tom Warren | last post by:
I found a c program called similcmp on the net and converted it to vba if anybody wants it. I'll post the technical research on it if there is any call for it. It looks like it could be a useful tool for breaking ties when a phonic call returns a bunch of possibilities. Also, I'm looking for someone that has a zip code file with alternate city names (the PO assigns whatever name is convenient to them), email me if you got something. ...
37
8541
by: Shri | last post by:
hi all, i am writing a code in which i have a char buffer "cwdir" which hold the current working directory by calling the function getcwd(). later i change the directory to "/" as i have to make my code Deamon. and later again i want to run some other executable available at the path holded by the "cwdir" using the system() system call. presently i concatenate program name (to be executed) to the "cwdir" and use system(chdir)to run the...
4
8823
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
13
3653
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
12
3228
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1) or (123*1 000)+(4*100)+(5*10)+(6*1) etc...
232
13327
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
11
2305
by: Shisou | last post by:
Hello again, This is still related to my previous post but a different question about the same program... I'm having some trouble converting a string to an array of integers... such as string = 12345 integer = 5 integer = 4 integer = 3
0
10040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.