Basic php learning notes

Source: Internet
Author: User
Tags php basics
This article mainly introduces the basic knowledge of php learning notes. For more information, see

This article mainly introduces the basic knowledge of php learning notes. For more information, see

I have been studying php for more than a year, and I have accumulated a lot of notes. I 'd like to write an article.

Php Basics

Basic commands for PHP output text: echo and print.

Differences between echo and print

Echo is a PHP statement, print and print_r are functions, and the statement does not return values. The function can return values (even if it is not used)

Echo outputs one or more strings.
Print can only print values of simple type variables (such as int and string)
Print_r can print values of complex types of variables (such as arrays and objects)

Differences between var_dump and print_r

Var_dump returns the expression type and value, while print_r returns only the result, which is easier to read than using var_dump in debugging code.

Variable

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

PHP has three different variable scopes:

Local)
Global)
Static)

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

Variables declared in the function have LOCAL scopes and can only be accessed within the function.

Global keywords are used to access global variables in functions.

PHP static keywords

Generally, when the function is completed/executed, all variables are deleted. However, sometimes I do not need to delete a local variable. Further work is required to achieve this.

To accomplish this, use the static keyword when declaring the variable for the first time:

<? Php
Function myTest (){
Static $ x =-1;
Echo $ x;
$ X --;
}
MyTest (); //-1
Echo"
";
MyTest (); //-2
Echo"
";
MyTest (); //-3
?>

Php type

Php type: ** PHP supports eight primitive types. **

Boolean

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

Integer

We can use (int) to forcibly convert decimals to integers.

<? Php
Var_dump (int) (26/3); // int (8)
?>

Array

Php has three Arrays:

Index Array: The subscript is an ordered Integer as an index (for example, the number of rows in the row) $ class [5]
Associated array: The subscript is a string as an index (for example, name) $ class2 ["zhangsan"]
Multi-dimensional array-an array containing one or more Arrays

The subscript must be an integer or a string.

<? Php
$ Array = array (
"Foo" => "bar ",
"Bar" => "foo ",
);
// From PHP 5.4
$ Array = [
"Foo" => "bar ",
"Bar" => "foo ",
];
?>

Array units can be accessed using the array [key] syntax.
Note: This does not mean that the key name is always enclosed in quotation marks. You do not need to quote the key name constants or variables. Otherwise, PHP cannot 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 TRUE $ a ===$ B. If $ a and $ B have the same key/value pairs and the order and type are the same, TRUE $! = $ B. If $ a is not equal to $ B, the value is TRUE $ a <> $ B. If $ a is not equal to $ B, the value is TRUE $! = $ B incomplete. If $ a is not complete, $ B is TRUE.

+ The operator attaches the array element on the right to the back of the array on the left. If both arrays have the key names, only the elements in the array on the left are ignored.

Object

To initialize an object, use the new statement to instance the object to 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 cannot be changed or undefined.
Common magic constants:

Example of defining constants:

<? Php
Define ("poems", "Homeric epic ");
Echo poems; // outputs "Homeric epic"
?>

Php string Operator

In PHP, there is only one string operator.
The concatenation operator (.) is used to connect two string values. For example, echo "a =". $ ."
";
Connect the string text "a =" to the value of the variable $ a on the left, and the second is the line break"
"Connection

Php Functions

The function is executed only when it is called. This is the same as JavaScript. Similarly, the function definition starts with the function keyword.

<? Php
Function sum ($ x, $ y ){
$ Z = $ x + $ y;
Return $ z;
}
Echo "-2 + 10 =". sum (-2, 10); // outputs "-2 + 10 = 8"
?>

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

Process Control

Here, we only talk about the foreach statement.

The foreach statement traverses the output array:
Syntax:

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

The array_expression parameter specifies the array to be traversed, and $ value indicates 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 be output:
Name: Marry
Name: Lorry
Name: mike

Two important magic methods

1. _ set () method: This method is used to set values for private member attributes. There are two parameters. The first parameter is the attribute name for the set value, the second parameter is the value to be set for the attribute, with no return value. 2. _ get (): This method is used to obtain the attribute value of a private member. There is a parameter that is used to input the name of the member attribute you want to obtain and return the obtained attribute value, this method does not need to be manually called.

Methods in php are case insensitive.

Require (dirname (_ FILE __). '/global. php '); // introduce the Global FILE require (dirname (_ FILE __). '/config. ini. php '); // introduce the basic configuration file

Object operator and double colon Operator

In the member methods of the class, you can use the-> (Object operator): $ this-> property (where property is the property name) method to access non-static properties.
Static attributes are accessed using: (double colon): self: $ property.

=> And->

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.