Common data types and basic syntax for the introductory PHP tutorial

Source: Internet
Author: User
Tags arrays bitwise logical operators php definition php tutorial print object scalar

A PHP common data type

1. Basic data types

1.1 Integral type
1.2 Decimal type (floating number) contains single precision and double precision
1.3 Boolean types (representation True, and false)
1.4 String

2. Composite data type

2.1 Arrays (Array)
2.2 Objects (object)

3. Special data types

3.1null
3.2 Resource Type (Resource)

The basic syntax of two PHP

The 1.PHP definition variable must begin with the $ sign, case-sensitive.
2. The name of the variable should begin with a letter or an underscore, do not start with a number, or begin with a special character.

First PHP Program

The code is as follows Copy Code
<?php
echo "Hello world!";
?>

1 notes
More than 1.1 lines
/*
Xxxx
*/
1.2 Single line
Xxxxx
2. Assigning value
$a = ' test ';
2.1 Check whether the variable has been declared
Isset ($a)
2.2 Releasing variables
unset ($a);
2.3 Static variables
static $a;
A static variable can hold a value in several invocations of a function without being released by the system, but can only be accessed in the function that declares it, and can only be initialized at the first declaration.
3. Basic types
3.1 Number types
3.1.1 Integer (integer, keyword int)
. Integers are available in 8-in-10-binary 16-notation
$a = 123; 10 in-system
$b = 0123; 8 in-system
$c =0x123; 16 in-system
. Since the operating system varies greatly in integer precision, 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 are equivalent in PHP
. Use floating-point numbers to remember: they are just approximate values
such as: 2.5 in the interior is often expressed as 2.499999999
Another example:
if (0.7+0.1>=0.8) {
Echo ' a ';
}else{
Echo ' B ';
}
The return value is B, depending on how the floating-point number is implemented, and the recommended practice is to avoid using floating-point values for comparison
3.2 string
. Surround with single or double quotes
such as: echo "Hello"; Echo ' Hello ';
The variable in double quotes is interpreted, and the single quotation marks are not
such as: var $name = ' Jano ';
echo "My name is $name.";/ /Show My name is Jano
Echo ' My name is $name '; Displays my name is $name
The variable in double quotes can be surrounded by {} to differentiate between variables and subsequent letters.
such as: var $n = "My name is {$name}yu";//cannot differentiate between variables and characters without {}
. Heredoc
$a = <<< HTM
Skjdfjsd
Lksdfjsdlf
HTML; The expression on the back must be top of the front
. Get a character in a string
$a = ' Hello ';
echo $a {1}; Show E
echo $a [1]; Show E
It is recommended to use the first method to separate the array
3.3 Boolean values
True False
4. Common functions
. NL2BR the line break in the string into <br/>
such as: Echo nl2br ($a);
. var_dump
Displays variable types and values, such as: Var_dump ($a);
. print_r
Var_dump, Print object type and content, array to play all elements, class object print all Members
such as: $a = array (1,2,3,4,5);
Print_r ($a);
5. Array
Arrays are declared with the array method
Cases:
$a = Array (' A ', ' B ', ' C ');
$a = Array (' A ', ' B ', Array (1,2,3));
. Default start assignment from index 0
such as: $a []= ' a '; $a [0]= ' a ';
$a []= ' b]; $a [1]= ' b];
. index using String values
Such as:
$a = array (' car ' => ' Ferrari ', ' number ' =>21, ' city ' => ' CQ ');
echo $a [' car '];
. Iterate through and change the array element values
foreach ($array as $key =>& $value) {//& $value
//...
}
6. Special types and values
. NULL is case-sensitive, indicates no value, never assigned value, clears with unset
. resources
7. Force type Conversions
(int) $a
(float) $a
(string) $a
(BOOL) $a
(array) $a
(object) $a
. The Special
(int) "0123";//returns 123, does not convert 8 in 0123 to 10 digits
(int) "123 mu tou ren";//Return 123
(int) "Mu tou ren 123";//returns 0, because the conversion only starts at the first character and finds that the Non-numeric stop immediately
. Convert to a Boolean value
Non-null Non-zero is True (includes positive and negative numbers), 0 is False
The array containing 0 elements is false
Null is False

Convert to Integer
. floating point conversion to integral type
The number after the decimal is discarded, if the certificate is more than a valid bit, the result may be 0 or the smallest negative
. Boolean conversion to integral type
True to 1
False to 0
. string conversion to integral type
Judge the first digit on the left side of the string. If the first digit is a number, the reading to the number is converted to an integer starting at the first digit. If the first digit is not a number result is 0.
. PHP does not provide other types of methods for converting to integers

Converting to floating point numbers
. integer conversion to floating-point number
The result is the same
. Boolean conversions to floating point numbers
True to 1
False to 0
. String conversions to floating-point numbers
Similar to integers
. PHP does not provide a way for other types to be converted to floating-point numbers

Convert to String
The method of converting a non string to a string is to use a "(string)" cast before the variable.
The rules are as follows:
1. Integral or floating-point type: The result is its value
2. Boolean: True converts to ' 1 ', False to an empty string
3. Object or array: If the variable being converted is an object or an array, the result of the conversion will be a string object or an array of strings, which needs to be analyzed according to the actual situation.
4. Resource type: Return resource identification number
8. Type judgment and acquisition

. Convert Array
Cast with "(array)" before a variable. Converts a variable to the same array of member variable data types, with only one element in the array.
Such as:
$a = 1;
Print_r ((array) $a);
Results:
Array
(
[0]=> 1
)

. Convert to Object
Cast with "(object)" before a variable. A new object is generated, where the member variable named scalar will contain the value of the original variable. Such as:
$a = 1;
$o = (object) $a;
Echo $o->scalar;

Using functions for data conversion
BOOL Settype (var, string type)

Type value: Boolean,integer,float,string,array,object,null

. To judge a type function
Is_integer such as: Is_integer ($a); Returns 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 the is_scalar is a scalar
. Type get
GetType ($a);
9. Variables and Constants
. constant
Define (' num_usr ', ' 0 ');
$u = NUM_USR;
. Reference
$a = 0;
$b = & $a;
$b + +;
echo $a//display 1 because $b is a reference to $a, $b change means $a change
10. Operator
10.1 Mathematical operators
+-*/% (remainder)
10.2 Comparison Operators
==
= = = value Same, same type
!=
<> and!= are not equal
!== values are the same, types are different
<
>
<=
>=
10.3 Logical operators
and &&
or | | Or
XOR exclusive OR, if one is true, but not two are true, the result is true
! Non -
10.4 Bitwise operation
& Bitwise AND
| by bit or
^ per-bitwise XOR OR
~ Bitwise NON
<< left Shift
>> Right Shift
10.5 Ternary operator
Indicates whether the expression before the question mark is true, returns the value before the colon, or returns the value after the colon
Such as:
$c = $a > $b? 1:2;
echo $a > $b? "Hello": "No";
. The following two statements are equivalent
$a = ($b!= ' ")? True:false;
$a = $b!= ' the "
10.6 Other operators
+ + self-increase
--Self-reduction
@ Ignore call failure for specific function error, such as: $u = @file (XXX);
. string concatenation operations, such as: $a = ' hello '. ' World '; $a = ' hello '. $a;
11.7 Special logical operator expressions
$a = 0;
$b = 100;
echo $a | | $b//$a the echo $a if the value of bool is converted to true, or the echo $b, regardless of whether the $b expression is true, this expression always displays 100
echo $a && $b//will not display anything because the entire expression $a && $b returns false
$a = 1;
$b = 0;
echo $a && $b//will not display anything because the entire expression $a && $b returns false
echo $a && $b;//Always show $a
$a = 1;
$b = 0;
$a && $b = 12;
The Echo $b//Displays A, $a is true, the $b=12 is executed, the system first reads &&amp, knows this is a, and then begins execution of the statement && previous, and then executes && when it returns true. After the statement, found that return false will no longer execute the statement after &&, because the && logic, as long as there is a false the entire expression will become false

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.