easy way to execute conditional html / javascript / css / other language code with php if else:
<?php if (condition): ?>
html code to run if condition is true
<?php else: ?>
html code to run if condition is false
<?php endif ?>
(PHP 4, PHP 5, PHP 7, PHP 8)
if
構文は、PHP を含む全ての言語において最も重要な
機能の一つです。
この構文は、命令の条件実行を可能にします。
PHP では、C 言語に似た次のような if
構文が使用されます。
if (式) 文
式のセクションで
記述したように式は論理値で評価されます。
式が true
と評価された場合、
PHP は文を実行します。false
と評価された場合は、これを無視します。どのような値が
false
と評価されるかについては論理値への変換
を参照ください。
以下の例は、$a が $b より大きい場合、aはbより大きい を表示します。
<?php
if ($a > $b)
echo "aはbより大きい";
?>
条件分岐させたい文が一つ以上ある場合もしばしばあります。
もちろん、各々の文をif
文で括る必要はありません。
代わりに、複数の文をグループ化することができます。
例えば、このコードは、$a
が $b よりも大きい場合に
aはbよりも大きいを表示し、
$a の値を $b に
代入します。
<?php
if ($a > $b) {
echo "aはbより大きい";
$b = $a;
}
?>
if
文は、他のif
文の中で無限に入れ子にできます。
これは、プログラムの様々な部分の条件付実行について
完全な柔軟性を提供します。
easy way to execute conditional html / javascript / css / other language code with php if else:
<?php if (condition): ?>
html code to run if condition is true
<?php else: ?>
html code to run if condition is false
<?php endif ?>
You can have 'nested' if statements withing a single if statement, using additional parenthesis.
For example, instead of having:
<?php
if( $a == 1 || $a == 2 ) {
if( $b == 3 || $b == 4 ) {
if( $c == 5 || $ d == 6 ) {
//Do something here.
}
}
}
?>
You could just simply do this:
<?php
if( ($a==1 || $a==2) && ($b==3 || $b==4) && ($c==5 || $c==6) ) {
//do that something here.
}
?>
Hope this helps!
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.
An other way for controls is the ternary operator (see Comparison Operators) that can be used as follows:
<?php
$v = 1;
$r = (1 == $v) ? 'Yes' : 'No'; // $r is set to 'Yes'
$r = (3 == $v) ? 'Yes' : 'No'; // $r is set to 'No'
echo (1 == $v) ? 'Yes' : 'No'; // 'Yes' will be printed
// and since PHP 5.3
$v = 'My Value';
$r = ($v) ?: 'No Value'; // $r is set to 'My Value' because $v is evaluated to TRUE
$v = '';
echo ($v) ?: 'No Value'; // 'No Value' will be printed because $v is evaluated to FALSE
?>
Parentheses can be left out in all examples above.
Any variables defined inside the if block will be available outside the block. Remember that the if doesn't have its own scope.
<?php
$bool = true;
if ($bool) {
$hi = 'Hello to all people!';
}
echo $hi;
?>
It will print 'Hello to all people!'
On the other hand, this will have no output:
<?php
if (false) {
$hi = 'Hello to all people!';
}
echo $hi;
?>
In addition to the traditional syntax for if (condition) action;
I am fond of the ternary operator that does the same thing, but with fewer words and code to type:
(condition ? action_if_true: action_if_false;)
example
(x > y? 'Passed the test' : 'Failed the test')
Note that the IF statement does not always evaluates all expressions. In the next example the second expression will not be evaluated causing our function not to run. Because the result is already TRUE and the rest of the expressions are behind the OR operator they will be ignored.
<?php
$a = 0;
if ( true || $a = important_function() ) echo $a;
function important_function(){
// Do very important stuff here.
return 1;
}
?>