PHP Notes 1-Code Marks, Comments, Variables, Constants, Data Types

php code tag

1. Asp tag <% echo'hello world2'; %> needs to enable asp_tags = Off in the configuration file http.conf
2. Short tag <? echo'hello world2'; ?> needs to enable short_open_tag in the configuration file http.conf = Off
3. Script mark
4. Standard mark <?php echo'hello world' ?> is the most used

Comment and separator

单行注释 
//注释
# 注释
多行注释
/*
多行注释
多行注释

The php delimiter
code uses the line unit English semicolon ";" to mark the end of a line, and the last line does not need to add a semicolon

variable

1. Variable definition
All variables in PHP must use the "$" symbol.
Naming rules: composed of letters, numbers, and underscores but cannot start with numbers. Chinese variables are allowed but not recommended.

$var1 =1;
 $var2 =$var1;
 echo 'var1='.$var1.';var2='.$var2;

Predefined variables:
$_GET: Get all the data submitted by the form in the get method
$_POST: The data submitted by POST will be saved here
$_REQUEST: Both GET and POST submissions will be saved
$GLOBALS: All global variables in PHP
$_SERVER: Server Information
$_SESSION: session session data
$_COOKIE: cookie session data
$_ENV: environment information
FILES: file information uploaded by the user 2. Variable variables If the value saved by a variable happens to be the name of another variable, you can directly access one Variable gets the value of another variable: Add one more _FILES in front of the variable: file information uploaded by the user 2. Variable variable If the value of a variable is just the name of another variable, you can directly access one variable to get the other The value of a variable: add one more before the variableFThe I L E S : a user on the transmission of packets member channel information 2 , may be variable variable amount as if a th variable amount of holding stored in the value just good is another outside a th variable amount of the name of the word , then it may be in direct contact through had visited asked Yi Ge variable amount was to another outsideA th variable amount of value : in variable amounts prior to surface again plurality add a th symbol.
//Variable variable

 $a = 'b';
 $b = 'bb';
 echo $$a  //输出bb

3. Variable value
transfer There are two methods for variable value transfer: value transfer and reference transfer.
Value transfer: assign a value to the saved value of the variable, and then transfer the new value to another variable.
Transfer by reference: save the value of the variable. The memory address of, passed to another variable: the two variables point to the same memory space

$a =10;
 $b = $a;
 $b = 5;
 echo $a,$b; //10  5
 
 $c =10;
 $d = &$c;
 $c =5;
 echo $c,$d  //5 5

constant

1. Definition and use
There are two ways to define constants in PHP (there are only two after 5.3)
1) Use the function that defines the constant: define('constant name', constant value);
2) Only after 5.3: const Constant name = value;

define('PI',3.14);
 const PI2=3.141;
echo PI,PI2; //3.14  3.141
define ('-_-','123'); //define可以定义特殊常量
//const -_-1 = '123' 会报错
echo constant('-_-');
//echo -_- //会报错

2. Several commonly used system constants
PHP_VERSION: PHP version number
PHP_INT_SIZE: integer size. (1 byte 8 bits. 32 bits 4 bytes, 64 bits 8 bytes)
PHP_INT_MAX: the maximum value that integers can represent (integers in PHP allow negative numbers: signed)
3.
There are magic constants in PHP Some special constants start with double underscore + constant name + end with double underscore. This kind of constant is called system magic constant: the value of magic constant usually changes with the environment, but the user cannot change it.

__DIR__:当前被执行的脚本所在电脑的绝对路径
__FILE__:当前被执行的脚本所在的电脑的绝对路径(带自己文件的名字)
__LINE__:当前所属的行数

__NAMESPACE__:当前所属的命名空间
__CLASS__:当前所属的类
__METHOD__:当前所属的方法
echo __NAMESPACE__,__DIR__,__FILE__,__LINE__ //D:\greenPro\apache\host1D:\greenPro\apache\host1\varandremark.php104

type of data

The eight data types of php
Simple (basic) data types: 4 sub-categories
Integer type: int/integer, the system allocates 4 bytes of storage, representing the integer type (with preconditions)
Floating point type: float/double, the system allocates 8 Byte storage, representing integers that cannot be stored in decimals or integers (such as integers that cannot be stored in 32 bits).
String type: string, the system allocates according to the actual length, representing a string (quotation mark).
Boolean type: bool/boolean, representing Boolean type, only two values: true and false

Compound data type: 2 sub-categories
Object type: object, storage object (object-oriented)
Array type: array, storage multiple data (one-time)

Special data types: 2 sub-categories
Resource type: resource, storing resource data (PHP external data, such as databases, files)
Null type: NULL, only one value is NULL (cannot be calculated)

Type conversion
Automatic conversion: The system determines and converts itself according to the needs (more used, the system determines the required type by itself, and the efficiency is low)
Coercive conversion: artificial conversion of the target type according to the needs.
Coercive conversion rule: add a bracket before the variable (), and then write the corresponding type in it: int/integer.... The NULL type uses unset(),
Boolean true is 1, and false is 0.
Strings have their own rules
. Strings beginning with a letter are always 0.
The string starting with a number is taken until it hits the string (will not contain two decimal points at the same time)

$a = 'abc1.1.1';
$b = '1.1.1bbb';
echo (int) $a; //0
echo  (int) $b;//1
echo (float) $a; //0
echo  (float) $b;//1.1

Type Judgment
Use a set of type judgment functions to judge the variable, and finally return the data type of the data stored in the variable (the same result is true, and the failure is false):
is_XXX (variable name)
Bool type cannot be viewed by echo (cannot distinguish whether it is String true or bool true), you can use var_dump structure to view

$a = 'abc1.1.1';
$b = '1.1.1bbb';
var_dump(is_int($a));//bool(false)
var_dump(is_string($a));//bool(true)

Get type
Gettype (variable name): Get the type, get the string corresponding to the type

$a = 'abc1.1.1';
$b = '1.1.1bbb';
echo  (Gettype($a))//string

Set type
(variable name, type) (return bool value to indicate whether the conversion is successful or not): set data type: different from forced conversion
1) forced conversion (type) variable name is to process the content of the data value copied (not Will handle the actual stored content)
2) settype will directly change the data itself

$b = '1.1.1bbb';
echo  (Gettype($b));//string
var_dump(settype($b,'int'));//bool(true)
echo  (Gettype($b));//integer
echo  $b;//1

Integer type
Save integer value (range limit), 4 bytes store data, the maximum is 32 bits: more than 4.2 billion. However, the default in PHP is a signed type (to distinguish between positive and negative numbers).
PHP provides four types of integer definitions: decimal definition, binary definition, octal definition and hexadecimal definition

$a = 120;		//10进制
$b=0b110;	//2进制  0b开头 b:binary
$c=0120;		//8进制 0开头
$d=0x120;	//16进制 0x开头
echo $b." ".$d // 6  288

Decbin():十进制转二进制
Decoct():十进制转八进制
Dechex():十进制转十六进制
Bindec():二进制转十进制
var_dump(decbin(200))//string(8) "11001000"

Floating point type The
decimal type and the integer that exceeds the storage range of the integer (precision is not guaranteed)
There are two ways to define the floating point type

$f = 1.23;
$f = 1.23e10;	//科学计数法,其中e表示底10。1.23*10^10

Try not to use floating-point numbers to make accurate judgments: the data stored in floating-point numbers is not accurate enough, and most of the decimals in the computer are basically inaccurate

Boolean type The
two values ​​true and false are usually used to judge and compare.
When making certain data judgments, you need to pay special attention to the type conversion
Empty(): judge whether the value of the data is "empty", not NULL, and return if it is empty true, return false
if it is not empty Isset(): Determine whether the variable stored in the data itself exists, return true if there is a variable, and return false if it does not exist

Guess you like

Origin blog.csdn.net/zhangxm_qz/article/details/108509522