Common Data Types and basic syntax for getting started with php

Source: Internet
Author: User
Tags getting started with php

In php, data types include integer, decimal (floating number), Boolean, character, array, variable, and constant. Let's take a look.

I. Common PHP Data Types

1. Basic data type

Integer 1.1
1.2 decimal (floating number) contains single precision and Double Precision
1.3 boolean type (true, false)
1.4 string

2. Composite data types

2.1 array)
2.2 object)

3. Special Data Types

3.1 null
3.2. resource)

2. Basic PHP syntax

1. PHP variables must start with $, Which is case sensitive.
2. The variable name must start with a letter or underscore. Do not start with a number or a special character.

First PHP Program

The Code is as follows: Copy code
<? Php
Echo "Hello World! ";
?>

1 Note
More than 1.1 rows
/*
Xxxx
*/
1.2 rows
// Xxxxx
2. Assignment
$ A = 'test ';
2.1 check whether the variable has been declared
Isset ($)
2.2 release Variables
Unset ($ );
2.3 static variables
Static $;
Static variables can be kept in a function for several times without being released by the system. However, they can only be accessed in the declared function set and can only be initialized at the first declaration.
3. Basic Types
3.1 numeric type
3.1.1 integer (integer, keyword int)
. Integers can be expressed in octal, decimal, or hexadecimal notation.
$ A = 123; // 10 hexadecimal
$ B = 0123; // octal
$ C = 0x123; // hexadecimal
The precision of different integers varies greatly in the operating system, but 32-bit is the most common
3.1.2 floating point (float, keyword float, 64-bit floating point number, precision 14-bit)
Float and double in. PHP are equivalent
. Use floating point numbers. Remember: they are just approximate values.
For example, 2.5 is often expressed as 2.499999999 internally.
Another example:
If (0.7 + 0.1> = 0.8 ){
Echo 'a ';
} Else {
Echo 'B ';
}
The returned value is B, which depends on the exact implementation method of the floating point number. We recommend that you avoid using the floating point value for comparison.
3.2 string
. Surrounded by single or double quotation marks
For example, echo "Hello"; echo 'hello ';
Variables in double quotation marks will be interpreted, but those in single quotation marks will not
For example, var $ name = 'jano ';
Echo "my name is $ name."; // display my name is jano
Echo 'My name is $ name'; // display my name is $ name
Variables in double quotation marks can be surrounded by {} to distinguish between variables and subsequent letters.
For example, var $ n = "my name is {$ name} Yu"; // variables and characters cannot be distinguished {}
. Heredoc
$ A = <HTM
Skjdfjsd
Lksdfjsdlf
HTML; // The following representation must be at the beginning
. Get a character in the string
$ A = 'hello ';
Echo $ a {1}; // display e
Echo $ a [1]; // display e
We recommend that you use the first method to separate it from the array.
3.3 Boolean Value
True false
4. Common functions
. Line breaks in the nl2br strings are converted to <br/>
For example, echo nl2br ($ );
. Var_dump
Display the variable type and value, such as var_dump ($ );
. Print_r
Var_dump enhanced edition: prints the object type and content. All elements are printed in the array, and all Members are printed in the class object.
For example: $ a = array (1, 2, 3, 4, 5 );
Print_r ($ );
5. Array
Declare An array Using the array Method
Example:
$ A = array ('A', 'B', 'C ');
$ A = array ('A', 'B', array (1, 2, 3 ));
By default, the value is assigned from index 0.
For example, $ a [] = 'a'; // $ a [0] = 'a ';
$ A [] = 'B'; // $ a [1] = 'B ';
. Index using string values
For example:
$ A = array ('car' => 'ferrari ', 'number' => 21, 'city' => 'cq ');
Echo $ a ['car'];
. Traverse and change the array element value
Foreach ($ array as $ key =>&$ value) {// & $ value
//...
}
6. Special Types and values
. NULL is case sensitive, indicating no value. It is not assigned a value and is cleared by unset.
. Resource
7. Forced type conversion
(Int) $
(Float) $
(String) $
(Bool) $
(Array) $
(Object) $
. Special
(Int) "0123"; // returns 123. The octal 0123 is not converted to a decimal number.
(Int) "123 mu tou ren"; // 123 is returned.
(Int) "mu tou ren 123"; // return 0, because the conversion only starts to read from the first character, and non-numbers are immediately stopped.
. Convert to a Boolean Value
Non-null, non-zero, true (including positive and negative numbers), 0, false
The array containing 0 elements is false.
NULL is false
 
Convert to integer
. Floating Point to integer type
The number after the decimal point is discarded. If the number exceeds the valid digit of the certificate, the result may be 0 or a minimum negative number.
. Boolean to integer
True is 1
False is 0
. String to integer
Judge the first character on the left of the string. If the first digit is a number, the read to the number is converted to an integer from the first digit. If the first digit is not a number, the result is 0.
. PHP does not provide other methods to convert data types to integers.
 
Convert to floating point
. Integer to floating point
Result unchanged
. Boolean to floating point
True is 1
False is 0
. String to floating point
Similar to Integers
. PHP does not provide a method to convert other types to floating-point numbers.
 
Convert to string
To convert a non-string to a string, use "(string)" to force conversion before the variable.
The rules are as follows:
1. Integer or floating point type: the result is its value
2. boolean: convert true to '1', and convert false to an empty string.
3. object or array: If the converted variable is an object or array, the conversion result is a string object or string array, which needs to be analyzed based on the actual situation.
4. Resource Type: Resource ID number returned
8. Type judgment and Acquisition
 
. Convert to array
Use "(array)" to force the conversion before the variable. Converts a variable to an array with the same data type as a member variable. The array contains only one element.
For example:
$ A = 1;
Print_r (array) $ );
Result:
Array
(
[0] => 1
)
 
. Convert to object
Use "(object)" to force conversion before a variable. A new object is generated. The member variable named scalar contains the value of the original variable. For example:
$ A = 1;
$ O = (object) $;
Echo $ o-> scalar;
 
Convert data using functions
Bool settype (var, string type)
 
Type value: boolean, integer, float, string, array, object, null
 
. Judgment Type Function
Is_integer, for example, is_integer ($ a); // return true or false
Is_float
Is_double
Is_real
Is_int
Is_long
Is_numeric
Is_string
Is_bool
Is_array
Is_object
Is_null
Is_resource
Whether is_scalar is a scalar
. Type acquisition
Gettype ($ );
9. Variables and constants
. Constant
Define ('num _ usr', '0 ');
$ U = NUM_USR;
. Reference
$ A = 0;
$ B = & $;
$ B ++;
Echo $ a; // display 1. Because $ B is a reference of $ a, Changing $ B means changing $.
10. Operators
10.1 mathematical operators
+-*/% (Remainder)
10.2 comparison Operators
=
===Same value, same type
! =
<> And! = Both are not equal
! = Same value, different types
<
>
<=
> =
10.3 logical operators
And &
Or | or
Xor exclusive or. If one is true, but not both are true, the result is true.
! Non
10.4 bitwise operations
& Bitwise AND
| By bit or
^ Bitwise OR
~ Non-bitwise
<Left shift
> Right shift
10.5 ternary Operators
Indicates whether the expression before the question mark is true. If yes, the value before the colon is returned. If not, the value after the colon is returned.
For example:
$ C = $ a> $ B? ;
Echo $ a> $ B? "Hello": "no ";
. The following two statements are equivalent.
$ A = ($ B! = 'China ')? True: false;
$ A = $ B! = 'China ';
10.6 other operators
++ Auto-Increment
-- Auto-Subtraction
@ Ignore the call failure error of a specific function, for example: $ u = @ file (xxx );
. String connection operations, such as: $ a = 'Hello'. 'World'; $ a = 'Hello'. $;
11.7 special logical operator expressions
$ A = 0;
$ B = 100;
Echo $ a | $ B; // when the bool value of a is converted to true, echo $ a; otherwise, echo $ B, regardless of whether the $ B expression is true, this expression always displays 100
Echo $ a & $ B; // do not display anything, because the entire expression $ a & $ B returns false.
$ A = 1;
$ B = 0;
Echo $ a & $ B; // do not display anything, because the entire expression $ a & $ B returns false.
Echo $ a & $ B; // always show $
$ A = 1;
$ B = 0;
$ A & $ B = 12;
Echo $ B; // display 12. If $ a is true or not, $ B = 12 is executed if it is true. The system first reads and, so we started to execute the & preceding statement. If we found that the return value is true, we would execute the & following statement. If we found that the return value is false, we would not execute the & following statement, because & logic, if there is a false expression, the entire expression becomes false.

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.