Getting started with PHP Basics


Variables and scopes

  1. $globals [index]. Index holds the name of the variable. This array can be accessed inside a function or used directly to update global variables.
  2. Static scope
    Keep a local variable from being deleted

  3. Parameter scope

Echo and print

  1. Echo – can output one or more strings
  2. Print – only one string is allowed to be output, and the return value is always 1 (with newline)

    Tips:Echo output speed is faster than print, echo has no return value, print has a return value of 1.


  1. Must be followed by a semicolon, otherwise compilation fails.
  2. EOF can be replaced by any other character, just to ensure that the end ID is consistent with the start ID.
  3. The end tag must have a single line at the top (i.e. it must start at the beginning of the line, and no white space and characters can be connected before and after).
  4. The start identification can be without quotation marks or with single and double quotation marks. The effect of without quotation marks is the same as that of with double quotation marks. The embedded variables and escape symbols are interpreted, while the embedded variables and escape symbols are not interpreted with single quotation marks.
  5. When the content needs to be embedded in quotation marks (single quotation marks or double quotation marks), there is no need to add escape characters, and the single and double quotation marks are escaped by themselves, which is equivalent to the usage of Q and QQ here.
    EOT, EOD and EOF are usually used in uppercase, but not limited to those (JSON, HTML, etc.) as long as the start tag and end tag are not in the body.
    Variables between the start and end tags can be parsed normally, but functions cannot. In heredoc, variables do not need to be spliced with connector. Or

data type

  1. Var_dump() returns the type and value of the variable
  2. Array: $x = array (1, 2, 3);
  3. Boolean: $x = true;
  4. Null value: $x = null;
  5. Object: Class

Type comparison (not finished)

  1. ==: compare values only
  2. ===: compare types and values


  1. Set constant: define (name, value, case_insensive)
    • Name: required parameter, constant name, i.e. flag.
    • Value: required parameter, constant value.
    • Case_insensitive: optional parameter. If it is set to true, the constant is not case sensitive. The default is case sensitive.
  2. Constants are global
    Function can be used directly constant, no keyword!

String variable

  1. Collocate operator: ‘
  2. String length: strlen()
  3. Location of query character: strpos (STR1, STR2)


  1. arithmetic
  2. assignment
  3. Up / down
  4. compare
    • Not equal to:! And < >
    • Absolutely not equal to:! = = (different value and type)
  5. logic
    • And: and & &
    • Or: or
    • Non: XOR!
  6. Array operation
    • Set: +
    • Judgment: = =, ====,! =, < >==
  7. Three yuan:
    • a1? a2: a3
    • Php5.3 +: middle expression can be omitted
  8. Php7 + operation
    • ??
  9. Operator priority
  10. Parentheses change operator precedence

Conditional statement

  • If statement – executes code when conditions hold
  • If… Else statement – executes one block of code when the condition is established and another when the condition is not established
  • If… Else if…. else statement – executes a block of code when one of several conditions holds
  • Switch statement – executes a block of code when one of several conditions holds


  1. Create array: array()
  2. There are three types of arrays in PHP:
    • Numeric array-Array with numeric ID key
    • Associative array-An array with specified keys, each associated with a value
    • Multidimensional array-An array containing one or more arrays
  3. Get the length of the array: count ($variable name)
  4. Traversal (numeric and associative traversal methods are different)

Array sorting

  • Sort () – ascending array
  • Rsort() – descending array
  • Asort () – ascending the array according to the value of the associated array
  • Ksort () – ascending the array according to the key of the associated array
  • Arsort() – arrange the array in descending order according to the value of the associated array
  • Krsort() – arrange the array in descending order according to the key of the associated array

PHP super global variable

  • $_SERVER
  • $_POST
  • $_GET
  • $_FILES
  • $_ENV
  • $_COOKIE


  • While – loop the code block as long as the specified condition is true
  • Do… While – executes the code block once, and then repeats the loop when the specified condition holds
  • For – the number of times the code block is executed in a loop
  • Foreach – loop the code block according to each element in the array (for traversing the array)
foreach($array as $value)
   To execute the code;


function name(){}


magic constant


Namespace (include for importing files)

  1. namespace MyProject{ }
  2. Encoding method of declaration namespace: declare (encoding =’utf-8 ‘)
  3. Fatal error before namespace – namespace must be the first statement of the program script
  4. Subnamespace:
    Follow the relationship between files and directories
    Namespace myproject \ sub \ level; // declare a hierarchical single namespace
  5. Use of namespaces
    • Unqualified name or class name without prefix
    • Qualified name, or name with prefix
    • Fully qualified name, or the name containing the global prefix operator
  6. Namespace operator and namespace__
  7. Use operator: import as
    Use only affects unqualified and qualified names, not fully qualified names
  8. There is no difference between qualified and fully qualified names in dynamic types (file and file calls each other)
  9. Global space: Using


  1. $this: object of class itself
  2. Member variables are defined by VAR
  3. Create object: $a = new car;
  4. Object call: – >
  5. Constructor:

    function __construct($par1, $par2){
    $this->url = $par1;
    $this->url = $par2;
  6. Destructor:

    function __destruct(){}
  7. Inheritance: (PHP does not support multiple inheritance)

    class Car extends Parent{
    //Code part
  8. Access control: public, protected, private
    Public and protected can be redefined in inheritance, but private cannot
  9. Interface
    • With an interface, you can specify which methods a class must implement, but you do not need to define the contents of those methods.
    • Interface is defined by interface keyword, just like a standard class, but all methods defined in it are empty.
    • All methods defined in an interface must be public, which is a feature of the interface.
    • To implement an interface, use the implements operator.
    • Class must implement all methods defined in the interface, otherwise a fatal error will be reported. Class can implement multiple interfaces, separating the names of multiple interfaces with commas.
  10. Constant: const

  11. abstract class

    abstract class AbstractClass
    //Force subclasses to define these methods
    abstract protected function getValue();
    abstract protected function prefixValue($prefix);
    //Common method (non abstract method)
    public function printOut(){
        print $this->getValue() . PHP_EOL;

    In addition, a subclass method can contain optional parameters that do not exist in the parent abstract method.

  12. Static keyword
    • Declare the class property or method as static, and you can access it directly without instantiating the class.
    • Static properties cannot be accessed through an instantiated object of a class (but static methods can).
    • Because static methods do not need to be called through objects, the dummy variable $this is not available in static methods.
    • Static properties cannot be accessed by objects through the – > operator.
    • Since PHP 5.3.0, classes can be called dynamically with a single variable. But the value of the variable cannot be the keyword self, parent or static.
  13. Final keyword
    • If a method in the parent class is declared final, the child class cannot override the method. If a class is declared final, it cannot be inherited.
  14. Call the parent class constructor
    • PHP does not automatically call the constructor of the parent class in the constructor of the child class. To implement the parent class’s construction method, you need to call parent:: __construct () in the construction method of the subclass.