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

search for in the

else> <Контролни структури
Last updated: Fri, 27 Jun 2008

view this page in

if

Конструкцията if (ако) е едно от най-важните свойства на много езици, включително и на PHP. Тя позволява условно изпълнение на части от кода. PHP предоставя структура if, подобна на тази в C:

if (expr)
    statement

Както е описано и в раздела за изразите, expression се изчислява до булевата си стойност. Ако expression се изчисли на TRUE, PHP ще изпълни statement, а ако се изчисли на 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;
}
?>

Kонструкциите if могат да бъдат вмествани безкраен брой пъти в други конструкции if, което ви дава пълна гъвкавост за условното изпълнение на различни части от вашата програма.



else> <Контролни структури
Last updated: Fri, 27 Jun 2008
 
add a note add a note User Contributed Notes
if
Wiseguy
28-Aug-2008 04: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
14-Aug-2008 07: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 02: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 02: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 12: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 11: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, 27 Jun 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites