Web Applications
When the client sendsProgramWhen a request is submitted, the web server sends a response to the corresponding page. When the page contains a PHP script, the server sends it to the PHP interpreter for explanation and execution.CodeSend the code back to the client. The browser of the client interprets the HTML code to form a webpage.
What can PHP do?
PHP is mainly used in three fields:
PHP analyzer, a Web server and a web browser.
PHP syntax structure
Programming LanguageLexical structure refers to a set of basic rules for managing how to write programs in languages.
User-Defined Function names or class names are case-insensitive and variables are case-sensitive. That is to say, $ name, $ name, and $ name are three different variables.
PHP separates simple statements with semicolons.
PHP comments
PHP supports C, C ++, and shell script-style annotations, as shown below:
// Single line comment
/**/Multi-line comment (Note: Nesting is not allowed)
# Script Annotation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Constant
A constant is a fixed value defined by a simple identifier. A constant is case sensitive by default.
By convention, constant identifiers are always capitalized.
Define () uses this function to define constants.
String constants include built-in constants and custom constants.
Constants can only contain scalar data (Boolean), INTEGER (integer), float (floating point number ))
Built-in constants: constants provided by the PHP system that do not change the value on any page
Php_ OS: displays the operating system version of the server.
Php_version: displays the PHP version.
Some common system Constants
_ File __: PHP file name. If it is a reference file, the reference file name is displayed.
_ Line __: Number of PHP files
True False: a constant of true and false.
E_error: indicates the most recent error in the code.
E_warning: Specifies the nearest warning area in the code.
E_parse: analyze potential problems with code
E_notice: the location where exceptions occur but not necessarily errors
Custom constant
Use define () to define Constants
Define ("mycomputer", "IBM ");
Define a constant: the value of mycomputer constant is IBM
Defined ("mycomputer ");
Check whether the constant is defined. If 1 is defined, null is returned.
Variable
In PHP, a dollar sign ($) is followed by a variable name, that is, a variable. Variable names are case sensitive.
<PHP
$ Var = 'bob ';
$ Var = 'job ';
Echo "$ var, $ var"; // the output "Bob, Joe" can output two variable names at the same time
$4 site = 'not yun'; // The variable name is invalid. A variable starting with a number cannot start with a number.
$ _ 4 site = 'not yun'; // valid variable name. The following dashes start with the dashes.
$ I site is = 'mansikka '; // valid variable name; Chinese characters are supported, but not recommended.
Isset ($ var) // check whether the variable is defined
Unset ($ var) // Delete the variable $ VaR
Empty ($ var) // determines whether the value of a variable exists.
Echo $ var // null
>
Variable
A variable obtains the value of a common variable as the variable name of the variable.
<PHP
$ A = 'hello'; // common variable
$ A = 'World'; // variable uses the value of a common variable as the variable name.
Echo "$ A $ {$ A}"; // output: Hello World
Echo "$ A $ hello"; // output: Hello World
>
Constants and variables are different.
There is no dollar sign before the constant ($ );
Constants can only be defined using the define () function, but cannot be defined using the value assignment statement;
Constants can be defined and accessed anywhere, regardless of the variable range rules;
Once defined, a constant cannot be redefined or canceled;
The constant value can only be a scalar.
Data Type
Four scalar types:
Boolean)
Integer)
Float (float) (floating point number, also double)
String)
Two composite types:
Array)
Object)
There are two special types:
Resource)
Null
PHP is a very weak type language.
In PHP, the variable type is usually not set by the programmer. Specifically, it is determined by the context used by the variable at runtime (that is, the value of the variable.
Instance:
<PHP
$ Bool = true; // Boolean
$ STR = "foo"; // string
$ Int = 12; // integer
Echo GetType ($ bool); // Output Boolean (GetType obtains the variable type)
Echo GetType ($ Str); // output string
>
Integer
The integer value can be specified in decimal, hexadecimal, or octal notation. An optional Symbol (-or +) can be added before ).
<PHP
$ A = 1234; // decimal number
$ A =-123; // a negative number
$ A = 0123; // The number of octal bytes (83 in decimal format)
$ A = 0x1a; // hexadecimal number (equal to 26 in decimal format)
>
Floating Point Type
Floating-point numbers (also called floating-point numbers, double-precision numbers or real numbers) can be defined using any of the following syntax:
<PHP
$ A = 1.234;
$ A = 1.2e3;
$ A = 7e-10;
>
String
String is a series of characters. In PHP, the character is the same as the byte, that is, there are a total of 256 different characters. This also implies that PHP does not support Unicode locally. (For more information about the character string types, see the following section)
<PHP $ STR = "Hello World !"; >
Boolean
This is the simplest type. Boolean represents the true value, which can be true or false.
When other types are converted to the boolean type, the following values are considered as false:
Boolean value false
Integer value 0 (0)
Floating point value: 0.0 (0)
Blank string and string "0"
Array without member variables
Objects without cells (applicable only to PHP 4)
Special Type null (including unset variables)
All other values are considered to be true (including any resources ).
Array
Arrays are an important data type in PHP. A scalar can only store one data, while an array can store multiple data.
$ My = array ('1', '2', 'abc', 'D ');
Object)
Objects are advanced data types that will be learned later
Resource)
Resources are created and used by dedicated functions.
forced type conversion
forced type conversion in PHP: add the target type enclosed in parentheses before the variable to be converted.
the following mandatory conversions are allowed:
(INT), (integer)-converted to an integer
(bool), (Boolean) -convert to boolean
(float), (double), (real)-convert to float
(string)-convert to string
(array) -convert to array
(object)-convert to object
$ Foo = 10; // $ foo is an integer
$ bar = (Boolean) $ Foo; // $ bar is a Boolean