I. GENERAL statement
Although PHP is a weakly typed language, it is sometimes necessary to use a type conversion. Here's a look at type conversions and defining variables and assigning values to variables.
1.1 Type Conversions
The type conversion in PHP is as simple as the C language, with only the type name enclosed in parentheses before the variable.
<?php//type Conversion Description: When using the operator to convert a variable, the original value will not be changed, and when using the Settype function conversion, the original value is changed $num = ' 3.1415926r '; Echo ' uses the (integer) operator to convert the variable $ Num type: '. (integer) $num, echo ' <p> ', echo ' output variable $num value: '. $num; Echo ' <p> '; Echo ' uses the Settype function to convert the variable $num type result: '. Settype ($num, ' Integer '), echo ' <p> ', echo ' output variable $num value: '. $num, Echo ' <p> ';? >
The Settype () function in which the specified variable can be converted to the specified data type.
There are some functions in PHP that can be used to detect whether a variable is a specified type, such as Is_bool () is to detect whether it is a Boolean type, is_string () to detect whether it is a string type, and so on.
1.2 Defining constants
Constants can be understood as values unchanged, constant values are defined, can not be changed anywhere else in the script, the syntax is: define (constant_name,value,case_sensitive), three parameters are constant name (required), the value of the constant (required), is case sensitive (optional). There are two ways to get a constant: the first is to use the variable name directly, and the second is to get it through the constant () function. To determine if a constant is already defined, you can use the defined (Stringname) function to return true successfully, otherwise false.
<?php//defines a constant: define (), gets the value of the constant: constant (), determines whether the constant is defined: defined () define (' Message ', ' value of constant '), echo Message. ' <br> ';d efine (' count1 ', ' constant value 2 '); echo count1; $name = ' count1 '; echo constant ($name). ' <br> '; Actually the output of the count of this constant is echo defined (' Message '). ' <br> ';? >
<?php//pre-defined constant echo ' current file path: '. __file__. ' <br> ', echo ' Current number of rows: '. __line__. ' <br> '; 82echo ' Current PHP version information: '. Php_version. ' <br> '; Echo ' current operating system: '. Php_os;echo ' <p> ';? >
1.3 Defining variables and assigning values to variables
Unlike many languages, it is not necessary to declare a variable before using it in PHP (PHP4.0 needs to declare a variable before), just assign a value to the variable.
<?php//variable Assignment//First: Variable direct assignment, for example, $e= ' SS ';//second: the assignment between variables, the assignment between variables refers to the assignment of the two variables using their own memory, gu no interference;//Third: Reference assignment, the concept of reference is, When you change the value of one of the variables, the other changes as well, using the & symbol to represent the reference. The assignment between variables $string1 = ' SPCN '; $string 2 = $string 1; $string 1 = ' Zhuangjia '; the value of the Echo ' variable string2 is: '. $string 2. ' The value of the <br> '; Echo ' variable string1 is: '. $string 1. ' <br> '; Echo ' <p> ';//reference Assignment $i = ' SPCN '; $j = & $i; $i = "Hello, $i"; The value of Echo ' J is: '. $j. ' <br> '; the value of Echo ' I is: '. $i. ' <br> ';? >
<?php//Global variables can be accessed from anywhere in the program, but are not available in user-defined functions. Use the Global keyword statement if you want to use it. $zy = ' will not see '; $zyy = ' will see '; function Lxt () {// echo $zy. ' <br> '; Global $zyy; echo $zyy. ' <br> ';} LXT ();? ><?php//variable variable $change _name = ' trans '; $trans = ' You were met '; echo $change _name. ' <br> '; echo $ $change _name; The implementation principle is similar to the escape character, $change _name represents trans, and then adds a $ symbol, that is, the output is $trans echo ' <p> '; >
1.4 operator
Operators, like other languages, include +-*/() | ^ ~ << et cetera, but here I need to explain that PHP inside even using the division sign divisor of 0, can also circumvent the error does not affect the operation. And then there's the tri-op operator.
<?php//@ Operator: Mask error message $err = @ (5/0); Echo $err. ' <br> ';? ><?php//ternary operator $value = $res = ($value = = 100)? ' Ternary operation ': ' No value changed '; Echo ' haha '. $res. ' <br> '; Echo ' <p> ';? >
1.5 Functions
function is to write some of the reused functionality in a separate code fast, call it separately when needed, create the function syntax: Functions fun_name ($str 1, $str 2 ... $strn) {}, and then call Fun_name (XXX).
<?php//simple function CountNumber ($num 1, $num 2) { return "$num 1 * $num 2 =". $num 1 * $num 2. ' <br> ';} Echo CountNumber (10,10); Echo ' <p> ';///function pass parameter//By value Method function Example ($m) { $m = $m * 5 +; Echo ' $m within the function is: '. $m. ' <br> ';} $MM = 1;example ($MM); The value of $m outside the Echo ' function is: '. $mm. ' <br> '; Echo ' <p> ';//by reference mode function example1 (& $m) { $m = $m * 5 + ten; Echo ' $mmm within the function is: '. $m. ' <br> ';} $mmm = 1;example1 ($mmm); The value of $mmm outside the Echo ' function is: '. $mmm. ' <br> '; Echo ' <p> ';//optional parameters, wherein $tax is optional parameter, fill also line, do not fill the function values ($price, $tax = "") { $price + = $tax; Echo ' Price is: '. $price. ' <br> ';} VALUES (100,20), values (+), echo ' <p> ',//function &rexample2 ($tmp =0) { $tmp = $tmp. ' 123456 '; return $tmp; } $str 5 = &example2 ("Kankan"); Echo $str 5. ' <p> ';? >
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Chubby Learn PHP summary 2-----PHP b variable and assignment