Basic PHP Learning

Source: Internet
Author: User
Tags arithmetic operators array sort closing tag define function
Overview

The purpose of learning PHP is to extend the server-side development capabilities. and learning Web-based server development can also be considered very good learning.

Learning to feel language is the process: basic knowledge, advanced features, standard library learning, and extended library learning. The best way to learn is to do more hands-on and write code.

The basics are: annotations and descriptions, variables and constants, type systems, operator systems, process controls, file reads and writes, array descriptions, string manipulation and regular expressions, function systems, object-oriented functions, and error exception control.

Finally, the experience of PHP: in grammar, PHP borrowed a lot of C + + elements, so it is good to learn, at least more than to see Erlang strong. In this is the understanding of how PHP works: Because originally wrote a simple server, feel Apache/nginx responsible for all the underlying work such as network, synchronization and other issues, and PHP is specialized in processing the logic processing part. That way, it's a lot easier than I originally wrote the server-perhaps the focus of two jobs is different.

Points

--Comments and descriptions:

The file suffix is typically. php or. Inc (include abbreviations).

PHP can be embedded in HTML, the markup style is:

(XML style-recommended);

(short style);

(script style);

<%.%> (ASP style).

Handling of whitespace: As with html PHP also ignores whitespace characters-carriage returns, spaces, Tab.

Comments:

Single line://And #.

Multiple lines:/* ... */mode.

PHP's statement is the same as C + +, as a closing tag.

Ways to Access form variables:

Short style? $tireqty, easy to use server configuration, not recommended.

Medium style? $_post[' Tireqty '), recommended for use.

A lengthy style? $HTTP _post_vars[' Tireqty '), deprecated.

Echo is used for echoing output;

The string join operator is a dot (.).

Where a string represents a single quotation mark and double quotation marks are different: single quotation marks denote the literal meaning of the output that does not do the processing (very individual transfers), and the double quotation marks handle the string-processing the transfer, and so on.

--Variables and constants:

Identifiers--the same as C + + naming:

Length Unlimited but not recommended too long;

Consists of letters, numbers, and underscores, but cannot begin with numbers;

Case-sensitive, but function names are not case-sensitive;

Variable names can have the same name as function names-strongly not recommended.

The format of the variable name, such as $arge, requires a $ prefix.

Definition of constants: Define (' Tireprice ', +);(recommended word Embox Uppercase)

Variable variables can be used: $a = ' Feng ', $ $a equivalent to $feng.

Scope of the variable:

Super Global variables: such as $_post are visible throughout the script;

Constants are also visible throughout the script;

The variables defined in the script are visible to the script, but not visible within the function;

Variables defined within the function are local and not visible outside the function;

Global variables that are visible inside and outside the function can be defined by the keyword global;

Common super-global variables are:

$GLOBALS, array of all global variables;

$_server, an array of server environment variables;

$_get, an array of variables passed to the script by the GET method;

$_post, an array of variables passed to the script via the POST method;

An array of $_cookie,cookie variables;

$_files, an array of variables related to file uploads;

$_ENV, environment variables;

$_request, an array of variables entered by all users, including $_get,$_post,_cookie.

$_session, reply to the variable array.

--Type System:

The supported types are: integers, floating-point numbers, strings, booleans, arrays, and objects.

The special types supported are: NULL (for unassigned, etc.) and resource (a data structure passed between functions, such as open files, database connections, etc.).

Conversions between types are used for C-style $a= (float) 10.

You can also use built-in functions for type conversions.

For strings: There is a single-quote style and a double-quote style. The string has corresponding manipulation functions and operators.

Built-in pair type support functions:

GetType () and Settype () can test and set variable types;

The correlation judgment also has the is_* () series function;

Isset ()--test whether the definition and unset ()--delete the variable;

Variable conversions: intval (), Floatval (), Strval ().

--Operator System:

Arithmetic operators: Plus (+) minus (-) multiplication (*) except (/) and remainder (%);

string operator: dot operator (.) ;

Assignment operators: = and mixed assignment operators (+=,-=,*=,/=,%= and. =);

Increment and decrement operators: As with C + + is the same prefix form and suffix form;

Reference syntax: Use the address character (&) before the variable;

Comparison operators: increased compared to C + + (==,!=,<,<=,>,>=) (===,!==,<>);

Logical operators: Symbols (!,&&,| |) and character form (AND,OR,XOR);

Bitwise operator:&,|,~,^,<<,>> (not commonly used);

Other operators:

Ternary operator: form is condition? True:false;

Error suppression operator: Use the @ symbol before the statement;

Execute operator (execute terminal command): Use symbol inverted single quotation mark--next to 1 on keyboard;

Array operators: using symbols [];

Type operator: use instanceof to operate;

The precedence and associativity of operators is similar to C + +.

--Process Control:

Conditions:

If statement form: if (cond) {}elseif (cond) {}else{};

The switch statement is the same as C + +;

Cycle:

While,for and Do...while are the same as C + +;

The array also provides support for the Foreach Loop statement;

Jump:

In addition to the Break,continue also added exit--launch script;

Replaceable form:

Do not use curly braces instead of characters, such as if (cond) ... endif form;

The use of replaceable forms is: If,switch,while,for and foreach;

Using declare:

Not commonly used, but where the ticks need to learn to use;

--File Read and write:

Common functions:

Open file: fopen (), which can choose to open the file mode;

Close file: fclose () function;

Read files: fread (), file_get_contents (), fgets (), FGETSS (), Fgetcsv (),

ReadFile (), Fpassthru () and file (), fgetc ();

Write file: fwrite (), file_put_contents ();

Delete file: unlink ();

Control functions:

File Status judgment: Feof (), file_exists (), FileSize ()

Lock file: Flock ();

File read-write pointer operation: Rewind (), fseek () and Ftell ();

--Array Description:

Unlike the concept of arrays in C + +, it is somewhat similar to a dictionary in Python;

Numeric index array:

The numeric index starts at 0 and is defined in the form of array ();

A function that automatically generates an array has a range ();

You can use foreach () to iterate through an array-a numeric index and a non-numeric index are also used;

The syntax for foreach () is foreach ($array as $key + = $value);

Array operator:+,==,===,!=,<>,!==;

You can define multidimensional arrays;

Array sort function:

Support functions sort (), Asort (), Ksort (), Rsort (), Arsort (), and Krsort ();

A multi-bit array usort () requires a user-defined comparison function and is used as a parameter;

Random ordering of an array: Shuffle () function;

The array is reversed: the Array_reverse () function;

The function file () can directly return an array of text, which can then be manipulated directly into the text array;

Other array operations:

Array browsing: each (), current (), reset (), end (), Next (), POS (), and Prev ();

A specific function can be executed for each element in the array: Array_walk ();

Array Statistics: count (), sizeof (), and array_count_values ();

The array is converted to a scalar: extract () function;

Explanations for non-numeric indexes:

First of all, in the definition of different, followed by different methods of access caused by the different traversal method;

At the same time the other array functions are still applicable;

--string and regular expressions:

String functions:

Format: RTrim (), LTrim (), Trim (), nl2br (), printf (), sprintf (), vsprintf (),

Strtoupper (), Strtolower (), Ucfirst (), Ucwords (),

Addslashes (), stripslashes ();

Connect and split: Explode (), implode (), join (), Strtok (), substr ();

Comparisons: strcmp (), strcasecmp (), strnatcmp (), strlen ();

Match and replace: Strstr (), STRCHR (), STRRCHR (), Stristr (), Strpos (), Strrpos (),

Str_replace (), Substr_replace ();

Regular Expressions:

Pending

--Function System:

Introduction File:

You can use the require () and include () functions to introduce files;

Variant functions require_once () and include_once ()--ensure that only one is introduced;

Configure headers and footnotes using the server configuration Auto_prepend_file and auto_append_file;

Using functions:

Functions are called in the same way as in other languages such as C + +--non-member functions;

Calling an undefined function will get an error message;

Function names are case-insensitive;

It is also important to note that the function name does not have a $ prefix;

Custom functions:

Define function Syntax: Functions func_name ($arg 1, $arg 2) {...} Form

The parameters are also divided into the values (default) and the reference (prefixed & conforming to the referential semantics) two ways;

return value with Renturn statement;

Recursion can be used, but it is not recommended for performance reasons;

Namespace issues:

You can use namespaces in PHP--to learn in depth;

--Object oriented:

Classes and objects:

Class definition mode: Class classname{...} Form

Access control has public (default), protected and private;

Class instantiation (Object) method: $a the form of =new classname (parameter);

Constructor (__construct ()) and destructor (__destruct ()) Form;

You can use the $this pointer--to represent the object itself;

You can define __get () and __set () two functions-accessors and sets;

The access symbols for class properties and methods are arrow symbols (-);

Inherited:

The inheritance form uses the keyword "extends" to indicate inheritance;

Multi-inheritance is not supported, and the effect of multiple inheritance can be realized by interface.

Inheritance and overloading can be forbidden through the final keyword-classes and methods are applicable;

Overloads--Overloads of parent and child classes:

Defining a function with the same name as the parent class in the subclass will overload the parent class function;

The invocation of the member function calls the appropriate object for the type of the calling object;

The method of invoking the parent class object is using Parent:: Form--but it is important to note that although the call is

function But if an overloaded member is also called within a parent function, the child class member is preferred--unlike C + +;

Interface:

Interface definition mode: interface infname{...} Form

The implementation of the interface requires the use of the keyword "implements" form;

Advanced Features:

Use Pre-class constant (static data member): Class Math{const pi=3.14159} form;

The static method uses the keyword "static" and is called by the symbol "::" and has no $this keyword;

The type of object can be checked by instanceof;

Supports lazy static binding--to be in depth (not used);

Copy objects by keyword $a= clone $b;

And the copy function is defined in the class by __clone ()-Because of the need to refer to the member;

Abstract class definition methods are: abstract methods and abstract classes--all prefixed with "abstract";

Overloads of class methods can be provided by __call ()--overloads in the same class;

Independent function __autoload ():

will be called automatically when instantiating a class that is not yet declared;

The primary purpose is to attempt to include or request any files that are used to initialize the required classes;

Iterators and Iterations:

To implement an iterator, you need to implement the Iteratoraggregate interface and define the Getiterator ();

Need to implement complex behavior to implement iterator interface;

Class into a string:

Implemented using the __tostring () function;

Using the Reflection (reflection) API:

The reflection technology should be the inverse class structure from the instance;

API interface is $class=new Reflectionclass ("Empl");

--Errors and exceptions:

Abnormal:

Exception mechanism is try{...} catch (...) {...} and throw form;

PHP provides a built-in class for exceptions? Exception;

Custom Exceptions:

You can customize the inheritance exception to define your own exceptions;

Other error handling mechanisms:

In addition to the exception handling mechanism, PHP provides complex error handling support-to learn in depth;

Summary

The basic part is done; you can go further.

  • Related Article

    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.