PHP tags, variables, constants

Source: Internet
Author: User
Tags variable scope

PHP Tags

Syntax: There are 4 types of writing formats

1.<?php ...? > highly recommended.

If the current PHP code snippet is the last paragraph of the entire document, you can omit the end tag? (omitted)

Each sentence is separated by a semicolon; If this statement is at the end of PHP, you can omit it. (Provided that PHP finally has?> this end tag)

2.<script language= "php" > ... </script> easy to confuse with JavaScript tags

3.<? ... ? > PHP is not supported by default, you must set Short_open_tag = On in the php.ini file

4.<%.%> shell Style, PHP is not supported by default, you must set asp_tags = on in php.ini file

Comments

Line Comment

# line Comment

/* ... */block comment

Block comment multi-use and function comment

Example:/*

* Functions of the function

* @param parameter type argument name 1 parameter explanation

* @param parameter type argument Name 2 parameter explanation

* @return return value type return value interpretation

*/

PHP Common output statements

  Print Simple output statement, you can output strings, variables, expressions and other items of the value! (Cannot output complex data types such as arrays, objects, etc.)

Example: Print ("hello,world!");

Output Result: hello,world!

  Echo is also a simple output statement, but you can output multiple data at a time, separated by commas!

For example: Echo "Hello", "world!";

Output Result: helloworld!

  Var_dump When testing the code, the most used statements, not only can output the value of one or more data items, you can also print out the type of data items, length and other additional information! And you can output complex data types!

For example: $arr = Array ("A" =>10, "B" =>20, "C" = "abc");

Var_dump ($arr);

Output: Array (3) {["A"]=> Int (Ten) ["B"]=> Int (a) ["C"]=> string (3) "ABC"}

  Print_r is typically used to print a set of complex data types such as group

For example: $arr = Array ("A" =>10, "B" =>20, "C" = "abc");

Print_r ($arr);

Output: Array ([A] = [B] = [C] = = ABC)

  printf is more adept at outputting "mixed products" made up of static text and other variables! The statement typically has two parts: the first half is a string that is quoted in quotes, and if a variable is required inside a string, it is represented by a variable placeholder, with a placeholder representing a different data type. The second part is the variable list, and the variable list should correspond to the previous variable placeholder one by one!

For example: $name = "Zhangsan";

$age = 30;

$home = "cn";

printf ("My name is%s; age is%d;i come from%s", $name, $age, $home);

Output Result: My name is Zhangsan; age is 30;i come from CN

Placeholder

%s : The following variable is considered a string and displayed as a string!

%c: considers the subsequent variable to be an integer and displays the value of the ACSII code corresponding to the integer (value 0~127)

  %d : The following variable is considered an integer and displayed as a signed decimal number (plus or minus)

%u : The following variable is considered an integer and displayed as an unsigned decimal number (no plus or minus)

%o : The following variable is considered an integer and is displayed as an unsigned octal number

%x : The following variable is considered an integer and is displayed as an unsigned hexadecimal number

%f: The following variable is considered a floating-point number and displayed as a floating-point number

variables

Base syntax: $

$ is just a syntax form, representing the following identifier as a variable name! Therefore, $ is not part of the variable name, which identifies the following string as a variable name

declaring variables (increment) PHP is a weakly typed language, mainly reflected in the following two points:

PHP does not need to display the declaration variables, in contrast, the declaration of the variable is generally with the assignment (initialization) of the variable at the same time!

A variable can be any type without specifying a specific type, or you can place a value of any type!

Although the PHP variable does not require a declaration to be displayed, it does not mean that we can directly use a variable that is not initialized at all!

A PHP variable does not need to specify a type, but does not mean that the variable has no data type, and that the variable's data type is the type of the value it holds.

Remove variables ( delete) This time you need to use PHP's built-in functions (System functions) unset to complete!

Modify the variable (change ) to re-assign the value on the line!

get the value of the variable (check) use the $ symbol to find the appropriate variable and output it

variable Variable

There are two parts to the variable: variable name variable value

The variable value is certainly variable, so the meaning of the variable variable is variable name variable, that is, the name of the variable can be replaced by another variable! So, mutable variables are also called variables.

Example: <?php

$a = "Itcast";

$itcast = "hello,world!";

echo $ $a;

Output Result: hello,world!

value passing between variables

Values change between value-passing variables

For example:

<?php

$a = 100;

$b = $a;

$b = 200;

echo $a;

Echo $b;

Output results: 100

200

  A reference pass is also called an address pass, assigning the address of a variable value to another variable! The address of the variable has changed

For example:

<?php

$a = 300;
$b =& $a;
Echo $b;

$b = 200;

echo $a;

Echo $b;

Output results: 300

200

200

pre-defined variables

$_server refers to the collection of browser information and server information that the server can collect! is an array!

What to print in a real project, and access to some of the elements through the data's bracket syntax

$_get is used to receive all data submitted by a user when filling out a form by using GET method.

The features of Get pass value: 1 , when the user submits, the information in the form will be passed to the server-side file after the URL address, the request script name and data using question mark?

segmentation, data and data between the use of & segmentation, the name of the data and the value of the data by equal sign = split!

2. Not very safe! Transfer volume comparison

$_post is also used to receive data from a form, except to receive data submitted by post

Note: Most of the forms are submitted by post!

because: 1, post can pass binary and get way not!

2, Post submitted more data, by default can reach 8M

3, more secure

$_requet ($_request = $_gET + $_pOST +$_cookie) contains all of the user's request data, typically post data and get data

$_cookie,$_session Session Technology

$_files File Upload

$GLOBALS Variable Scope

Constants

Grammar:

 Define ("Constant name", constant value)

In addition, in the new version of PHP, another syntax for defining constants is supported:

Const Constant name = constant value;

(Note: const is a syntax for defining a class constant, and a new version of PHP extends its functionality and can also define normal constants)

Attention:

Constants are global and can be used anywhere in the script! (both inside and outside functions)

Constant name cannot have $

The naming rules for constant names are similar to variables and are more relaxed

The constant name is generally capitalized;

Get_defined_constants ()

A system function that can get all the constants that have been defined! is an array

Pre-defined constants

Magic Constants

The syntax used is consistent with the constant and is predefined, but its value is determined by the location of the code, that is, when the location is different, its value is not the same!

The greatest feature of the magic constant is that it starts with two underscores and ends with two underscores

__file__ The path where the current script is located

__dir__ The directory where the current script resides

__line__ the line number where the current code is located

__function__ The name of the current function

__method__ The name of the current method

__class__ The name of the current class

__namespace__ The name of the current namespace

     

  

PHP tags, variables, constants

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.