RE: chrislabricole at yahoo dot fr on 09-Aug-2008 05:53
You're referring to the ternary operator.
http://php.net/manual/en/language.operators.comparison.php
if
if 構文は、PHP を含む全ての言語において最も重要な 機能の一つです。 この構文は、命令の条件実行を可能にします。 PHP では、C 言語に似た次のような if 構文が使用されます。
if (式)
文
式のセクションで 記述したように式は論理値で評価されます。 式が TRUE と評価された場合、 PHP は文を実行します。FALSE と評価された場合は、これを無視します。どのような値が FALSE と評価されるかについては論理値への変換 を参照してください。
以下の例は、$a が $b より大きい場合、aはbより大きい を表示します。
if ($a > $b)
echo "aはbより大きい";
条件分岐させたい文が一つ以上ある場合もしばしばあります。 もちろん、各々の文をif 文で括る必要はありません。 代わりに、複数の文をグループ化することができます。 例えば、このコードは、$a が $b よりも大きい場合に aはbよりも大きいを表示し、 $a の値を $b に 代入します。
if ($a > $b) {
echo "aはbより大きい";
$b = $a;
}
if文は、他のif文の中で無限に入れ子にできます。 これは、プログラムの様々な部分の条件付実行について 完全な柔軟性を提供します。
if
Wiseguy
28-Aug-2008 05:22
28-Aug-2008 05:22
jchau at bu dot edu
14-Aug-2008 08:50
14-Aug-2008 08:50
RE: henryk dot kwak at gmail dot com's comment from 04-May-2008 05:01
I think you made a mistake.
For maximum efficiency, assuming each expression requires the same amount of processing, the expression that is least likely to be true should come first for expressions connected by && (and). This will reduce the probability that later expressions will need to be evaluated.
The opposite is true for || (or). If the most likely expression comes first, then the probability of needing to evaluate later expressions is reduced.
chrislabricole at yahoo dot fr
10-Aug-2008 03:53
10-Aug-2008 03:53
You can do IF with this pattern :
<?php
$var = TRUE;
echo $var==TRUE ? 'TRUE' : 'FALSE'; // get TRUE
echo $var==FALSE ? 'TRUE' : 'FALSE'; // get FALSE
?>
henryk dot kwak at gmail dot com
05-May-2008 03:01
05-May-2008 03:01
When you use if command with many condidions like
if ( expr1 && expr2 && expr3 && etc. )
it is more effective to put expressions in special order
Firstly you should put that, which has the biggest
probability to occur.
This is because PHP checks each condition in order from left to right and it takes some time to check each condition.
grawity at gmail dot com
10-Mar-2008 01:41
10-Mar-2008 01:41
re: #80305
Again useful for newbies:
if you need to compare a variable with a value, instead of doing
<?php
if ($foo == 3) bar();
?>
do
<?php
if (3 == $foo) bar();
?>
this way, if you forget a =, it will become
<?php
if (3 = $foo) bar();
?>
and PHP will report an error.
redrobinuk at aol dot com
09-Jan-2008 12:54
09-Jan-2008 12:54
This is aimed at PHP beginners but many of us do this Ocasionally...
When writing an if statement that compares two values, remember not to use a single = statement.
eg:
<?php
if ($a = $b)
{
print("something");
}
?>
This will assign $a the value $b and output the statement.
To see if $a is exactly equal to $b (value not type) It should be:
<?php
if ($a == $b)
{
print("something");
}
?>
Simple stuff but it can cause havok deep in classes/functions etc...
