PHP Learning One (base) 1th/2 page _php Basics

Source: Internet
Author: User
Tags arrays constant null null php script scalar type null what php
The application of Web

When the client requests to the server's program, the Web server responds to the Request Response page, when the page contains a PHP script, the server will give the PHP interpreter to interpret the execution, the generated HTML code back to the client, the client's browser to interpret the HTML code, A page that eventually forms a Web page format.
What PHP can do

PHP is mainly used in three areas:
PHP Analyzer, a Web server and a Web browser.
PHP syntax structure

The lexical structure of a programming language (lexical structure) is a collection of basic rules that govern how to write a program in a language.
User-defined function names or class names are case-insensitive, and variables are case-sensitive. This means that $name, $NAME, and $name are three different variables.
PHP separates the simple statements with semicolons.
PHP comments

PHP supports C, C + +, and shell script-style annotations as follows:
Single-line Comment
/*/Multiline Comment (Note: cannot be nested)
# script Annotations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Constant

A constant is a fixed value, defined with a simple identifier, which is Changshime considered to be case sensitive.
By convention constant identifiers are always capitalized.
Define () uses this function to define constants.
String (string) constants are divided into: built-in constants and custom constants.
Constants can only contain scalar data (Boolean), Integer (reshape), float (floating point))

Built-in constants: Constants that the PHP system provides that will not change in any page value

Php_os: Displays the operating system version of the server
Php_version: Show PHP version

Some common system constants
__file__:php file name, if it is a reference file, display the reference file name
Number of rows in __line__:php file
TRUE false: A constant that represents true or false
E_error: Indicates the most recent error in the code
E_warning: Indicates the most recent warning in the code
E_parse: Analyzing where the code has potential problems
E_notice: A place unusual but not necessarily wrong for something to happen

Custom Constants
To define constants with define ()
Define ("MyComputer", "IBM");
Defining constants: The value of the MyComputer constant is IBM
Defined ("MyComputer");
Detects whether a constant is defined, returns 1 if it is defined, or returns empty

Variable
A dollar sign ($) in PHP follows a variable name, representing a variable. The name of the variable is case sensitive
< PHP
$var = ' Bob ';
$Var = ' Joe ';
echo "$var, $Var"; Output "Bob, Joe" can output two variable names at the same time
$4site = ' not yet '; Illegal variable name, starting with a number cannot begin with a number
$_4site = ' not yet '; The legal variable name, which begins with an underscore at the beginning of the following line
$i site is = ' Mansikka '; Legal variable name, which can be used in Chinese but not recommended
Isset ($var)//Check whether the variable is defined
Unset ($var)//delete variable $var
Empty ($var)//Determine whether the value of a variable exists
echo $var//Empty
>

Variable variable
A variable variable obtains the value of a normal variable as the variable name of this variable variable
< PHP
$a = ' Hello '; Ordinary variable
$ $a = ' world '; Variable variable variables use the value of a normal variable as the name of a variable variable
echo "$a ${$a}"; Output: Hello World
echo "$a $hello"; Output: Hello World
>

Constants and variables are different
Constants are not preceded by a dollar sign ($);
Constants can only be defined with the Define () function, not by assignment statements;
Constants can be defined and accessed anywhere, regardless of the variable-scope rules;
Constants cannot be redefined or redefined once they are defined;
The value of a constant can only be a scalar

Data type
Four scalar types:
Boolean (Boolean)
Integral type (integer)
Float (float) (floating point number, also double)
Strings (String)
Two types of composite:
Arrays (Array)
Objects (object)
Finally, there are two special types of:
Resources (Resource)
NULL NULL
PHP is a very weak type language.
In PHP, the type of a variable is usually not set by the programmer, or rather, based on the context used by the variable at runtime (that is, the value of the variable).
Instance:
< PHP
$bool = TRUE; Boolean type
$STR = "Foo"; String
$int = 12; Integral type
Echo GetType ($bool); Output Boolean (GetType gets the type of the variable)
Echo GetType ($STR); Output string
>

Integral type
Integer values can be specified in decimal, hexadecimal, or octal notation, preceded by an optional symbol (-or +).
< PHP
$a = 1234; Decimal number
$a =-123; A negative number
$a = 0123; Octal number (equal to decimal 83)
$a = 0x1a; Hexadecimal number (equal to decimal 26)
>

Floating-point type
Floating-point numbers (also called floating-point numbers, doubles, or real numbers) can be defined in any of the following syntax:
< PHP
$a = 1.234;
$a = 1.2e3;
$a = 7E-10;
>

String
A string is a series of characters. In PHP, characters and bytes are the same, that is, the possibility of a total of 256 different characters. This also implies that PHP does not have local support for Unicode. (For a specific chapter on the character behind the string type)
< php $str = "Hello world!"; >

Boolean type
This is the simplest type. A Boolean expresses a true value, and can be or FALSE.
The following values are considered false when other types are converted to Boolean types:
Boolean value FALSE
Integer value 0 (0)
Floating-point value 0.0 (0)
Blank string and string "0"
An array with no member variables
Objects with no cells (PHP 4 only)
Special type NULL (includes variables that have not yet been set)
All other values are considered TRUE (including any resources).

Array
Arrays are an important type of data in PHP. A scalar can hold only one data, while an array holds multiple data.
$my =array (' 1 ', ' 2 ', ' abc ', ' d ');
Objects (object)
An object is an advanced data type that you will learn later

Resources (Resource)
Resources are created and used by specialized functions.

Type cast
Type cast in PHP: precede the variable to be converted with the target type enclosed in parentheses.
The allowed casts are:
(int), (integer)-Convert to integral type
(bool), (Boolean) – Convert to Boolean
(float), (double), (real)-Convert to floating-point type
(string)-Convert to String
(array)-Convert array
(object) – Convert to Object
< PHP
$foo = 10; $foo is an integer
$bar = (Boolean) $foo; $bar is a Boolean
>
Current 1/2 page 12 Next read the full text

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.