...

View Full Version : Operators



ooiyh
07-16-2007, 03:35 PM
Hi,

What does this means? What will the output of $a?


$a = true ? 0 : true ? 1 : 2;

And what does a backticks (``) do?

firepages
07-16-2007, 05:09 PM
$a=2 and I don't see how it could ever be anything else ... so I don't see the point of the code though I am probably missing something obvious ?

backticks are essentially the same as system() call

so


<?
system('/etc/init.d/blah start');
?>
and
<?
`/etc/init.d/blah start`;
?>

both do the same thing.

NancyJ
07-16-2007, 05:55 PM
Your code appears to be a nested ternary.

Normally, it would look like this:

$a = (condition) ? value if condition= true : value if condition = false

so it would look a bit like

$a = true ? 2 : 1;

which says if true then $a = 2 otherwise $a = 1;

Now your code appears to be using another ternary as the conditon part of the second one... so its like this:

$a = (condition) ? 1: 2;

where the condition is the result of:
true ? 0 : true;

Now the condition part of the second ternary is just 'true' therefore it always returns the first value - 0 - which equates to false. So the condition returns false. So $a = 2

Off the top of my head I cant think of any use for a nested ternary since the condition of the condition would have to evaluate to true or false, theres no need for the ternary.

Also in sql statements ` backticks are sometimes used around field and tables names, although this is unecessary unless the names are reserved words - eg. if you had a column called 'order' you would have to write it as `order` so as not to be confused with the sql instruction ORDER BY

dumpfi
07-16-2007, 05:57 PM
This:
$a = true ? 0 : true ? 1 : 2;is equivalent to this:
if(true)
{
if(0)
{
$a = 1;
}
else
{
$a = 2;
}
}
else
{
if(true)
{
$a = 1;
}
else
{
$a = 2;
}
}Thus the code is evaluated as:
$a = (true ? 0 : true) ? 1 : 2;Probably more useful and how many other languages treat nested ternaries is this form:
$a = true ? 0 : (true ? 1 : 2);

See also the manual for ternary operators (http://de3.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary).

dumpfi

NancyJ
07-16-2007, 06:03 PM
This:
$a = true ? 0 : true ? 1 : 2;is equivalent to this:
if(TRUE){
$a = 0;
}
else
{
if(TRUE)
{
$a = 1;
}
else
{
$a = 2;
}
}See also the manual for ternary operators (http://de3.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary).

dumpfi

Not quite - in your example $a would = 0 and never progress to the second part. If you run the code $a = 2;

I dont think its actually possible to express as a simple if then else - this is the best I could come up with



if(true)
{
$temp = 0;
}
else
{
$temp = true;
}
if($temp)
{
$a = 1;
}
else
{
$a=2;
}

GJay
07-16-2007, 06:03 PM
except it isn't. As firepages said, the result will be 2, with brackets it's:


(true ? 0 : true) ? 1 : 2

which is a bit messy to express in longer notation:


if(true) {
$first_bit = 0;
}
else {
$first_bit = true;
}
if($first_bit) {
$a = 1;
}
else {
$a = 2;
}

It's still fairly pointless code though.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum