<object id="xwuag"></object>
      1. <tbody id="xwuag"><pre id="xwuag"><video id="xwuag"></video></pre></tbody>
        <progress id="xwuag"></progress>

        <nav id="xwuag"><big id="xwuag"><rt id="xwuag"></rt></big></nav>
        <em id="xwuag"></em>

        <nav id="xwuag"></nav><rp id="xwuag"></rp>
        <li id="xwuag"></li>
      2. <rp id="xwuag"></rp>
        <s id="xwuag"></s><th id="xwuag"><pre id="xwuag"><video id="xwuag"></video></pre></th>
      3. <nav id="xwuag"><center id="xwuag"></center></nav>
          <dd id="xwuag"><big id="xwuag"></big></dd>

          if

          (PHP 4, PHP 5, PHP 7)

          if 结构是很多语言包括 PHP 在内最重要的特性之一,它允许按照条件执行代码片段。PHP 的 if 结构和 C 语言相似:

          <?php
          if (expr)
            statement
          ?>
          

          如同在表达式一章中定义的,expr 按照布尔求值。如果 expr 的值为 TRUE,PHP 将执行 statement,如果值为 FALSE ——将忽略 statement。有关哪些值被视为 FALSE 的更多信息参见转换为布尔值一节。

          如果 $a 大于 $b,则以下例子将显示 a is bigger than b

          <?php
          if ($a $b)
            echo 
          "a is bigger than b";
          ?>

          经常需要按照条件执行不止一条语句,当然并不需要给每条语句都加上一个 if 子句。可以将这些语句放入语句组中。例如,如果 $a 大于 $b,以下代码将显示 a is bigger than b 并且将 $a 的值赋给 $b

          <?php
          if ($a $b) {
            echo 
          "a is bigger than b";
            
          $b $a;
          }
          ?>

          if 语句可以无限层地嵌套在其它 if 语句中,这给程序的不同部分的条件执行提供了充分的弹性。

          add a note add a note

          User Contributed Notes 20 notes

          up
          188
          robk
          6 years ago
          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 ?>
          up
          34
          Christian L.
          8 years ago
          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.
          up
          25
          grawity at gmail dot com
          11 years ago
          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.
          up
          19
          techguy14 at gmail dot com
          8 years ago
          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!
          up
          16
          Donny Nyamweya
          8 years ago
          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')
          up
          3
          cole dot trumbo at nospamthnx dot gmail dot com
          2 years ago
          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;
          ?>
          up
          -20
          austinderrick2 at gmail dot com
          9 years ago
          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
          ===================================
          up
          -32
          sofwan at sofwan dot net
          7 years ago
          It seems that only numbers can be compared between them but actually an alphabet can be compare too. For example :

          <?php
          // Number comparison
           
          $a="C";
           
          $b="X";
            if (
          $a<$b)
               {
              echo
          $a."is smaller than".$b;
              }               
          // Result : C is smaller than X
          ?>
          up
          -20
          redrobinuk at aol dot com
          11 years ago
          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...
          up
          -36
          brian at webdesignacademy.co.za
          4 years ago
          You can also check alphabet characters like this

          <?php
          // Alphabetical Comparison
           
          $a="brian";
           
          $b="zebra";
                if (
          $a < $b){
                  echo
          $a." is before ".$b." in the alphabet";
                }
                else{
                    echo
          $a." is after ".$b." in the alphabet";
                }
          // Result : brian is before zebra in the alphabet
          ?>
          up
          -36
          chrislabricole at yahoo dot fr
          10 years ago
          You can do IF with this pattern :
          <?php
          $var
          = TRUE;
          echo
          $var==TRUE ? 'TRUE' : 'FALSE'; // get TRUE
          echo $var==FALSE ? 'TRUE' : 'FALSE'; // get FALSE
          ?>
          up
          -24
          admin at leonard !spam challis dot com
          8 years ago
          When using if statements without the curly braces, remember than only one statement will be executed as part of that condition. If you want to place multiple statements you must use curly braces, and not just put them on the same line.

          <?php

          if (1==0) echo "Test 1."; echo "Test 2";

          ?>

          Whereas some people would expect nothing to be displayed, this piece of code will show: "Test 2".
          up
          -29
          contact at bsorin dot romania
          10 years ago
          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.
          up
          -32
          john
          10 years ago
          @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.
          up
          -35
          Anonymous
          10 years ago
          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 
          ?>
          up
          -38
          Rudi
          8 years ago
          Note that safe type checking (using === and !== instead of == and !=) is in general somewhat faster. When you're using non-safe type checking and a conversion is really needed for checking, safe type checking is considerably faster.

          ===================================
          Test (100,000,000 runs):
          <?php
          $start
          = microtime(true);
          for(
          $i = 0; $i < 100000000; $i++)
              if(
          5 == 10) {}
          $end = microtime(true);
          echo
          "1: ".($end - $start)."<br />\n";
          unset(
          $start, $end);

          $start = microtime(true);
          for(
          $i = 0; $i < 100000000; $i++)
              if(
          'foobar' == 10) {}
          $end = microtime(true);
          echo
          "2: ".($end - $start)."<br />\n";
          unset(
          $start, $end);

          $start = microtime(true);
          for(
          $i = 0; $i < 100000000; $i++)
              if(
          5 === 10) {}
          $end = microtime(true);
          echo
          "3: ".($end - $start)."<br />\n";
          unset(
          $start, $end);

          $start = microtime(true);
          for(
          $i = 0; $i < 100000000; $i++)
              if(
          'foobar' === 10) {}
          $end = microtime(true);
          echo
          "4: ".($end - $start)."<br />\n";
          unset(
          $start, $end);
          ?>

          ===================================
          Result (depending on hardware configuration):
          1: 16.779544115067
          2: 21.305675029755
          3: 16.345532178879
          4: 15.991420030594
          up
          -7
          christian johansson
          6 months ago
          It can be tricky to know what commands are executed if these expressions are stacked, here is an overview, general rule is that only the first statement following a true conditional will be executed.

          php > if (true) if (true) if (true) echo "1 "; echo "2 "; echo "3 "; echo "4 ";
          1 2 3 4

          php > if (true) if (true) if (false) echo "1 "; echo "2 "; echo "3 "; echo "4 ";
          2 3 4

          php > if (true) if (false) if (false) echo "1 "; echo "2 "; echo "3 "; echo "4 ";
          2 3 4

          if (false) if (false) if (false) echo "1 "; echo "2 "; echo "3 "; echo "4 ";
          2 3 4
          up
          -10
          phphlx at one six three dot com
          9 months ago
          $x = 1;
          $y = 2;
          if ($x != $y)
            $x = $x * $y;
          else
            $x = $x + $y;

          //can you guess the answer?
          echo $x; #2
          up
          -8
          ganzales at inbox dot ru
          6 months ago
          <?php
          function b() {
              echo
          'b';
          }

          $a = true;
          $a && b(); //b

          $a = false;
          $a && b(); //
          up
          -24
          johovich at yandex dot ru
          1 year ago
          'IF' STATEMENT WRONG BEHAVIOR
          If assign var to function that returns 0 as integer or 0 as string 'if' statement condition works as false.

          Test script:
          ---------------
          //In this script you can see, that set var value to 0 is equal to boolean false
          //applied to if statement. There is no diff between integer 0 or string '0'

          if($pos = 0){
              $pos++;
          } else {
              $pos = 0;
          }
          var_dump($pos);

          //$pos is not boolean false, so it should do if condition true, but it's not

          //to make this work well i use this
          $pos = 0;
          if($pos !== false){
              $pos++;
          } else {
              $pos = 0;
          }
          var_dump($pos);

          備份地址:http://www.lvesu.com/blog/php/control-structures.if.php

          国产成人午夜福利r在线观看_欧美性受xxxx孕妇_白丝娇喘过膝袜爽短裙调教_jizz jizz xxxx