Beginner PHP variables, constants, arrays, Functions, PHP constants _php Tutorials

Source: Internet
Author: User

Beginner PHP variables, constants, arrays, functions, PHP constants


Recently this paragraph myself learned some PHP, continue to record notes, easy to view later, JS and Swift These two days to find time to finish. PHP is not as ink-dry as it was before.

PHP is a door and the server to interact with the language, object-oriented, other features lazy to say, summed up a word, now very fire, but also very practical, learned is also true.

Variable declarations in PHP

There is no explicit type modifier in PHP, so the declared variable is not like other languages to give a type, or even just a Var, in PHP, the variable with $ as the identity of the representative, the rest of the features and JS very much like, no type, the type can be changed, are legitimate.

$string = "Jianweiwang"; Declaring a variable

$string = 5; Modify the value of $string to 5, legal

Echo $string; 5, printing result is 5

The naming conventions of variable names, like other languages, are also similar, based on the beginning of the identity, the letters underscore the beginning of the letter, the composition can only have a numeric letter underscore (/\w/in the regular), the variable name is not allowed to contain spaces, case-sensitive. ($_name, $name, $name 1, these are all legal).

Again, empty types in PHP, NULL, NULL, variables destroyed with unset, and the final type will be uniformly null.

Constant declarations in PHP

Constants play a significant role in the robustness of a program, and do not cause unsafe factors such as variable re-assignment, which occurs because of a variable name that is caused by a project's changing hands.

PHP is not like other languages, constants have special modifiers, or in JS with a closure declaration of a constant, but there is a special function method to define the constant.

Define ("PI", 3.14, true);

Three parameters, the first parameter is a constant name, requires a string type, the second is a constant value (value), the third argument is case-sensitive, if set to true, that is not sensitive, the default is false, generally no one so boring and insensitive it--.

So the above declaration is a constant value of 3.14, it is not allowed to be modified, of course, the name can not have the $ identity, and set the true, so this constant may be accessed by pi, or it can be pi.

Sometimes, in order to prevent constants from being redefined, we need to test whether this constant exists, so use another method. Defined (), the argument is a constant name string, the return value is bool type, not much said ...

Array

Array is a form of data collection, or normal, simple introduction, to be added later in the description of the method, the definition of an array in PHP generally there are two ways, one is to use the array constructor, one is the form of literal.

Array constructor

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

Literal form

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

In the array of PHP everything is more clear (seemingly no dictionary, because you can use associative arrays), it will be the relationship between key and value is more obvious than JS, the index can be modified, rather than only the traditional 0, 1, 2, 3. This is also an associative array.

Initialization of associative arrays

Or two ways, the constructor, the literal.

Type constructor

$fruit = Array (
' Apple ' and ' apples ',
' Banana ' = ' banana ',
' Pineapple ' = ' pineapple '
);

Literal quantity

$fruit = [

' Apple ' and ' apples ',
' Banana ' = ' banana ',
' Pineapple ' = ' pineapple '

];

Does this look like a dictionary? Here's the same access as JS, accessed through the key value.

Print_r ($fruit [' Apple ']); Here you can get the apples.

Iterating through an array typically uses the Foreach method, which is described here in a nutshell, where you write more.

foreach ($fruit as $key = = $value) {

Echo $key. $value. '
';

}

This will print out

Apple Apple

Banana Banana

Pineapple Pineapple

Function

function, is to be able to reuse a piece of code, in the later use can be repeated calls, improve the readability of the code, this and other languages are the same, do not repeat, the main point of different, such as the parameter table.

function func () {};

In PHP, this is also the basic structure of a function, but it is different from some of the use of JS, such as the implicit trigger and so on, but through the function name string assignment to the variable name, and then through the variable name trigger function, these and the underlying implementation is very similar.

Function method () {

echo "Wang";

}

$func = "Method"; Assign method () to Variable $func

Mechod (); Call Method () methods

$func (); Calling $func is also called Method (), which is called a mutable function.

PHP has a large number of built-in functions, it is not detailed.

  

http://www.bkjia.com/PHPjc/1108740.html www.bkjia.com true http://www.bkjia.com/PHPjc/1108740.html techarticle Beginner PHP variables, constants, arrays, functions, PHP constants recently this paragraph has learned some PHP, continue to record notes, convenient for later viewing, before the JS and swift two days to find time to finish. P ...

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