downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

else> <導入
Last updated: Fri, 06 Nov 2009

view this page in

if

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文の中で無限に入れ子にできます。 これは、プログラムの様々な部分の条件付実行について 完全な柔軟性を提供します。



else> <導入
Last updated: Fri, 06 Nov 2009
 
add a note add a note User Contributed Notes
if
austinderrick2 at gmail dot com
03-Oct-2009 08:50
As an added note to the guy below, in such a case, use the !== operator like this.

$nkey = array_search($needle, $haystack);

if ($nkey !== false) { ...

The !== and the === compare the "types". So, with this type of comparision, 0 is not the same as the FALSE returned by the array_search array when it can not find a match. :)

Quoted Text:

===================================
Be careful with stuff like

if ($nkey = array_search($needle, $haystack)) { ...

if the returned key is actually the key 0, then the if won't be executed
===================================
jm+phpweb at roth dot lu
29-Aug-2009 06:25
Be careful with stuff like

if ($nkey = array_search($needle, $haystack)) { ...

if the returned key is actually the key 0, then the if won't be executed
php dot net at bgr8 dot com
24-Aug-2009 01:17
an interesting concept, using only double == ... but i'm executing php from the command line on an ubuntu server using php5 ... and if i use ==, the function fails, but if i use = the function works.

i try it with good and bad value pairs echoing "match" to the screen when they match (the executed line) and "match" will not happen with ==. only with =. neither one tosses an error, but the == NEVER shows a match and the = matches when the two strings being compared actually match. this is not something i was expecting after programming in several languages.

go figure.

#!/usr/bin/php
<?php

if ($check = $output)
  echo
"match\n";
?>

obviously there's code to make them match or not match, but that code does not change. i JUST add/remove an = and the code works with =.
strata_ranger at hotmail dot com
05-Apr-2009 10:13
Although most programmers are aware of this already, if for whatever reason you need to 'break' out of an if() block (which, unlike switch() is not considered a looping structure) just wrap it in an appropriate looping structure, such as a do-while(false):

<?php
do if ($foo)
{
 
// Do something first...

  // Shall we continue with this block, or exit now?
 
if ($abort_if_block) break;

 
// Continue doing something...

} while (false);
?>
Anonymous
02-Apr-2009 09:32
If you need to do something when a function return FALSE and nothing when it return TRUE you can do it like that :
<?php
function call()
{
return
FALSE;
}

if(
call()==TRUE) // or if(call())
{
// nothing to do
}
else
{
// do something here
}
?>

You can also write it like this :
<?php
if(!call()==TRUE) // or if(!call())
{
// do something here
}
// here '!' will invert 'FALSE' (from call()) into 'TRUE'
?>
/!\ WARNING /!\
The '!' only work with booleans !
Check http://fr.php.net/manual/en/language.types.boolean.php to know if you can use '!'

If you want to compare two strings and use '!' be careful how you use it !!!!
<?php
$string1
= "cake";
$string2 = "foo";

if(!
$string1==$string2)
{
echo
"cake is a lie";
}
//this will ALWAYS fail without exception because '!' is applied to $string1 and not to '$string1==$string2'

//to work, you have to do like this
if(!($string1==$string2))
{
echo
"cake is a lie";
}
//it will display 'cake is a lie' because ($string1==$string2) return FALSE and '!' will invert it into TRUE
?>
For array/float, it's the same !
contact at bsorin dot romania
08-Mar-2009 01:28
This has got the better part of my last 2 hours, so I'm putting it here, maybe it will save someone some time.

I had a

if (function1() && function2())

statement. Before returning true or false, function1() and function2() had to output some text. The trick is that, if function1() returns false, function2() is not called at all. It seems I should have known that, but it slipped my mind.
Anonymous
28-Sep-2008 09:03
Re : henryk dot kwak at gmail dot com
<?php function message($m)
{
echo
"$m <br />\r";
return
true;
}
$k=false;
if (
message("first")&& $k && message("second")){;}
// will show
//first
class
$k=true;
if (
message("first")&& $k && message("second")){;}
// will show
//first
//second 
?>
john
25-Sep-2008 12:24
@henryk (and everybody):

You should put your arguments in order by *least* likely to be true. That way if php is going to be able to quit checking, it will happen sooner rather than later, and your script will run (what amounts to unnoticeably) faster.

At least, that makes the most sense to me, but I don't claim omniscience.
Wiseguy
28-Aug-2008 11:22
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
jchau at bu dot edu
15-Aug-2008 02: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 09: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 09: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 08: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 07: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...

else> <導入
Last updated: Fri, 06 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites