PHP Basic Syntax

Source: Internet
Author: User

Variables: 1. Variable declaration: Starts with $, consists of numbers and letters, numbers cannot be in front.

    • Output: Echo//Output to page, cannot output complex type
    • Print_r ()//can print complex types
    • var_dump//can print detailed information about complex types
    • Print_r and Var_dump are mainly used for commissioning
    • PHP is also a weakly typed language

3. Arrays

  • A. Create array $arr=array (1, "a", 3);//elements can be of different types
  • Another form of array initialization
    $arr [] = ' Hello ';
    $arr [] = ' Hi ';
    $arr [] = ' Nihao ';

  • B. Two-dimensional array:

    Two-dimensional arrays
    $arr = Array (
    Array (123,456,789),
    Array (111,222,333),
    Array (32423,22322,33233),
    Array (213123,234324,345345)
    // );
    Another way to initialize a two-dimensional array
    $arr [] = array (123,456,789);
    $arr [] = array (123,456,789);
    $arr [] = array (2342,456,78234239);
    $arr [] = array (123,456,789);
    $arr [] = array (123,456,789);

  • C. Two-D arrays
  • Associative arrays
    $arr = Array (' attr1 ' = ' hello ', ' attr2 ' = ' Hi ', ' attr3 ' = ' Nihao ');
    $arr [] = ' Tom ';
    $arr [] = ' Jerry ';
    echo count ($arr); 5ATTR1-----Hello
    ATTR2-----Hi
    ATTR3-----Nihao
    0-----Tom
    1-----Jerry

  • D. Array method://php Built-in function count (), the function is to calculate the length of the array
  • The function of the Array_push () function is to append an element to the end of the array
    Array_push ($arr, 5);
    Array_push ($arr, ' hello ');
    The action of the Array_pop () function is to take out the last element of the array
    $ret = Array_pop ($arr);

4.//traversing an array

    • $arr = Array (123,456,789);
    • for ($i =0; $i < count ($arr); $i + +) {
      echo $arr [$i];
      // }

    • The $k here refers to the index of the array, $v refers to the index corresponding to the value

foreach ($arr as $k = = $v) {
Echo $k;
Echo ' <br> ';
Echo $v;
}

    • Traversal of two-dimensional arrays
    • $arr = Array (
      Array (123,456,789),
      Array (111,222,333),
      Array (32423,22322,33233),
      Array (213123,234324,345345)
      // );
    • foreach ($arr as $value) {
      foreach ($value as $v) {
      echo $v. ' <br> ';
      }
      }

5. Use of string concatenation

    • The string concatenation used in PHP is.
    • PHP has a difference between single and double quotes: double quotes parse the variables in the string, but single quotes do not
    • $num = 123;
    • $str = ' Hello ';
    • echo $str. '----------&nbsp;----'. $num;
    • echo ' Hello $num ';
    • echo "Hello $num";

6. Functions

    • Declaration of a function
    • function foo ($n) {
      $sum = 0;
      for ($i =0; $i <= $n; $i + +) {
      $sum + = $i;
      }
      return $sum;
      }

      $ret = foo (100);
      Echo ' <div> ' $ret. ' </div> '

7. Garbled problem:

    • The function of this sentence is to tell the browser to receive this page, as a Web page display and use UTF-8 encoding display

Header (' content-type:text/html; Charset=utf-8 ');

PHP Basic Syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.