PHP Basic Primer Learning Note _php Tutorial

Source: Internet
Author: User
Tags modulus scalar type casting
This article introduces in detail the most basic content in PHP learning, including basic data types, variable types, operators and other things to get access to.
-
Basic data types supported by PHP:
Integer: Integers
Float (double, doubles): floating-point number, representing all real numbers
String: Strings
Boolean: Boolean value, True/false
Array: Saving multiple data items with the same type
Object: An instance of the Save class
Null: A variable that has not been assigned, has been reset, or is assigned a special value of NULL
A specific built-in function (such as a database function) returns a variable of type resource

Four types of scalar:

String (String)
Integer (integer type)
Float (floating point type, also double)
Boolean (Boolean)
Two kinds of composite types:

Array (arrays)
Object (Objects)
Two special types:

Resource (resources)
Null (empty)
View variable types
The GetType () function makes it easy to see the type of a variable:

The code is as follows
$var _bool = TRUE; A Boolean
$var _str = "Foo"; A string
$var _int = 12; An integer

Echo GetType ($var _bool); Output Boolean
Echo GetType ($var _str); Output string
Echo GetType ($var _int); Output integer
?>

Tips
For historical reasons, if the data is float type, the GetType () function returns a double instead of float.
If you want to see the value and type of an expression, use the Var_dump () function.
Judging variable types
If you want to determine the next logical action by judging the variable type, do not use GetType () and use the Is_type series function:

The code is as follows
$var _int = 12;

If $var _int is of type int, this is an addition
if (Is_int ($var _int)) {
$var _int = $var _int+4;
}
echo $var _int; Output 16
?>


Both null and resource represent external resources, such as database connections
Basically, you cannot manipulate a resource variable directly, but usually they are returned by the function and must be passed as arguments to other functions.

In PHP, the type of the variable is determined by the value assigned to the variable. (Dynamic type language)
PHP can determine the type of a variable at any time based on the value stored in the variable, which is the ability to transparently change the variable type at any time.
PHP will "automatically" get the input data type, and once the variable value is retrieved from the variable, it will return data with the same data type.
Habit: Constant names are all made up of uppercase letters. This makes it easy to differentiate between variables and constants.
A significant difference between a constant and a variable is when a constant is referenced, and there is no $ symbol in front of it.
Another difference between constants and variables is that constants can hold only Boolean values, integers, floating-point numbers, or string data, which are scalar data.

Variable type change (juggling)
PHP does not need (or is not supported) to explicitly define his variable type in a reputation variable; the type of a variable is determined by the relationship that the variable is used in, that is, if you assign a value of a string to a variable Var, var becomes a string variable. If you assign an integer value to Var, he becomes an integer variable. An example of a PHP automatic conversion variable type is the operator ' + ' of the addition. If any one operand is a double-precision number, then all operands are evaluated as double-precision numbers, and the result is a double-precision number. Otherwise, the operand will be considered an integer and the result will be an integer. Note that this does not affect the variable type of each operand itself, and the only change is how the operand is handled during the calculation. $foo = "0"; $foo is a string with a value of "0" (ASCII 48)

PHP code

The code is as follows
$foo = "http://www.hzhuti.com/HTC/G11/"; $foo is a string with a value of "0" (ASCII 48)
$foo + +; $foo is a string with a value of "1" (ASCII 49)
$foo + = 1; $foo is now an integer (2).
$foo = $foo + 1.3; $foo is now a double-precision number (3.3).
$foo = 5 + "Ten Little piggies"; $foo is an integer (15)
$foo = 5 + "Ten Small Pigs"; $foo is an integer (15)


If you think the last two expressions in the example above look a bit strange, look at the "Conversion of Strings" section. If you want to force a variable to be evaluated as a fixed type, see the section "type coercion (casting)". If you want to change the type of a variable, see the description of the function "Settype ()".
Determine the type of a variable
Because PHP itself determines the types of variables and generally translates them as needed, the type of a particular variable is not obvious at any time. PHP includes some functions to find out the type of the variable. These functions are GetType (), Is_long (), is_double (), is_string (), Is_array (), and Is_object ().
Type coercion (type casting)
Type coercion in PHP is similar in C: Write the kind of type you want in parentheses in front of a strong variable.

PHP code

The code is as follows
$foo = 10; $foo is an integer
$bar = (double) $foo; $bar is a double-precision number


The following enforcement methods are allowed: (int), (integer) – Coerced to Integer (real), (double), (float) – Force double (string) – Force to String (array) – Force arrays (object) – force Objects Note that tabs (tabs) and spaces (spaces) are allowed in parentheses, so the following statement is equivalent: $foo = (int) $bar; $foo = (int) $bar;
String conversions
When a string is evaluated as a numeric value, his result and type are determined as described below. If the string contains the character '. ', ' e ', or ' e ', it is treated as a double-type variable, otherwise it is considered an integer. The value of this string is determined by the first part of the word. If the string starts with any valid numeric data, then the numeric data is the value of the string participating in the operation. Otherwise, the value is 0 (zero). Valid numeric data follows these tags, followed by one or more digits (which can contain a decimal point) followed by an optional exponent. An exponent is made up of one or more numbers following the ' e ' or ' e '.

PHP code

The code is as follows
$foo = 1 + "10.5"; $foo is the double precision number (11.5)
$foo = 1 + " -1.3e3"; $foo is the double precision number (-1299)
$foo = 1 + "bob-1.3e3"; $foo is an integer (1)
$foo = 1 + "BOB3"; $foo is an integer (1)
$foo = 1 + "Ten Small Pigs"; $foo is an integer (11)
$foo = 1 + "Ten Little piggies"; $foo is an integer (11);
This string includes the character ' E '


Identifier


Operator:
1) assignment Operator: =

2) arithmetic operator: +,-,*,/,% (modulo)

3) connection operator:. , regardless of the operand, as a string, the result returns a string

4) Total assignment operator (Combined assignment Operators): +=,*=,/=,-=,%=,.=

5) Auto increment and decrement operator (automatically incrementing and decrementing):

(1) $variable +=1⇔ $variable + +; $variable-=1⇔ $variable-as in C, do other things first, after + + or-

(2) + + $variable,-$variable, first + + or-, then do other operations

6) comparison operator:

= = (left equals right),! = (left not equal to right),
= = = (left equals right, and data type is the same),
>=,>,<,<=

7) logical Operator: | | or,&& And,xor (when the left and right sides have and only one is true, return true)!

A reference table

Operator (arithmetic Operators) Operator
Symbol Description
Description Example
Case Result
Results
+ Addition
Plus x=2
X+2 4
-Subtraction
Minus x=2
5-x 3
* Multiplication
Multiplication Sign X=4
X*5 20
/Division
Division Sign 15/5
5/2 3
2.5
% modulus (division remainder)
Modulus (remainder) 5%2
10%8
10%2 1
2
0
+ + Increment
Self-added x=5
X + + x=6
--Decrement
Self-reducing x=5
x--x=4


Assignment Operators
Quantifier (Assignment Operators)

Operator
Symbol Example
Case is the same as
Equivalent to
= X=y X=y
+ = X+=y X=x+y
-= X-=y x=x-y
*= x*=y X=x*y
/= x/=y x=x/y
%= x%=y x=x%y


Comparison Operators
Comparator (Comparison Operators)

Operator
Symbol Description
Specific Description Example
Case
= = Equal To
equals 5==8 return False
! = is not equal
Not equal to 5!=8 returns True
> is greater than
Greater than 5>8 returns false
< is less than
Less than 5<8 returns True
>= is greater than or equal to
Greater than or equal to 5>=8 return false
<= is less than or equal to
Less than or equal to 5<=8 returns True


Logical Operators
Logical judgment (Logical Operators)

Operator
Symbol Description
Description Example
Case
&& and
and x=6
Y=3
(x < ten && y > 1) returns True

|| Or
or x=6
Y=3
(x==5 | | y==5) returns false

! Not
Non-x=6
Y=3
! (x==y) returns True


Other operators:

Ternary operator, error suppression operator, execution operator, array operator, type operator

Operator "?" The previous expression

If an expression containing a two-tuple operator appears in the ternary operator "? : "The"? " Before, you should add a pair of parentheses to the expression. For example:
(x >= 0)? ×:-X;

Now let's look at a simple one? Example of an expression of number

The code is as follows

$a = ' http://www.zhutiai.com/zhuti/c7/';

echo $a? ' True ': ' false ';

The output result is Trur;

Look again

if ($a)

{

Echo ' true ';

}

Else

{

echo ' false ';

}

excerpt from PHP development

http://www.bkjia.com/PHPjc/478318.html www.bkjia.com true http://www.bkjia.com/PHPjc/478318.html techarticle This article introduces in detail the most basic content in PHP learning, including basic data types, variable types, operators and other things to get access to. -Basic data supported by PHP ...

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