problem in type casting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

problem in type casting

Post by itsmani1 »

I want to cast a real value to an int value and its giving me error

Code: Select all

$aa = 2.323;
  echo int($aa);
is there any solution it it?

Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: problem in type casting

Post by Chris Corbyn »

itsmani1 wrote:I want to cast a real value to an int value and its giving me error

Code: Select all

$aa = 2.323;
  echo int($aa);
is there any solution it it?

Thanks
That's not cast notation, that's function notation. It should be:

Code: Select all

$aa = 2.323;
  echo (int) $aa;
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

Thanks much man.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

$aa = 2.323;
  echo int($aa);
Obviously someone from a C++ background who doesn't know his C roots. Tisk tisk :P
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

ole wrote:

Code: Select all

$aa = 2.323;
  echo int($aa);
Obviously someone from a C++ background who doesn't know his C roots. Tisk tisk :P
Note: There is a function that returns the integer value of it's argument. intval()
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Why? Who exactly thought that function was a good idea?
I swear the PHP developers just like to bloat PHP
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

ole wrote:Why? Who exactly thought that function was a good idea?
I swear the PHP developers just like to bloat PHP
It probably reads more logically to people who aren't well-versed in programming. Passing everything through functions is a lot easier to learn than getting to grips with various syntactical concepts. PHP attracts a lot of people who've never written a line of code in any other language before.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

why the hell didn't they call it int() then?!
And also this is another example of why there should be two versions of php
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

ole wrote:I swear the PHP developers just like to bloat PHP
What's keeping you from doing it better then? It's not like that you can't fork your own php-done-good-this-time...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

What's keeping you from doing it better then? It's not like that you can't fork your own php-done-good-this-time...
Well yes it is. There is plenty stopping me

I went through a stage were I would design documents for new languages for fun but the whole process is really depressing because I don't have the time or ability to write my own language parsers. And I usually found as the years progressed my ideas showed weakness as others devised and implemented better solutions.

And anyway despite my complaints I love PHP and the annoyances are only small.
Post Reply