PHP Learning Day4---constants and data types

Source: Internet
Author: User
Tags constant definition

Today I learned PHP knowledge about constants and data types

Here's what you'll learn today:

One, constant 1. Constant definition

A constant is an "identifier" in which the data stored is not and should not be changed . In general, there are two ways to define a constant:

// The first of these methods // define ("Constant name", "Constant Value"); Define ("HH","123"); // The second method of // Const  Constant name = constant value;const  123;
Methods for the value of constants: two methods
// use the name directly, or take a value from the constant () function // directly using its name     echo HH;         // HH = 123 // using the constant ("constant name") function, note: The solid name is a string     Echo constant ("HH");   // hh=123
Constants have the following characteristics:

1. Constants do not need to be used for $.

2. The value of a constant cannot be changed or destroyed.

3 A Hyper-global scope.

4. Constants can only store scalars.

In a project that is developed by multiple people, how do you tell if a constant exists or is defined by someone else?

Here, we give the function, this function is specifically to determine whether the constant exists:

      Defined ("Constant")

Returns true if it exists, false if it does not exist

In PHP, if you use an undefined constant, you will get an error, but the constant will still have a value.

Reserved for constants

Predefined constants are pre-defined constants in the system, we can take them directly, with a constant, there are hundreds of, we need to know a few can be:

M_PI: pi;

Php_os: The operating system where PHP is running.

Php_version: This is the version number of PHP.

The largest integer of the php_int_max:php species.

Magic Constants

_file_: Represents the full physical path of the current Web page file

_dir_: Represents the folder where the current Web page file resides

_line_: Represents the line number where the current constant name is located

Ii. Types of data

There are 8 types of data in PHP altogether.

4 Types of basic type (scalar type):

Integral type: int integer

Floating point: Float,double,real

Strings: String

Boolean type: Bool,boolean

Two kinds of composite types:

Arrays: Array ()

Objects: Object ()

2 Types of special type

NULL type: null

Resource type: Resource

Method of data Type 1. Integer: int integer 1). There are four types of notation for integer types
123;         // decimal 0123;       // octal 0x123;     // 16 binary $n 4 = 0b1010;   // 2 binary
2). 10 binary conversion to other binary methods

Decbin (): The result returns a string of 2 binary digits of the number

decoct (): The result returns a string of 8 binary digits of the number

Dechex (): The result is a number that returns a string in the form of a binary number

3). Other binary conversions to 10 binary 

Bindec ( string ): The result returns the number of the 2 binary numeric string corresponding to the ten -binary

Octdec ( string ): The result returns the number of the 8 binary numeric string corresponding to the ten -Binary

Hexdec ( string ): The result returns the number of the string corresponding to the Binary numbers

2. Floating point numberfloating-point numbers are written in two ways:

General notation: with decimal points. (123.456)

Scientific counting method: with special symbol E. (123456E3)

    Note : Floating-point numbers should not be compared in size because all the final forms of the digits are binary, and the binary of most floating-point numbers is not allowed to be expressed. When an integer's result exceeds the maximum range of integers, it becomes a floating-point number.

3. Stringthe four-medium form of a string:
= = = <<<= <<< ' identifier b ' string content ... identifier B;

Four differences:

   

3. Boolean type (Bool,boolean)

it has only 2 data:true,false;

A common application scenario for Boolean types is to judge a variable directly.

       0, 0.0, "", "0", NULL, Array (), false, and another is "Undefined variable "

Iv. Type Conversion Free conversion

in any operation, if a type of data is required, and the data given is not of that type, an automatic conversion usually occurs: converting the type to the type required by the target.

Forced conversions

An automatic type conversion is determined by a statement of "operator" or similar operator. Forcing type conversions is just a simple syntax:

(int), (float), (string), (bool), (array), (object)

The coercion type conversion above does not alter the variable's own data or type. Corresponding, there is a syntax that directly alters the data (and type):

      

Settype ($ variable name , "target type");

v. Common functions related to data types

Var_dump (): Print information about a variable

GetType ($ variable): Gets the data type, the return value is a string of the type name

SetType ($ variable name, "target Type"): Sets the data type of the variable, directly changes the data (and type)

Is_* (): Determine if a variable is of a certain type

Isset (): Determines whether the variable is null or exists

Empty (): Determines whether a variable is empty "", "0", 0,false,arry (), NULL is considered null, returns a Boolean value

Today's notes are written here and will continue tomorrow.

PHP Learning Day4---constants and data types

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.