PHP Learning Notes Basics, PHP learning notes _php Tutorials

Source: Internet
Author: User

Basic knowledge of PHP learning notes, PHP learning notes


PHP learning so far more than a year, notes accumulated a lot of, but also quite miscellaneous, write an article to organize it.

Basic PHP Section

PHPBasic instructions for outputting text: echo and print .

The difference between Echo and print

echois a PHP statement, print and print_r is a function, the statement does not return a value, the function can have a return value (even if it is not used)

echoOutputs one or more strings.
printCan only print out values for simple type variables (such as int,string)
print_rYou can print out values for complex type variables (such as arrays, objects)

The difference between Var_dump and Print_r

var_dumpReturns the type and value of an expression and print_r returns only the result, which is easier to read than the debug code var_dump .

Variable

Variables are used to store values, such as numbers, text strings, or arrays. All variables in PHP start with the $ symbol.
The PHP variable name is case sensitive!

PHP has three different scope of variables:

local(局部)
global(全局)
static(静态)

Variables declared outside the function have Global scope and can only be accessed outside of the function.

Variables declared inside the function have LOCAL scope and can only be accessed inside the function.

globalKeywords are used to access global variables within functions.

PHP Static Keywords

Typically, all variables are deleted when the function finishes/executes. However, sometimes I need to not delete a local variable. Achieving this requires a bit of further work.

To do this, use the static keyword when you first declare a variable:

<?php    
function myTest() {
static $x=-1;
echo $x;
$x--;
}
myTest();//-1
echo "
";
myTest();//-2
echo "
";
myTest();//-3
?>

PHP type

Boolean type

To specify a Boolean value, use the keyword TRUE or FALSE. Two are case insensitive.

Integral type

We can use (int) to convert a decimal coercion type to an integer.

<?php
var_dump((int)(26/3));//int(8)
?>

Array

There are three types of arrays in PHP:

索引数组:就是下标是顺序整数作为作为索引(比如第几排第几列)$class[5]
关联数组:就是下标是字符串作为索引(比如名字)$class2["zhangsan"]
多维数组 - 包含一个或多个数组的数组

The subscript should be an integer, or a string.

<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
);
// 自 PHP 5.4 起
$array = [
"foo" => "bar",
"bar" => "foo",
];
?>

Array cells can be array[key] accessed through syntax.
Note: This does not mean that you always enclose the key name in quotation marks. You do not need to enclose the key name as a constant or variable, otherwise PHP you will not be able to parse them.

Array operator Example name result $ A + $b union $a and $b union $ A = = $b equal if $a and $b have the same key/value pair then true$a = = = $b congruent if $a and $b have the same key/value pairs and the same order and type are true $a! = $b Unequal If the $a is not equal to $b true$a <> $b Unequal if $a is not equal to $b is true$a!== $b not equal if $a not all equals $b true

+The operator appends the array element on the right to the left array, and the key names in each of the two arrays are ignored on the right side of the array.

Object

To initialize an object, use the new statement to instantiate the object into a variable.

Common functions

The strlen () function is used to calculate the length of a string.
The Strpos () function is used to retrieve a string or a character within a string.

Constant

You can use the Define () function to define constants. Once a constant is defined, it can no longer be changed or undefined.
Common Magic constants:

To define a constant example:

<?php
define("poems" , "Homeric epic");

?>

PHP string Operators

In PHP, there is only one string operator.
The collocated operator is (.) used to concatenate two string values together. Such as:echo "a= ".$a."
";

On the left, concatenate the string literal "a=" with the value of the variable $ A, and the second is to connect to the line break "
"

PHP functions

Functions are executed only when they are called, and this is the same as JS, where function definitions begin with the Functions keyword.

<?php
function sum($x,$y){
$z=$x + $y;
return $z;
}
echo "-2+10= ".sum(-2,10);//outputs "-2+10=8"
?>

When there is no return statement, the admiral will become " -2+10=";

Process Control

Here, just say the following foreach statement.

foreachThe statement traverses the output array:
Grammar:

foreach (array_expression as $value){ statement};foreach (array_expression as $key => $value){ statement};

Parameter array_expression is the array that you want to iterate over, which $value is the value of the array

<?php
$actors [0] ="Marry";
$actors [1] ="Lorry";
$actors [2] = "mike";
foreach ($actors as $values){
echo "Name:$values

}
?>

The above code will output:
Name:marry
Name:lorry
Name:mike

Two important methods of magic

    1. __set( )方法:这个方法用来为私有成员属性设置值的,有两个参数,第一个参数为你  要为设置值的属性名,第二个参数是要给属性设置的值,没有返回值。    2. __get()方法:这个方法用来获取私有成员属性值的,有一个参数,参数传入你要获取的成员属性的名称,返回获取的属性值,这个方法不用我们手工的去调用

Methods in PHP are case insensitive

require(dirname(__FILE__).'/global.php'); //引入全局文件require(dirname(__FILE__).'/config.ini.php'); //引入基本配置文件

Object operators and double colon operators

Inside a member method of a class, you can access a non-static property in such a way that (object operator): $this->property (where property is the name).
A static property is accessed with a :: (double-colon): self::$property .

=

=>Array member access symbols, -> object member access symbols;
$this-: Sets the value of the >$name=$value variable of the current class name to $value ;
$thisRepresents the class itself and -> is the operator that accesses its class member
Double colon operator ( :: ) class name :: static Properties/Methods
" :: " to invoke static properties and methods in a class

include(): Contains external file, syntax format is include (string filename);
require(): The error message is output and the script is terminated
include_once(): When the same file is called multiple times, the program calls only once
require_once(): first check if the file has been called elsewhere
array_pop(): Gets and returns the last element in the array
count(): Number of elements in the statistics array
array_search(): Gets the key name of the element in the array
$array_keys(): Gets all the key names of the repeating elements in the array

Single and double quotation marks

PHP treats the data in single quotes as a normal string and no longer handles it. and double quotes also handle the strings in them.

Get and Post

$_get[] and $_post[] global arrays: Each used to receive the data passed to the current page by the GET and POST methods. "[]" Inside is name.

PHP parameters are commonly used in 3 ways: $_post[], $_get[], $_session[], respectively, to get the value of the form, URL and SESSION variables.

The differences between get and post methods in form submissions are summarized as follows:

GET是从服务器上获取数据,POST是向服务器传送数据。
GET 是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。POST是通过HTTP POST机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。
对于GET方式,服务器端用Request.QueryString获取变量的值,对于POST方式,服务器端用Request.Form获取提交的数据。
GET传送的数据量较小,不能大于2KB(这主要是因为受URL长度限制)。POST传送的数据量较大,一般被默认为不受限制。但理论上,限制取决于服务器的处理能力。
GET 安全性较低,POST安全性较高。因为GET在传输过程,数据被放在请求的URL中,而如今现有的很多服务器、代理服务器或者用户代理都会将请求URL记 录到日志文件中,然后放在某个地方,这样就可能会有一些隐私的信息被第三方看到。另外,用户也可以在浏览器上直接看到提交的数据,一些系统内部消息将会一 同显示在用户面前。POST的所有操作对用户来说都是不可见的。

When the form is submitted, if you do not specify method, the default is a GET request (. NET default is post), and the data submitted in the form will be appended to the URL, separated from the URL. The alphanumeric character is sent as is, but the space is converted to the "+" sign, and the other symbol is converted to%XX, where XX is the ASCII (or ISO Latin-1) value that the symbol is represented in 16 binary. The GET request submits the data to be placed in the HTTP request protocol header, and the data submitted by the post is placed in the Entity data, and the data submitted by the Get method can only have 2048 bytes, while Post does not have this limit. The parameters of the post pass are in Doc, as well as the text that is passed by the HTTP protocol, and then parse the parameters section when it is accepted. Get the parameters. It is generally better to use post. The post submission data is implicit, get is passed in the URL, used to pass some data that does not need to be confidential, get is passed in the URL parameters, post is not.

1.GET requested data is appended to the URL (that is, the data is placed in the HTTP protocol header), to split the URL and transfer data, the parameters are connected to &

The data submitted in 2.GET can only be 1024 bytes, in theory post is not limited, can transmit a large amount of data, IIS4 in the maximum of 80KB,IIS5 100KB

HTTP status Code

Cookie and Session Differences

The contents of the cookie mainly include: name, value, expiration time, path and domain. The path together with the domain constitutes the scope of the cookie. If you do not set an expiration time, this indicates that the
The lifetime of a cookie is a browser session, and the cookie disappears when you close the browser window. This cookie, which is the lifetime of the browser session, is referred to as a session cookie.
Session cookies are generally not stored on the hard disk but are kept in memory, although this behavior is not regulated. If the expiration time is set, the browser will place the cookie
Save to the hard disk, turn off the browser again, and these cookies remain valid until the set expiration time is exceeded.

When a program needs to create a session for a client's request, the server first checks to see if a session ID is included in the client's request.
(referred to as session ID), if it is already included, it indicates that the session was previously created for this client, and the server retrieves the session according to the session ID.
If the client request does not contain a session ID, a session is created for the client and a corresponding session is generated
The value of the associated session Id,session ID should be a string that is neither duplicated nor easy to be found to mimic the pattern, and the session ID will be in this response
Back to the client to save. This session ID can be saved by using a cookie so that the browser can automatically send the tag to the
Server.
1. The cookie data is stored on the client's browser and the session data is placed on the server.
2, the cookie is not very safe, others can analyze the cookie stored in the local and cookie deception
Consider that security should use the session.
3. Session will be saved on the server for a certain period of time. When access is increased, it will be more likely to occupy your server's performance
The cookie should be used in consideration of mitigating server performance.
4, a single cookie can not save more than 4K of data, many browsers restrict a site to save up to 20 cookies.
5, so personal advice:
Storing important information such as login information as session
Additional information can be placed in a cookie if it needs to be retained

PHP Code specification

1. Variable assignment must remain equal spacing and arrangement

2. Extra spaces are not allowed at the end of each line

3. Ensure that file naming and invocation are case-sensitive, because the Unix-like system above is case-insensitive

4. Method names are only allowed by letter, underline is not allowed, first letter to lowercase, followed by each word first letter uppercase

5. The name of the attribute is only allowed by the letter, underline is not allowed ...

6. For access to object members, we must always use the "get" and "set" methods

7. When a class member method is declared private, it must begin with a double underscore "__", which must begin with a single underscore "_" when declared as protected, and the member property declared as public is not allowed to be underlined at any time.

8. If we need to define some of the frequently used methods as global functions, we should define them in the class statically (static)

9. The function is named using lowercase and underscores and should be able to describe the function.

Both 10.Boolean values and null values are lowercase.

11. When a string is composed of plain text (that is, does not contain a variable), it must always be a single quotation mark (') as the delimiter

12. When declaring an associative array using the array type, divide it into multiple lines, guaranteeing the alignment of the key to the value of each row

13. All code in a class must be indented with four spaces

14. Var is not allowed to declare variables, and class member variables must be declared with private,protected and public. You typically use the get and set methods to access class members.

15. The method must always declare its scope with private,protected or public

16. No extra spaces are allowed between the function or method name and the parameter brackets

http://www.bkjia.com/PHPjc/908173.html www.bkjia.com true http://www.bkjia.com/PHPjc/908173.html techarticle PHP Learning basic knowledge of notes, PHP study notes PHP learning so far more than a year, notes accumulated a lot of, also quite miscellaneous, write an article to tidy up. PHP Basic part PHP output text ...

  • 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.