Getting Started with PHP (c)

1. Constant

  After the constant value is defined, is not to be changed at any elsewhere in the script, a constant consists of letters, underscores, and numbers, but the numbers can not appear as the first letter. (Constant names do not need to add $ modifier).

  Note: Constants can be used throughout the script.

  Setting constants, using the define () function, the function has the following syntax: BOOL DEFINE ( String $ name , Mixed value $ [, BOOL $ CASE_INSENSITIVE = to false ] )

  The function takes three parameters:  name:Required parameters constant names, i.e., identifiers. value:Required parameter values, constants. CASE_INSENSITIVE : optional parameter, if set to TRUE, this constant is not case sensitive. The default is case-sensitive.

  Example:

  <div class = "Box">
    <PHP?
      the DEFINE ( 'LIANG', 'Welcome to the PHP world'); // If the third argument without default is false
      echo LIANG; // output: Welcome to the PHP world
      echo '<br>';
      echo Liang; // output: liang because case-sensitive, so LIANG not recognize this constant defaults to echo a content
    >?
  </ div>
  <div class = "Box">
   <PHP?
    define ( 'LIANG', 'Welcome to the PHP world', to true);
    echo LIANG; // output: Welcome to the world PHP
    echo '<br>';
    echo Liang; // output: Welcome to the world PHP
    // the above is provided as a third parameter is true, so here is not case sensitive, it is possible to identify constant LIANG
   ?>
  </ div>

  After defining the constants, the default is a global variable can be used anywhere in the entire run of the script. Even constants defined may be outside a function in the function of normal use constants.

  Such as:  

    <div class="box">
      <?php
        define("hermit", 'php,你好!', true);

        function hua(){
          echo hermit;
        }

        hua (); // output php, hello!
      ?>
    </ Div>

 

2. string variable

  String variable for storing and processing the text. A character string variable with a value. After you create a string we can manipulate it up. You can use directly in a function string, or store it in a variable.

 

3. The concatenation operator

  In PHP, there is only one string operator. Concatenation operator (.) Is used to connect the two string values.

  如:<div class="box">

      <? PHP
        $ text1 = "Welcome";
        $ text2 = "came to the PHP world!";
        . $ echo $ text1 text2;
      ?>
    </ div>

4. strlen () function

  Sometimes you know the length of the string value is useful. strlen () function returns the length of the string (number of characters). strlen () is often used in the cycle and other functions.

  Such as:

  <div class = "Box">
    <PHP?
      $ text3 = "Liang Tuhua"; // Note that spaces are also considered a position
      echo strlen ($ text3); // output: 11
    ?>
  </ div>

5.strpos () function

  the strpos () function is used to find a character string or within a specified text. If a match is found in the string, the function returns a match for the first character position (subscript). If a match is not found, FALSE is returned.

  Such as: ? <PHP

      text4 = $ "Liang Tuhua";
      echo strpos ($ text4, "Tuhua"); // output 6, starting at index 0, including spaces
    >?

6. Operator

  1. Arithmetic Operators

    + (Addition)  - (subtraction)    * (multiplication)   / (division)   % (modulus, also known as remainder)    (juxtaposed).  

    

  PHP7 + version of the new operator divisible  intdiv () , such as:

 

  <?php

 

     var_dump ( intdiv ( 10 3 ) ) ; // Output: 3 (the latter is not to take the integer.)

 

  ?>

 

  2. assignment operator

    = (Assignment) + = (plus etc.)   - = (minus the like)  * = (multiplication, etc.)  / = (except the like)   % = (mold etc.)   = (and the like).  Example: a = b = A corresponds. a. b

  3. increment / decrement operators

    ++ i (pre-increment, I before adding 1, then return to I)  I ++ (post-increment, the first return I , in I plus 1  )   Inc. (www.i-levelmedia.com) (pre-decrement, I is decremented by 1 first, and then returns I )   I - ( after descending, first return to I , I is decremented by 1 in ) 

  4. Comparison Operators

 

    == (equal)    === (absolute equal to)   ! = (Not equal to) <> (not equal to)   ! == (absolutely not equal)  > (greater than)   <(less than)    > = (greater than or equal to)   <= ( less)

  The logical operators

    and b (and also known as and)  if a and b are true, then returns to true   a or b (or a) if a and b have at least one true, then returns to true a XOR b (XOR) if a and b and only one is true, return true   

    a &&  b (and also known and)   if a and b are true, then return TRU E    a || b (or a)if at least one of a and b is true, then returns to true! a (non) if a is not true, return true    

 

 

  6. The ternary operator (also called operation trinocular)

    (expr1) ? (expr2) : (expr3)  

    Expr1 expr2 is evaluated for the time is TRUE, the value at the time of expr3 expr1 evaluates FALSE. Since PHP 5.3 onwards, the ternary operator can omit the intermediate portion. Expression expr1:? Expr3 returns expr1 when expr1 evaluates to TRUE, otherwise returns expr3.

  7. The combination of comparison operators (PHP7 +)

    PHP7 + comparison operator support composition (combined comparison operator), also known spacecraft operator, symbol  <=> . Comparison operator can easily compare the combination of two variables, of course, not limited to the value of class data comparison.

    If $ a> $ b, $ c is a value of 1. If $ a == $ b, $ c is a value of 0. If $ a <$ b, $ c is a value of -1.

    Such as:  Note: Use the accident report will appear in php '>' error on the version of less than PHP7 ,

      $a = 8;

      $b = 5;

      $c = $a <=> $b;

      echo: $ c; // Output 1

  8. Operator Priority

  

 

   9. the If ... Else statement

    if statement  - execute code when conditions are met

    if ... else statement  - execute code when a condition is satisfied, another piece of code is executed when the condition is not satisfied

    if ... elseif .... else statement  - execute a block of code // when one of several conditions are met elseif and  else if the effect is the same, elseif  is PHP is  else if  a special edition achieve fault tolerance. Strict wording for the latter:  the else IF

    switch statement  - execute a block of code when one of several conditions are met

    Such as: the use of javascript and the same.

    ? <PHP
      $ = n-2;
      Switch (n-$) {
        Case. 1:
          echo "two. 1";
          BREAK;
        Case 2:
          echo "three 2";
          BREAK;
        Case liang3:
          echo "four. 3";
          BREAK;
        default:
          echo "no number";
      }
    >?

 

  

  

 

Guess you like

Origin www.cnblogs.com/hermit-gyqy/p/11592712.html