PHP variable, constant, array, function, php constant _ PHP Tutorial

Source: Internet
Author: User
PHP variables, constants, arrays, functions, and php constants. PHP variables, constants, arrays, functions, and php constants have recently learned some PHP and keep notes for future reference, in the past two days, JS and Swift have been well organized. P beginner PHP variables, constants, arrays, functions, php constants

Recently, I learned some PHP and continued to record the notes for later viewing. The previous JS and Swift files have been sorted out and improved. PHP is not as smooth as it was before, so it is just a good job.

PHP is a language used to interact with the server. it is object-oriented and is too boring to say anything about other features. to sum up a sentence, it is very popular and practical now. it is correct to learn it.

Variable Declaration in PHP

In PHP, there is no clear type modifier, so declared variables are not like other languages to give a type, or even just a var. in PHP, variables are represented by $. Other features are similar to those of JS. no type is available, and the type can be changed. they are legal.

$ String = "JianweiWang"; // declare a variable

$ String = 5; // modify the value of $ string to 5, valid

Echo $ string; // 5. the print result is 5.

The naming rules of variable names are similar to those of other languages. They start with a letter or underscore (_) based on the $ identifier, a variable name can contain only numbers, letters, and underscores (/\ w/in the regular expression). it cannot contain spaces and is case sensitive. ($ _ name, $ name, $ name1, all of which are valid ).

Let's talk about the null type, NULL, NULL in PHP and the variables destroyed using unset. The final types will all be null.

Constant declaration in PHP

Constants play a very important role in the robustness of a program. they do not cause unsafe factors such as variable re-assignment due to variable names being unclear due to project changes.

PHP does not have constants in other languages that have special modifiers, or uses closures in js to declare a constant. Instead, it has specialized function methods to define constants.

Define ("PI", 3.14, true );

Three parameters. The first parameter is the constant name. it must be of the string type, the second parameter is the constant value, and the third parameter is case sensitive. if it is set to true, that is, it is not sensitive. the default value is false. generally, no one is bored and not sensitive --.

Therefore, the above declaration is a constant with a value of 3.14, which cannot be modified. of course, the name cannot have a $ identifier and is set to true, therefore, this constant can be accessed by PI or pI.

Sometimes, to prevent a constant from being redefined, we need to test whether the constant exists, so we need to use another method. defined (), the parameter is a constant name string, the return value is of the bool type, not to mention...

Array

Array is a form of data set, or a normal old rule. in a brief introduction, there are two methods to define an array in PHP, one is to use the array constructor, and the other is a literal form.

Array constructor

$ Array = array (1, 2, 3, 4, 5 );

Literal form

$ Array = [1, 2, 3, 4, 5];

Everything in the PHP array is clearer (it seems that there is no dictionary, because the associated array can be used). it makes the relationship between key and value more obvious than that of JS, and the index can be modified, instead of the traditional 0, 1, 2, 3. this is the associated array.

Initialization of correlated arrays

Or two methods, constructor, literally.

Type constructor

$ Fruit = array (
'Apple' => "apple ",
'Banana '=> "banana ",
'Pineapple' => "pineapple"
);

Literal

$ Fruit = [

'Apple' => "apple ",
'Banana '=> "banana ",
'Pineapple' => "pineapple"

];

Does this look like a dictionary? the access here is the same as that in JS, and it is accessed through the key value.

Print_r ($ fruit ['apple']); // you can get an apple here.

Generally, the foreach method is used to traverse an array. here is a brief introduction.

Foreach ($ fruit as $ key => $ value ){

Echo $ key. $ value .'
';

}

This will print out

Apple

Banana Bananas

Pineapple

Function

A function encapsulates reusable code and can be called repeatedly in future use, improving the readability of the code. this is the same as other languages, I will not repeat it here. it mainly involves different parameters, such as parameter tables.

Function func (){};

In PHP, this is also the basic structure of a function, but it is different from some JS functions, such as implicit triggering. However, the function name string is assigned to the variable name, then, the function is triggered by the variable name, which is similar to the underlying implementation.

Function method (){

Echo "wang ";

}

$ Func = "method"; // give the method () method to the variable $ func

Mechod (); // call the method () method

$ Func (); // call $ func and call method (). This method is called a variable function.

PHP has a large number of built-in functions.

  

The callback variable, constant, array, function, and php constant have recently learned some PHP and continue to record the notes for future viewing. The previous JS and Swift files have been sorted and improved in the past two days. p...

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.