473,385 Members | 1,620 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,385 software developers and data experts.

Typecast problem in PHP 5.0.4

Hi,

I am using the following snippet to compare an object with integer in
my script.

if ( $forecast['day_sequence'] < 4 ) {

I got the "segmentation fault" error message when i executed this
script in CLI. After debugging, i had changed my code to

if ( (int) $forecast['day_sequence'] < 4 ) {

and its worked well. So its means my php version dont do typecasting
automatically it seems.

Is it true?

--
Mani

Aug 30 '06 #1
11 1904
*** Manikandan escribió/wrote (30 Aug 2006 04:43:29 -0700):
if ( $forecast['day_sequence'] < 4 ) {

I got the "segmentation fault" error message when i executed this
script in CLI. After debugging, i had changed my code to

if ( (int) $forecast['day_sequence'] < 4 ) {

and its worked well. So its means my php version dont do typecasting
automatically it seems.

Is it true?
Nope. A segmentation fault is normally a bug in the PHP interpreter or some
other system library. Bad code may help to trigger it some times but it's
always an unexpected result: ideally, PHP should complain if necessary but
not crash.

I've tried this code:

<?php

class Foo{
}

$foo=new Foo();

if($foo < 4){
echo "foo is less than four\n";
}

?>

The condition is true and it also raises a notice: "Notice: Object of class
Foo could not be converted to int"

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Aug 31 '06 #2
Hi

Thnks for ur reply.
Nope. A segmentation fault is normally a bug in the PHP interpreter or some
other system library. Bad code may help to trigger it some times but it's
always an unexpected result: ideally, PHP should complain if necessary but
not crash.
Bad code in the sense... i ddint get ur point. Can you explain more
about segmentation fault?

You mentioned that automatic typecast wont raise segmentation error,
then how come after doing only explicit typecast in the code it worked
well

or
The condition is true and it also raises a notice: "Notice: Object of class
Foo could not be converted to int"
Will there any difference between class object and xml object?

If this is simple question, please forgive and help me to know about
this fact.

--
Mani

Sep 2 '06 #3
*** Mani escribió/wrote (2 Sep 2006 01:52:33 -0700):
Bad code in the sense... i ddint get ur point. Can you explain more
about segmentation fault?

You mentioned that automatic typecast wont raise segmentation error,
then how come after doing only explicit typecast in the code it worked
well
Alright, let's use a non-programming example. Suppose you're typing a
dossier in Microsoft Word. You need to highlight an specific paragraph so
select the text and press the "bold" toolbar button. And then you get a
message that says "Application performed an illegal operation and will be
shut down" and Word crashes. Does it mean there's something wrong in using
bold fonts in documents? Obviously not!

>The condition is true and it also raises a notice: "Notice: Object of class
Foo could not be converted to int"

Will there any difference between class object and xml object?
I thought you used word "object" in PHP context. Never mind, whatever an
object is for you, it doesn't make any sense to cast it to integer. It's
like checking whether a JPEG picture is greater or equal than zero, it's
total nonsense.
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Sep 2 '06 #4
Hi

Thnks for your reply.

I understood the point explained by you and have to learn good
programming style.

I am in a situation to do further process only if it is less than 4 so
i need to compare in better way not like the existing code. Can you
suggest how to write alternative code for the following snippet?

if ( (int) $forecast['day_sequence'] < 4 ) {

--
Mani
Alvaro G. Vicario wrote:
*** Mani escribió/wrote (2 Sep 2006 01:52:33 -0700):
Bad code in the sense... i ddint get ur point. Can you explain more
about segmentation fault?

You mentioned that automatic typecast wont raise segmentation error,
then how come after doing only explicit typecast in the code it worked
well

Alright, let's use a non-programming example. Suppose you're typing a
dossier in Microsoft Word. You need to highlight an specific paragraph so
select the text and press the "bold" toolbar button. And then you get a
message that says "Application performed an illegal operation and will be
shut down" and Word crashes. Does it mean there's something wrong in using
bold fonts in documents? Obviously not!

The condition is true and it also raises a notice: "Notice: Object of class
Foo could not be converted to int"
Will there any difference between class object and xml object?

I thought you used word "object" in PHP context. Never mind, whatever an
object is for you, it doesn't make any sense to cast it to integer. It's
like checking whether a JPEG picture is greater or equal than zero, it's
total nonsense.
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Sep 3 '06 #5
*** Mani escribió/wrote (2 Sep 2006 22:38:05 -0700):
I am in a situation to do further process only if it is less than 4 so
i need to compare in better way not like the existing code. Can you
suggest how to write alternative code for the following snippet?

if ( (int) $forecast['day_sequence'] < 4 ) {
Did you write this code yourself or is it a snipped you copied from
somewhere? It seems that you don't have the faintest idea of what
forecast['day_sequence'] is ... :-?

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Sep 4 '06 #6

I didnt copied and it was written by me. $forecast is an xml element
and 'day_sequence' is an attribute in it and am using simplexml to
parse the document.

For ex, the structure is

<forecast day_sequence = "4" />

Hope the above info will get you to give me the solution.

--
Mani
Alvaro G. Vicario wrote:
*** Mani escribió/wrote (2 Sep 2006 22:38:05 -0700):
I am in a situation to do further process only if it is less than 4 so
i need to compare in better way not like the existing code. Can you
suggest how to write alternative code for the following snippet?

if ( (int) $forecast['day_sequence'] < 4 ) {

Did you write this code yourself or is it a snipped you copied from
somewhere? It seems that you don't have the faintest idea of what
forecast['day_sequence'] is ... :-?

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Sep 5 '06 #7
*** Mani escribió/wrote (5 Sep 2006 01:18:02 -0700):
I didnt copied and it was written by me. $forecast is an xml element
and 'day_sequence' is an attribute in it and am using simplexml to
parse the document.

For ex, the structure is

<forecast day_sequence = "4" />

Hope the above info will get you to give me the solution.
A guess: the XML file is somehow large. The fact that it crashes in that
very line is pure chance. I faced such problem not long ago.

Does it happen too if you parse a small XML file?

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Sep 5 '06 #8

Alvaro G. Vicario wrote:
*** Mani escribió/wrote (5 Sep 2006 01:18:02 -0700):
I didnt copied and it was written by me. $forecast is an xml element
and 'day_sequence' is an attribute in it and am using simplexml to
parse the document.

For ex, the structure is

<forecast day_sequence = "4" />

Hope the above info will get you to give me the solution.

A guess: the XML file is somehow large. The fact that it crashes in that
very line is pure chance. I faced such problem not long ago.

Does it happen too if you parse a small XML file?
No its not a large file and infact the xml document has only 6 forecast
element with about 20 attributes in each.

Do you think it is big?

--
Mani

Sep 7 '06 #9
*** Mani escribió/wrote (7 Sep 2006 04:50:45 -0700):
No its not a large file and infact the xml document has only 6 forecast
element with about 20 attributes in each.

Do you think it is big?
It depends on the size of the forecast, I suppose. My original XML file had
about 7,000 items and the script crashed with all test file larger than
2,000. Just a guess, as I said ;-)
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Sep 7 '06 #10

It is very small when comparing to ur small file itself.

So ur point is, only because of larger filesize it is showing
"segmentation fault" error not with my comparison of xml object with an
integer

Am i right?

--
Mani

Alvaro G. Vicario wrote:
*** Mani escribió/wrote (7 Sep 2006 04:50:45 -0700):
No its not a large file and infact the xml document has only 6 forecast
element with about 20 attributes in each.

Do you think it is big?

It depends on the size of the forecast, I suppose. My original XML file had
about 7,000 items and the script crashed with all test file larger than
2,000. Just a guess, as I said ;-)
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Sep 8 '06 #11
*** Mani escribió/wrote (7 Sep 2006 21:10:33 -0700):
So ur point is, only because of larger filesize it is showing
"segmentation fault" error not with my comparison of xml object with an
integer

Am i right?
Actually, we don't have the faintest idea of why it crashes. The cast
operation maybe the cause... or not. I pointed a similar case that happened
to me but yours seems to be different.

It's possible that if you upgrade or downgrade your PHP installation this
very same code won't segfault.

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Sep 8 '06 #12

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

Similar topics

5
by: Lars Plessmann | last post by:
I have a problem with typecast methods. Here is a setter of an object: function setKind($kind) { if (is_int((int)$kind) and (strlen($kind)<=6)) { $this->kind = $kind; return true;
9
by: Venkat | last post by:
Hi All, I want to typecast int to std::string how can i do it. Here is the sample code. int NewList; //Fill the NewList with integers values. .......
2
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents a rather good discussion of typecast operator overloading. I am presenting...
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
8
by: Trishia Rose | last post by:
this is something ive always wondered, does it take cpu time at run time to typecast or just at compile time? for example consider the two little bits of code: int a = 5; int b = a; and: ...
5
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
1
by: skumar22 | last post by:
when I typecast a float to an int. Does the compiler allocate an additional space? for eg if I do something like. float pi = 3.14; int i = (int) pi; then an additional space is created for...
3
by: ryan.gilfether | last post by:
I have a problem that I have been fighting for a while and haven't found a good solution for. Forgive me, but my C++ is really rusty. I have a custom config file class: class ConfigFileValue...
1
by: mike3 | last post by:
Hi. I heard that in C++ typecasts are "evil". However, what does one do with this thing?: --- /* Figure out how many digits of fraction we'll get. * We add 1 to the lengths of a and b to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...

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.