PHP Basics (Syntax and rationale)

Source: Internet
Author: User
Tags code tag echo date php basics

I. Introduction of php

PHP hypertext pre-processor. is a server script embedded in an HTML file.

PHP code tag: <?php ...? >

PHP file extension:. php

PHP File execution: Must start from the domain name to access

PHP Each statement must end in English (;)

ii. basic knowledge of PHP Web page IP Address

IP address is divided into V4, V6 two versions, V4 length is 32 bit 2 binary code, V6 is 128 bit 2 binary code. V4 version IP in around 2010 has been used up, the main format is 192.168.4.238 (dotted decimal method), composed of four paragraphs, each segment 8 binary, in decimal notation range is: 0-255

Special IP: 127.0.0.1 for native software testing or website testing, only own access to their own 127.

Domain Name Resolver ( DNS server)

It can be understood that the database where the domain name corresponds to the IP address table is stored

The local dns:hosts file, a hidden system file, does not have an extension. When entering a domain name in the browser, first locate the corresponding IP in the local DNS and then the external DNS to find the corresponding IP. Path to the Hosts file: C:\Windows\System32\drivers\etc\hosts

Domain name resolution, Access flowchart

iii. basic PHP Syntax PHP variables

PHP variables must start with "$", for example: $name, $age

Cannot start with a number, and finally follow the hump naming method

Variables written in quotation marks are best enclosed in {} to avoid the occurrence of characters not being parsed at the end

PHP Data Type

scalar data types: string, Integer, floating-point, Boolean

composite data types: arrays, objects

Special Data types: resources, NULL

(1) Integral type

Value range:-2.1 billion ~21 billion

(2) floating Point Type   (can contain integers)

Value range: -1.7e-308~1.7e+308

Because floating-point numbers cannot be converted to exact binary, sometimes errors occur during shipping, for example: (0.7+0.1) *10==8 result is false

(3) String Type (in quotation marks, parse the variable, you need to add a match or a space after the variable)
    1. Double quote ""

The value of the parsed variable within double quotation marks

    1. Single quote "'

The name of the parsed variable within single quotation marks, displayed directly on the client $name

    1. Long string

$STR = <<

....... Fill in the string contents

Heredoc End "Heredoc" must be another line, end of semicolon

(4) Resource-based

Third-party plug-ins and other operations, such as the call to MySQL database, and so on, third-party content called resources

(5) Boolean type

Only true or false two types of values

(6) NULL (empty type)

If the variable does not exist, returns NULL, the null type has only one value, which is null

(7) Array (a set of numbers that look up elements through an index) a) Classification of arrays: 1. indexed Array   (with JS arrays are basically the same)

The subscript of an array is a positive integer starting at 0, which is called an indexed array

$arr =array (10,20,30,40);

$arr [0]=10;

2. Associative Arrays

The subscript of an array is a string , which is called an associative array.

$arr =array ("name" = "Tabb", "sex" = "gender", "Age" and "22");

Because associative arrays do not have integer subscripts, it is not appropriate to use a For loop to traverse

3. Mixed Arrays

Array subscripts are both integer and string, such arrays are called "mixed arrays"

$arr = Array ("name" = "Tabb", "age" = "+", "Tom");

echo $arr [0] The output is "Tom"; the integer subscript is calculated from "Tom"

b) creation of arrays:

1. Create an array using the array () function:

assigning subscript to an array element by the "= =" Reload symbol

If the array element does not specify subscript, its subscript is the current maximum index of +1, example:

$arr =array (20=> "Tabb",2=> "" "," Tom ") where" 20 "index is 21, if it is a character subscript, then do not specify the subscript element, index starting from 0

To create an array using array:

$arr = Array ([$key =>] $value, [$key =>] $value,...);

2. Create an array using [] brackets

$arr ["Key"]= $value

Unlike JS, you can not declare an empty array first, and if the specified array does not exist, the array is created automatically

Creates an array when the contents of the brackets are empty

$arr []=30;

If the array does not exist, an array is created and the first element is added

If the array is present, the subscript of the element that adds the array is the largest in the array subscript +1

c) Multidimensional Arrays:

Array (Array (1,2,3,4), 1,2,3,array (1,2,3,4))

Use the [] bracket to quickly create a multidimensional array:

$arr [] [] []=10; Quickly create a 4-D array

d) array-related functions: 1. Print_r ()

Print easy-to-understand information about variables

If an array or object is given, the keys and elements are displayed in a certain format

2. unset ()

The array element is deleted, the value of the array element is deleted, the subscript is still there, and the length of the array is different from that of JS, when the element is deleted here.

Delete a variable

3. count ()

Count the number of cells in an array or the number of properties in an object

4. foreach () can only be used to iterate over an array

foreach ($arr as [$key =>] $value) {} If it is an indexed array, [$key =>] This content can not

PHP Data type judgment

(1) var_dump ()

Print variable information, you can print multiple variables, separated by "," comma

(2) is_* () a set of methods that determine the type of the variable that returns a Boolean value

    1. Is_bool (): Determine if the variable is Boolean
    2. Is_int (): Determine if the variable is an integral type
    3. Is_float (): Determine if the variable is floating-point
    4. Is_numeric (): Determine if the variable is a numeric type
    5. Is_string (): Determine if the variable is a string type
    6. Is_array (): Determine if the variable is not an array type
    7. Is_object (): Judging if the variable is not an object type
    8. Is_null (): Determine if the variable is not a null type
    9. Is_resource (): Determine if the variable is resource-based

......

(3) isset ()

Detect if a variable is set

Return value: Returns TRUE if the variable exists and is not equal to NULL, otherwise returns false

(4) empty ()

Detects if a variable is empty, such as "", 0, "0", Null,array (), Var $var, and objects without any attributes will be considered NULL, if NULL, the return value is True

Conversion of data types

( 1 ) converted to BOOL value (BOOL)

$a = "0";

$result = (bool)$a;

Var_dump ($result) Result: bool (FALSE)

This data is converted to a bool value of false:0, 0.0, "", "0", array (), NULL

Conversion of resource type to bool is always true

( 2 ) to an integer value (int) (int) $name

1. If a string begins with a numeric value, converts the integer part to an integer value, removing the subsequent character

2. If the string is not the beginning of the value, convert to 0

3.bool value True converts to 1,false conversion to 0,null to 0

( 3 ) to a floating-point value (float) (float) $name

1. If a string starts with a numeric value, converts the number part to a numeric value (containing a floating-point part), removing the subsequent character

2. If the string is not the beginning of the value, convert to 0

3.bool value True converts to 1,false conversion to 0,null to 0

( 4 ) into a string (string) $name

1.null, False to "", empty string

2.true converted to string "1"

PHP operator

( 1 ) numeric operator ( + , - , / , * , % , ++ , -- )

Usage is basically the same as the method used in JS

( 2 ) string operator

“.” The dot operator represents a string link, similar to the concatenation of the "+" sign in JS

$a = "ABC";

$b = $a. 100 or use ". =" to represent yourself with the link string: $a. =100

( 3 ) assignment operator ( = , += , -= , *= , /= , %= ) and JS basically the same

( 4 ) comparison operator ( > , < , >= , <= , == , != , === , !== ) and JS basically the same

Example: 10== "10px" results are: true; in operations that contain numbers, "10px" is first converted to numeric values

Example: 10=== "10px" result is: false; Congruent comparisons, including data types

(5) logical operators ( && , || , ! ) and JS basically the same

(6) ternary operator (expression?) Result 1: Result 2) is basically the same as JS

(7) Operator Precedence

PHP Get form data

<form method= "Get" action= "" >

User name: <input type= "text" name= "username" >

Password: <input type= "text" name= "password" >

<input type= "hidden" name= "ac" value= "Login" >//hidden domain, passing information to the back end

<input type= "Submit" >

</form>

(1) Hyper-Global array variable:$_get[]

Gets the data submitted by the Get method, stored in the Get array

Array (2) {["username"]=> string (6) "abcdef" ["Password"]=> string (5) "12345"}

Get an example of the data: $username =$_get["Name"]

(2) Hyper-Global array variable:$_post[]

Gets the data submitted by the Post method, stored in the post array

PHP the conditions in the Judgment if and the Switch with the JS basically the same

Switch is more efficient than if, and is mainly used to evaluate the value

PHP the Loop statement in

(1) while (conditional judgment) {break} is basically the same as JS

(2) for (conditional judgment) {} is basically the same as JS

(3) Break statement can add break 2 to jump out of a two-layer loop

(4) Continue statement jump out of this cycle, directly start the next cycle

PHP copy values and reference addresses in the

(1) Copy Transfer value

The value of a variable, "copy" a copy, passed to another variable, there is no relationship between the two variables, is independent of the two variables, modify one of the variables, the other variable will not change

PHP The data types that are copy-transmitted are: Character, integer, floating-point, NULL , Array

(2) Reference Address

The data address of a variable, "copy" a copy, passed to another variable, the two variables point to the "same object", if the "object" sent changes, two variables will send a change. So if you modify the value of one variable, the other variable will change as well.

PHP The data type that belongs to the reference address is: Object, resource

manually declare a reference to address: before referring to the variable, with the "&" symbol, so that all the data type implementation of reference to address, such as the following

$a = 10;  $b =& $a; This is where the quoted address of $ A is passed to $b.

PHP the functions in   with the JS basically the same

Examples are as follows:

Function name ($name) {//Functions name cannot be the same, can only exist, and does not have the $ symbol

The Age of Echo "{$name} is 20";

Return

}

( 1 the parameters of the function can pass the reference address:

Function test (& $a, $b) {//Here is passed a $ A reference path

$arr []= $abc;

}

( 2 ) The scope of the function:

Global variables: variables defined outside the function disappear when the Web page finishes executing

Local variables: variables defined inside the function disappear after the function is executed

Unlike JS, in PHP, global variables cannot be used directly inside a function

in the PHP used in functions Global keyword declares a global variable

Using the Global keyword, you can declare a variable as a global variable, where the global variable is not the global variable that is considered in JS

Global Use of keywords:

    1. The global keyword, which can only be used inside a function.
    2. The Global keyword, you cannot assign a value on one side of the world variable declaration. There is no way to do this:

Global $a = 10;

    1. The real meaning of global is "reference address", which is different from the global variable in JS

( 3 ) PHP Date-time functions:

1. Date ();

echo Date ("y-m-d h:i:s"); Output time can be changed to Y year m month D Day

2. Time (); timestamp 1970 years to the current number of seconds (js is milliseconds)

( 4 ) include Grammatical Structure

Description: contains and runs the specified file

Syntax:include $filename or include ($filename);

Example: include "include/conn.php";

( 5 ) Requrie Grammatical Structure

Description: contains and runs the specified file

Syntax:requrie $filename or Requrie ($filename);

Example: Requrie "include/conn.php";

Note:both include and require contain and run the file, but there is a difference. If the included file does not exist, include will report a warning error and the script continues to run down. While require will report a fatal error, the script will immediately terminate execution.

( 6 ) header ()

Description: send a custom HTTP message, that is, the data format or character set returned by PHP

syntax:void Header (string $string)

Example:

Header ("Content-type:text/html;charset=utf-8");

Header ("location:http:www.baidu.com"); page Jump

PHP Basics (Syntax and rationale)

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.