Jump to content

Archived

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

Orio

$foo=$bar and $bar=$foo

Recommended Posts

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

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

Thanks,
Orio.

Share this post


Link to post
Share on other sites
[code]

$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;
}

[/code]

Share this post


Link to post
Share on other sites


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

Share this post


Link to post
Share on other sites
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:
[code]$var=1;
if(!isset($age)){
$var=$age;
echo($age);
};
[/code]
Will any error occur? And is $var's value going to stay 1?

Thx again,
Orio.

Share this post


Link to post
Share on other sites
[!--quoteo(post=374067:date=May 15 2006, 01:05 PM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ May 15 2006, 01:05 PM) [snapback]374067[/snapback][/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:
[code]$var=1;
if(!isset($age)){
$var=$age;
echo($age);
};
[/code]
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.

Share this post


Link to post
Share on other sites
[!--quoteo(post=374067:date=May 15 2006, 12:05 PM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ May 15 2006, 12:05 PM) [snapback]374067[/snapback][/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:
[code]$var=1;
if(!isset($age)){
$var=$age;
echo($age);
};
[/code]
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

[code]
$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
[/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.