Jump to content


Photo

oop problem - url


  • Please log in to reply
7 replies to this topic

#1 pedrobcabral

pedrobcabral
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 02 October 2006 - 06:28 PM

Why does the following $someclass->number+01 returns to me 01+01, when it should return 02?

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 02 October 2006 - 07:00 PM

sorry, my crystal ball is broken. you'll have to display some code.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 pedrobcabral

pedrobcabral
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 02 October 2006 - 07:09 PM

I have a php class variable that calls number.
public $number; (that contains the number 01).

While calling a function that adds to that var the number 01, i mean:
$myclass->number + 01; this should output 02, once my variable number is set to be 01.

Wrongly the result comes to be 01+01, where i wanted to be 02.

Thanks for the help.

#4 Hi I Am Timbo

Hi I Am Timbo
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 02 October 2006 - 07:19 PM

Are you sure it is storing the number 01 and not the string 01?  PHP doesn't store preceding zeroes.

#5 Hi I Am Timbo

Hi I Am Timbo
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 02 October 2006 - 07:21 PM

Are you doing an echo $myclass->number + 01?

like was mentioned before, can you show us the code?

#6 pedrobcabral

pedrobcabral
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 02 October 2006 - 07:23 PM

I guess the problem could be explained like this:

<?php
$var = 10;

echo "Calc: $var+01";

?>

The echo goes for "Calc: 10+01" instead of 11.
Why? Thank you.

#7 Hi I Am Timbo

Hi I Am Timbo
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 02 October 2006 - 07:25 PM

Because PHP doesn't parse the whole string.  It treats the string as a string, and inserts the $var as a string. 

It would have to be like this:

<?php
$var = 10;

echo "Calc: ".($var+01);

?>

or this, which changes the value of $var:

<?php
$var = 10;

$var += 01;

echo "Calc: $var";

?>


#8 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 02 October 2006 - 07:31 PM

right, all your equations need to be done outside of the quotes (unless you're using exec(), but let's not get into that here ;) ):

<?php
// this outputs "01 + 01"
$myClass->number = '01';
echo "$myClass->number + 01";

// this outputs "2"
echo $myClass->number + 1;
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users