By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,044 Members | 1,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,044 IT Pros & Developers. It's quick & easy.

variables and values

P: n/a
hi all, i'm very new to php and i'm a little confused about something.
this will be dead simple to the rest of you but it doesn't work for
me.

i have a variable called "$a" which has a value of 10.

i want to pass this value to a new variable and increase it by 10, is
this the right way?

$a=$b;
$b + 10;

thanks for not laughing :o)
Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Fri, 02 Apr 2004 10:31:47 -0800, none wrote:
hi all, i'm very new to php and i'm a little confused about something.
this will be dead simple to the rest of you but it doesn't work for
me.

i have a variable called "$a" which has a value of 10.

i want to pass this value to a new variable and increase it by 10, is
this the right way?

$a=$b;
$b + 10;

thanks for not laughing :o)

What you have is perfectly "legal" and will work.. however, if you want to
tidy it up:
$b = $a + 10;
If you don't _really_ need to pass it to another value and just +10 to it,
you can use:
$a +=10;
This is the same as:
$a = $a + 10;
I realise this might not be what you're after (second example) but may be
of use for the future if you were unaware =)
HTH.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #2

P: n/a

Hi none! Welcome to PHP.

none wrote:
i have a variable called "$a" which has a value of 10.

i want to pass this value to a new variable and increase it by 10, is
this the right way?

$a=$b;
This puts into $a the contents of $b;

How did $a get to have the value of 10?
You didn't do "10=$a;" (parse error) -- this doesn't work because you
can't change the value 10. The 'thing' being changed _always_ (*) goes
on the left side of the equals sign.

To make $b have the same thing $a has do

$b = $a;

$b + 10;


This one is not changing any variable -- it just calculates the value
and then ignores it because there is no instruction to do something with
that value; you want to put that value into a variable, so you have to
say that to PHP:

$b = $b + 10;

Which means "add the contents of $b to 10 and save the resulting value
in $b".
(*) but some things can be changed on the right side too, or even
without an equals sign :)

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #3

P: n/a
On Fri, 02 Apr 2004 18:42:55 +0000, Ian.H wrote:
On Fri, 02 Apr 2004 10:31:47 -0800, none wrote:
hi all, i'm very new to php and i'm a little confused about something.
this will be dead simple to the rest of you but it doesn't work for
me.

i have a variable called "$a" which has a value of 10.

i want to pass this value to a new variable and increase it by 10, is
this the right way?

$a=$b;
$b + 10;

thanks for not laughing :o)

What you have is perfectly "legal" and will work.. however, if you want to
tidy it up:
$b = $a + 10;

Oops!

After reading Pedro's post below.. I realised I made a massive error here =[

Pedro is correct.. please excuse and ignore my reply about the above being
correct.

The following _is_ correct (just for my own FWIW =) ):
$b = $a;
$b =+ 10;

$b = $a + 10;

$b = $a;
$b = $b + 10;
All of these will work.. but your choice which is preferable (#2) =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #4

P: n/a
On Fri, 02 Apr 2004 19:30:39 GMT, "Ian.H" <ia*@WINDOZEdigiserv.net> wrote:
The following _is_ correct (just for my own FWIW =) ):

$b =+ 10;


*polite cough* ;-) Isn't that:

'$b' 'assignment' 'unary positive' 'constant integer 10' ?

You mean $b += 10...

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #5

P: n/a

"none" <ki*****@aol.com> wrote in message
news:86**************************@posting.google.c om...
hi all, i'm very new to php and i'm a little confused about something.
this will be dead simple to the rest of you but it doesn't work for
me.

i have a variable called "$a" which has a value of 10.

i want to pass this value to a new variable and increase it by 10, is
this the right way?

$a=$b;
$b + 10;

thanks for not laughing :o)


This is the right way to do it, I think:

class Number {
$value;

function Number($value) {
$this->value = $value;
}

function ToString() {
return $this->value;
}

function GetValue() {
return $this->value;
}

function Clone() {
return $this;
}
}

class RealInteger extends Number {
function RealInteger($value) {
$this->Number($value);
}

function IsPositive() {
return ($this->value >= 0);
}

function Add($increment) {
$this->value += $increment;
return $this->value;
}

function Minus($increment) {
$this->value -= $increment;
return $this->value;
}
}

class CustomRealInteger extends RealInteger {
function CustomRealInteger($value = 0) {
$this->RealInteger($value);
}

function IncreaseByTen() {
return $this->Add(10);
}
}

$a = new CustomRealInteger(5);
$b = $a->Clone();
$b->IncreaseByTen();
Jul 17 '05 #6

P: n/a
Pedro Graca <he****@hotpop.com> wrote in message news:<c4*************@ID-203069.news.uni-berlin.de>...
Hi none! Welcome to PHP.

none wrote:
i have a variable called "$a" which has a value of 10.

i want to pass this value to a new variable and increase it by 10, is
this the right way?

$a=$b;


This puts into $a the contents of $b;

How did $a get to have the value of 10?
You didn't do "10=$a;" (parse error) -- this doesn't work because you
can't change the value 10. The 'thing' being changed _always_ (*) goes
on the left side of the equals sign.

To make $b have the same thing $a has do

$b = $a;

$b + 10;


This one is not changing any variable -- it just calculates the value
and then ignores it because there is no instruction to do something with
that value; you want to put that value into a variable, so you have to
say that to PHP:

$b = $b + 10;

Which means "add the contents of $b to 10 and save the resulting value
in $b".
(*) but some things can be changed on the right side too, or even
without an equals sign :)

Beuty, all working!!!
Jul 17 '05 #7

P: n/a
This is the right way to do it, I think:

class Number {
$value;

function Number($value) {
$this->value = $value;
}

function ToString() {
return $this->value;
}

function GetValue() {
return $this->value;
}

function Clone() {
return $this;
}
}

class RealInteger extends Number {
function RealInteger($value) {
$this->Number($value);
}

function IsPositive() {
return ($this->value >= 0);
}

function Add($increment) {
$this->value += $increment;
return $this->value;
}

function Minus($increment) {
$this->value -= $increment;
return $this->value;
}
}

class CustomRealInteger extends RealInteger {
function CustomRealInteger($value = 0) {
$this->RealInteger($value);
}

function IncreaseByTen() {
return $this->Add(10);
}
}

$a = new CustomRealInteger(5);
$b = $a->Clone();
$b->IncreaseByTen();


dont you think that is a bit overkill for someone who just wants to know how
to add 10 to a variable
Jul 17 '05 #8

P: n/a
Chung Leong wrote:
"none" <ki*****@aol.com> wrote in message
news:86**************************@posting.google.c om...
hi all, i'm very new to php and i'm a little confused about
something. this will be dead simple to the rest of you but it
doesn't work for me.

i have a variable called "$a" which has a value of 10.

i want to pass this value to a new variable and increase it by 10, is
this the right way?

$a=$b;
$b + 10;

thanks for not laughing :o)


This is the right way to do it, I think:

class Number {

<snip>

LMFAO, I can't stop laughing...
Jul 17 '05 #9

P: n/a
On Fri, 02 Apr 2004 21:08:24 +0100, Andy Hassall wrote:
On Fri, 02 Apr 2004 19:30:39 GMT, "Ian.H" <ia*@WINDOZEdigiserv.net> wrote:
The following _is_ correct (just for my own FWIW =) ):

$b =+ 10;


*polite cough* ;-) Isn't that:

'$b' 'assignment' 'unary positive' 'constant integer 10' ?

You mean $b += 10...

Wow! today _really_ wasn't my day lol.

Thanks for pointing out this (additional) error. I'm just glad I post here
fairly frequently with some working answers.. I'd look a right prick
otherwise.. or a m$ employee (don't they fix bugs with more bugs? =) ).

Thanks Andy for pointing this out.. and you're correct, I did mean '+='
rather than '=+'.

Can I just pretent that $a was defined as 0? =D

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #10

P: n/a
On Sat, 03 Apr 2004 12:49:23 +0100, Filth wrote:
dont you think that is a bit overkill for someone who just wants to know how
to add 10 to a variable


For the humor impaired, Chung's post was pretty likely a joke.

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/

Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.