\$foo=\$bar and \$bar=\$foo

5 replies to this topic

#1 Orio

Orio
• Staff Alumni
• 2,491 posts

Posted 15 May 2006 - 01:34 PM

Hello,
Is there a diffrence between- \$foo=\$bar and \$bar=\$foo ?
Lets take an example:
```\$foo=1;
\$bar=10;
if(\$_POST['age']<20)
{\$bar=\$foo;}
elseif(\$_POST['age']<50)
{\$foo=\$bar;}
else
{\$foo=100; \$bar=100;};```

Lets say \$_POST['age] is smaller than 20. Will both vars will be equal 10? Or is it vice versa?

Thanks,
Orio.
Think you're smarty?

(Gone until 20 to November)

#2 alpine

alpine
• Members
• 756 posts
• LocationNorway

Posted 15 May 2006 - 01:40 PM

```
\$foo=1;
\$bar=10;

if(\$_POST['age']<20)
{
\$bar=\$foo; // \$bar will be 1 that is the value of \$foo
}
elseif(\$_POST['age']<50)
{
\$foo=\$bar; // \$foo will be 10 that is the value of \$bar
}
else
{
\$foo=100;
\$bar=100;
}

```

#3 _will

_will
• Members
• Member
• 25 posts

Posted 15 May 2006 - 01:43 PM

Both variables will be '1'. It will execute the first IF statement, and then exit the conditional, so \$foo = 1 and \$bar = 1.

#4 Orio

Orio
• Staff Alumni
• 2,491 posts

Posted 15 May 2006 - 05:05 PM

Thanks alpine [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
But what about varibles that weren't set?
Another example:
```\$var=1;
if(!isset(\$age)){
\$var=\$age;
echo(\$age);
};
```
Will any error occur? And is \$var's value going to stay 1?

Thx again,
Orio.
Think you're smarty?

(Gone until 20 to November)

#5 _will

_will
• Members
• Member
• 25 posts

Posted 15 May 2006 - 05:10 PM

[!--quoteo(post=374067:date=May 15 2006, 01:05 PM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ May 15 2006, 01:05 PM) [/div][div class=\'quotemain\'][!--quotec--]
Thanks alpine [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
But what about varibles that weren't set?
Another example:
```\$var=1;
if(!isset(\$age)){
\$var=\$age;
echo(\$age);
};
```
Will any error occur? And is \$var's value going to stay 1?

Thx again,
Orio.
[/quote]

Why don't you test the PHP code you put up? You could put that in a test page and echo the output to see if it behaves the way you think it will. It probably would have taken less time than it would to post to the forum.

#6 alpine

alpine
• Members
• 756 posts
• LocationNorway

Posted 15 May 2006 - 06:57 PM

[!--quoteo(post=374067:date=May 15 2006, 12:05 PM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ May 15 2006, 12:05 PM) [/div][div class=\'quotemain\'][!--quotec--]
Thanks alpine [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
But what about varibles that weren't set?
Another example:
```\$var=1;
if(!isset(\$age)){
\$var=\$age;
echo(\$age);
};
```
Will any error occur? And is \$var's value going to stay 1?

Thx again,
Orio.
[/quote]
First off, your code here isn't logical because you are setting \$var to be \$age only when \$age don't exist - so it would never echo anything

a variable that don't exist (isn't set) won't echo anything

```\$age = \$_GET['age'];
\$var = 1;

if(empty(\$age))
{
\$age = \$var;
}

echo \$age;
// prints out 1 if \$age is empty, else it will echo whatever \$age contains
```

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users