473,396 Members | 2,011 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,396 software developers and data experts.

confused: casting a variable to integer

Hi group,

Maybe I should stop working because this seems soo basic.
I almost feel ashamed to ask, but here we go.
:-/

Consider the following script:

<?
$name="henk";

echo "\$name=$name <br>";
echo "(int)\$name=".(int)$name."<br>";

if ($name == (int)$name){
echo "equal";
} else {
echo "Not equal";
}
?>

produces:
-----------------
$name=henk
(int)$name=0
equal
------------------

What am I missing here?

Is PHP casting the string "henk" to 0 somehow?

Regards,
Erwin Moller
Nov 29 '05 #1
3 1549
Erwin Moller wrote:
Hi group,

Maybe I should stop working because this seems soo basic.
I almost feel ashamed to ask, but here we go.
:-/

Consider the following script:

<?
$name="henk";

echo "\$name=$name <br>";
echo "(int)\$name=".(int)$name."<br>";

if ($name == (int)$name){
echo "equal";
} else {
echo "Not equal";
}
?>

produces:
-----------------
$name=henk
(int)$name=0
equal
------------------

What am I missing here?

Is PHP casting the string "henk" to 0 somehow?


Yes... When evaluating an expression, PHP uses the lowest common type.
int is simpler than string, therefore, to compare them for equality, PHP
must make them both the same type and then compare.

Once I can find time to write some more articles for my new site. "Type
Juggling" is actually the next on this list. ;) (URL withheld from
public post. If you are curious/interested in the site, email me.)

HTH

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Nov 29 '05 #2
See the differece if you use ===

<?
$name="henk";

echo "\$name=$name <br>";
echo "(int)\$name=".(int)$name."<br>";

if ($name === (int)$name){
echo "equal";
} else {
echo "Not equal";
}

?>

http://php.net/operators.comparison

Nov 29 '05 #3
Sean wrote:
See the differece if you use ===

<?
$name="henk";

echo "\$name=$name <br>";
echo "(int)\$name=".(int)$name."<br>";

if ($name === (int)$name){
echo "equal";
} else {
echo "Not equal";
}

?>

http://php.net/operators.comparison


But be careful with this...

$i="4";
if($i === intval($i)){
// this will not happen
}

Also keep in mind that values that are supplied via _GET, _POST, _COOKIE
are strings, not their representative types...

Therefore:

call page: page.php?i=4
if($_GET['i'] === intval($_GET['i'])){
// this will not happen
}

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Nov 29 '05 #4

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

Similar topics

5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
2
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
8
by: Sandy | last post by:
Hello! Help!!!! I have ten zillion books that attempt to describe the difference between ByVal and ByRef and none of them are clear to me. I have gathered that ByVal makes a copy and ByRef...
8
by: Jonathan Fielder | last post by:
Hi, I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
14
by: Shannon Richards | last post by:
Hello: I am confused about casting... Option Strict = ON Example1: --------------- When I say something like this... Dim lo_Foo = New Foo Dim lo_Obj as Object = lo_Foo
9
by: Brian | last post by:
Hello! What is the proper syntax for casting? For example, how do I change an Integer in to a String if the variable is called Joe1 and has 20 assigned to it. Thanks, Brian
4
by: Vincent | last post by:
Regarding the dropdetect function, defined as follows: Sub dropdetect(DropFrm As Form, DropCtrl As Control, Button As Integer, Shift As Integer, X As Single, Y As Single) Is it possible to...
11
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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...
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,...

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.