Jump to content

Archived

This topic is now archived and is closed to further replies.

pedrobcabral

oop problem - url

Recommended Posts

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.

Share this post


Link to post
Share on other sites
I guess the problem could be explained like this:

[code]
<?php
$var = 10;

echo "Calc: $var+01";

?>
[/code]

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

Share this post


Link to post
Share on other sites
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:

[code]
<?php
$var = 10;

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

?>
[/code]

or this, which changes the value of $var:

[code]
<?php
$var = 10;

$var += 01;

echo "Calc: $var";

?>
[/code]

Share this post


Link to post
Share on other sites
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 ;) ):

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

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.