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] View plaincopy
-
- //Type conversion Description: When using an operator to convert a variable, the original value is not changed, and the original value is changed when using the Settype function conversion
- $num = ' 3.1415926r ' ;
- Echo ' Use the (integer) operator to convert the variable $num type: '. (integer) $num ;
- Echo '
';
- Echo ' The value of the output variable $num: '. $num ;
- Echo '
';
- Echo ' Use the Settype function to convert a variable $num type result: '. Settype ($num,' integer ' );
- Echo '
';
- Echo ' The value of the output variable $num: '. $num ;
- Echo '
';
- ?>
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] View plaincopy
- !--? php
- //Defines constant: define (), gets the value of the constant: constant (), determines whether the constant is defined: defined ()
- define ( ' Message ' , ' constant value ' ');
- echo Message. '
' ;
- define ( ' count1 ' , ' constant value 2 ' );
- echo count1;
- $name = ' count1 ' ;
- echo constant ( $name ). '
' ; //This constant of the count of the output is actually
- echo defined ( ' Message ' ). '
' ;
- ?>
[PHP] View plaincopy
-
- //Pre-defined constants
- Echo ' current file path: '. __file__ . '
';
- Echo ' Current number of rows: '. __line__ . '
'; //82
- Echo ' current PHP version information: '. Php_version. '
';
- Echo ' current operating system: '. Php_os;
- Echo '
';
- ?>
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] View plaincopy
-
- //Assign value of variable
- ///First: variable direct assignment, e.g. $e= ' SS ';
- ///second: the assignment between variables, the assignment between the variables refers to the assignment of the two variables using their own memory, gu no interference;
- ///Third: Reference assignment, the concept of reference is that when you change the value of one of the variables, the other changes as well, using the & symbol to represent the reference.
- assignment between//variables
- $string 1 = ' SPCN ' ;
- $string 2 = $string 1 ;
- $string 1 = ' Zhuangjia ' ;
- Echo The value of the ' variable string2 is: '. $string 2 . '
';
- Echo The value of the ' variable string1 is: '. $string 1 . '
';
- Echo '
';
- //reference Assignment
- $i = ' SPCN ' ;
- $j = & $i ;
- $i = "Hello, $i" ;
- Echo The value of ' J ' is: '. $j . '
';
- Echo The value of ' I ' is: '. $i . '
';
- ?>
[PHP] View plaincopy
-
- //global variable can be accessed anywhere in the program, but is not available in user-defined functions. Use the Global keyword statement if you want to use it.
- $zy = ' won't see ' ;
- $zyy = ' will see ' ;
- function LXT () {
- echo $zy. '
';
- Global $zyy;
- Echo $zyy. '
';
- }
- LXT ();
- ?>
-
- //variable variable
- $change _name = ' Trans ' ;
- $trans = ' You're met ' ;
- Echo $change _name. '
';
- Echo $ $change _name ; //Implementation principle similar to escape character, $change _name represents trans, and then adds a $ symbol, that is, the output is $trans
- Echo '
';
- ?>
1.4 operator
Operators, like other languages, include +-*/() | ^ ~ <<>< span=""><>
[PHP] View plaincopy
- !--? php
- //@ operator: Masking error messages
- $err = @ (5/0);
- echo $err . '
' ;
- ?>
-
- !--? php
- //ternary operator
- $value = +;
- $res = ( $value = =)? ' ternary operation ' : ' No change value ' ;
- echo ' haha ' . $res . '
' ;
- echo '
' ;
- ?>
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] View plaincopy
-
- //Simple functions
- function CountNumber ($num 1,$num 2) {
- return "$num 1 * $num 2 =". $num 1 * $num 2 . '
';
- }
- Echo CountNumber (10,10);
- Echo '
';
- pass parameters between//functions
- //By Value delivery method
- function Example ($m) {
- $m = $m * 5 + ten;
- Echo ' The value of $m within the function is: '. $m . '
';
- }
- $mm = 1;
- Example ($mm);
- Echo ' The value of $m outside the function is: '. $mm . '
';
- Echo '
';
- //By reference delivery method
- function example1 (&$m) {
- $m = $m * 5 + ten;
- Echo ' The value of $mmm within the function is: '. $m . '
';
- }
- $mmm = 1;
- Example1 ($mmm);
- Echo ' The value of $mmm outside the function is: '. $mmm . '
';
- Echo '
';
- //optional parameters, where $tax is an optional parameter, fill the line, do not fill the line
- function values ($price,$tax="") {
- $price += $tax ;
- Echo ' price is: '. $price . '
';
- }
- Values (100,20);
- VALUES (100);
- Echo '
';
- //reference to the function itself
- function &rexample2 ($tmp=0) {
- $tmp = $tmp . ' 123456 ' ;
- return $tmp;
- }
- $STR 5 = &example2 ("Kankan");
- Echo $str 5. '
';
- ?>
The above introduces the small Fat Learning PHP summary 2-----PHP b variables and assignments, including the content, I hope to be interested in PHP tutorial friends helpful.