Chapter 2 PHP basics-php code writing

Source: Internet
Author: User

1. Several PHP code embedding styles on web pages
Standard or short style is recommended
Copy codeThe Code is as follows:
<? Php
// Standard style
Echo 'Hello World! ';
?>
<?
// Short style
Echo 'Hello World! ';
?>
<Script language = "php">
// Script style
Echo 'Hello World! ';
</Script>

2. Four Methods of code annotation
Copy codeThe Code is as follows:
<? Php
// Single line comment
/*
* Multi-line comment
*/
# Shell style comment
/**
* PHPdoc style Annotation
*/
?>

3. Several Methods for outputting strings to the browser
Copy codeThe Code is as follows:
<? Php
/*
* Echo function: outputs a string to the browser.
* Function return value: void
*/
Echo 'echo function! ';
Echo ('<br/> ');
/*
* Echo function: outputs a string to the browser.
* Function return value: int
*/
Print 'print function ';
Echo ('<br/> ');
Echo print 'echo value of print function .';
Echo ('<br/> ');
/*
* Printf function: outputs a string to the browser.
* Function return value: the length of the printed string
*/
Printf ("a weekend have % d days", 7 );
Echo ('<br/> ');
Echo printf ("a weekend have % d days", 7 );
Echo ('<br/> ');
/*
* Sprintf function: saves the string to the memory.
* Function return value: the saved string itself.
*/
Sprintf ('sprintf function ');
Echo ('<br/> ');
Echo sprintf ('sprintf function ');
Echo ('<br/> ');
?>

Output result:
Echo function test!
Print function test.
Print function test. 1
A weekend have 7 days
A weekend have 7 days. 23
Sprintf function test
Common Type Indicator

Type

Description

% B

Integer in binary format

% C

Integer, displayedASCIICharacter

% D

Integer, displayed as a signed decimal number

% F

Floating Point Number, displayed as a floating point number

% O

Integer, which is displayed as the octal number

% S

String, displayed as a string

% U

Integer. It is displayed as an unsigned decimal number.

% X

An integer in hexadecimal format.

% X

An integer in upper-case hexadecimal format.

Iv. Identifiers and variables
1. Basic rules for identifiers:
1) An identifier can be of any length and can contain any letter, number, or underline.
2) The identifier cannot start with a number.
3) in PHP, identifiers are case sensitive.
4) A variable name can be the same as a function name.
2. Variable assignment:
Copy codeThe Code is as follows:
<? Php
$ Sum = 0;
$ Total = 1.22;
$ Sum = $ total;
Echo $ sum; // 1.22
?>

3. Data Type of the variable:
Basic Data Type

Type

Name

Integer

Integer

Float

Single-precision floating point number

Double

Precise floating point number

String

String

Boolean

Boolean

Array

Array

Object

Object

4. Type strength
PHP is a dynamic language and a very weak type language. When running a program, you can dynamically change the type of the variable.
5. type conversion:
Implicit type conversion:
Copy codeThe Code is as follows:
<? Php
$ Sum = 0;
$ Total = 1.22;
$ Sum = $ total;
Echo gettype ($ sum); // double
?>

Explicit type conversion:
Copy codeThe Code is as follows:
<? Php
$ Sum = 100;
$ Total = (string) $ sum;
Echo gettype ($ sum); // string
?>

Use the settype () function for type conversion. The return value 1 indicates that the conversion is successful, and the null value indicates that the conversion is failed.
Copy codeThe Code is as follows:
<? Php
$ Sum = 58;
Echo settype ($ sum, "float ");
Echo $ sum; // 58
Echo gettype ($ sum); // double
?>

6. Function for variable Detection:

Function

Function

Return Value

Gettype ()

Get the variable type

One of the basic data types

Settype ()

 Set the variable type

Bool (1: true 0: false (or ''))

Isset ()

Used to determine whether a variable exists

Bool

Unset ()

Release a given variable

Void

Empty ()

Checks whether the value of a variable is null.

Bool

Is_int () is_integer ()

Check whether the variable is an integer.

Bool

Is_string ()

Checks whether the variable is a string.

Bool

Is_numeric

Checks whether the variable is a numeric or numeric string.

Bool

Is_null

Check whether the variable isNULL

Bool

Intval ()

Returns the integer of a variable.

Int

Basic use of Isset ()
Copy codeThe Code is as follows:
<? Php
$ A = 10;
Echo isset ($ a); // 1
?>
<? Php
Echo isset ($ B );//''
?>

Basic usage of Usset ()
Copy codeThe Code is as follows:
<? Php
$ A = 10;
Unset ($ );
Echo isset ($ );//''
?>

Basic use of Empty ()
Copy codeThe Code is as follows:
<? Php
$ A = 5;
$ B = 1;
$ C = 0;
$ D = "";
$ E = array ();
$ F = null;
$ G = "0 ";
$ H = false;
Echo empty ($ a); // ''(false)
Echo '<br/> ';
Echo empty ($ B); // ''(false)
Echo '<br/> ';
Echo empty ($ c); // 1 (true)
Echo '<br/> ';
Echo empty ($ d); // 1 (true)
Echo '<br/> ';
Echo empty ($ e); // 1 (true)
Echo '<br/> ';
Echo empty ($ f); // 1 (true)
Echo '<br/> ';
Echo empty ($ g); // 1 (true)
Echo '<br/> ';
Echo empty ($ h); // 1 (true)
Echo '<br/> ';
Echo empty ($ f); // 1 (true)
?>

Is_int. Similar functions include: is_float (), is_double (), is_string (), is_bool (), is_array (), is_null (), is_long (), is_object (), is_resource () is_numeric (), is_real (), etc.
Copy codeThe Code is as follows:
<? Php
$ A = 11;
$ B = 1.23;
$ C = 3.1415926;
$ D = "hello ";
$ E = false;
$ F = array ();
$ G = null;
Echo is_int ($ a); // 1
Echo '<br/> ';
Echo is_float ($ B); // 1
Echo '<br/> ';
Echo is_double ($ c); // 1
Echo '<br/> ';
Echo is_string ($ d); // 1
Echo '<br/> ';
Echo is_bool ($ e); // 1
Echo '<br/> ';
Echo is_array ($ f); // 1
Echo '<br/> ';
Echo is_null ($ g); // 1
Echo '<br/> ';
Echo is_numeric ($ a); // 1
?>

Basic use of Intval () functions. Similar functions: floatval (), strval ()
Copy codeThe Code is as follows:
<? Php
$ A = 22.23;
Echo gettype ($ a); // double
Echo '<br/> ';
$ B = intval ($ a); // the original type of $ a is not changed after type conversion.
Echo gettype ($ a); // double
Echo '<br/> ';
?>
<? Php
$ A = 22.23;
Echo gettype ($ a); // double
Echo '<br/> ';
Settype ($ a, "integer"); // the original type of $ aa is changed after type conversion.
Echo gettype ($ a); // integer
Echo '<br/> ';
?>

7. Scope of Variables

Super global variable

Variable name

Function

$ GLOBALS

All global variable Arrays

$ _ SERVER

Server environment variable array

$ _ GET

Variable array passed to the script through GET

$ _ POST

Array of variables passed to the script in POST Mode

$ _ COOKIE

COOKIE variable array

$ _ FILES

Variable array related to File Upload

$ _ ENV

Environment variable array

$ _ REQUEST

Array of variables input by the user

$ _ SESSION

Session variable array


8. Constants
Once defined, it cannot be changed again.
Copy codeThe Code is as follows:
<? Php
Define ("TOTAL", 100 );
Echo TOTAL; // 100
Echo '<br/> ';
Define ("TOTAL", 200 );
Echo TOTAL; // 100
?>

Methods for viewing predefined PHP Constants
Copy codeThe Code is as follows:
<? Php
Phpinfo ();
?>

Reference PHP predefined Constants
Copy codeThe Code is as follows:
<? Php
Echo $ _ SERVER ["SERVER_NAME"]; // localhost
Echo '<br/> ';
Echo $ _ SERVER ["SERVER_PORT"]; // 8090
Echo '<br/> ';
Echo $ _ SERVER ["DOCUMENT_ROOT"]; // D:/AppServ/www
Echo '<br/> ';
?>

V. Access form variables
Three Common Methods
Copy codeThe Code is as follows:
<? Php
Echo $ username; // short style. It is easy to confuse with variable names and is not recommended.
Echo '<br/> ';
Echo $ _ POST ['username']; // medium style, supported after version 4.1.0, recommended
Echo '<br/> ';
Echo $ HTTP_POST_VARS ['username']; // lengthy style, out of date, may be removed in the future
?>

Posttest.html
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> method of obtaining form data </title>
</Head>
<Body>
<Form method = "POST" action = "demo10.php">
Username: <input type = "text" name = "username"/>
<Input type = "submit" value = "submit"/>
</Form>
</Body>
</Html>

6. String connection.
Copy codeThe Code is as follows:
<? Php
Echo "the student name is:". $ _ POST ['username'];
Echo "<br/> ";
Echo "welcome to". "school ";
?>

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.