Jump to content

$foo=$bar and $bar=$foo


Orio

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.
Link to comment
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]
Link to comment
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.
Link to comment
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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.