PHP Introductory Training tutorial PHP variables use

Source: Internet
Author: User
Tags form post

PHP Introductory Training tutorial PHP variables the use

   PHP programs sometimes have an indeterminate problem with variables, and many problems are caused by improper handling of variables. Here brother Lian php training small, Span style= "font-family: ' The song body '; font-size:14px;" > the PHP variable system says

PHP variables are divided into global variables and local variables.

What is a global variable ?

have learned A friend like c/java/c++ must know the area of the variable declared in main () {}, and PHP is not an explanatory statement

compiled language, we also know PHP does not have the main () {} Principal, what is the declaration? In fact, the PHP page itself is a main () {} As long as it is within the page

instead of variables declared inside a function, we call them global variables ... For example:

<?php

$int _a=0; The initial global variable $int_a assigns it a value of 0

echo "global variable:". $int _a; Print Global variables

function child_a () {//use of local variables

$int _a=0; This is a local variable, although it is the same as the global variable name, but this variable is visible only within child_a

echo "from Child_a:". $int _a;

}

function Child_b () {//Call global variable

Global $int _a; DECLARE call global variable

echo "from Child_b:". $int _a;

}

?>

from an example above, we can see it very simply and clearly. The gap between global and local variables in PHP ...

and we're happy to see PHP provides additional CGI language for server global variables ... These variables are automatically generated by the system when the page is paged in.

These global variables are included in the

$_server ($HTTP _server_vars) (server-supplied related variable service) PHP4.1.0

$_env ($HTTP _env_vars) (Save related environment variables) PHP4.1.0

$_post (Save a variable submitted using the form POST method) PHP4.1.0

$_get (Save the variable submitted using the form Get/uri method) PHP4.1.0

$_cookie (save COOKIE data obtained using the page) PHP4.1.0

$_session (Save in-site session variable) PHP4.1.0

$HTTP _post_vars (with $_post, press linuxforum.net a comment on this is $_post more efficient than it) PHP4.1.0

$HTTP _get_vars (same as $_get, comments ibid.) PHP4.1.0

$_request (including $_get,$_post,$_cookie,$_files) PHP4.1.0

$_files (strictly speaking, this variable is already included in the $_post and $http_post_vars, mainly the file variable submitted by the form POST) PHP4.1.0

$GLOBALS (save an array of all global variables) PHP3.0.0

$php _errormsg (This global variable must be opened in php.ini: Track_error=on)

The above variables are system global variables, you do not need to declare them to be able to use directly, of course, your The PHP version must be higher or equal to the version number that is indicated later.

when using these variables, you don't need to declare them with the Global keyword ...

<?php

function G_p () {

echo "Welcome". $_server[remote_addr]. ". Nice meet I AM ". $_server[server_addr];

Welcome 192.168.0.3. Nice meet I am 192.168.0.1

}

?>

Custom variables, The custom variables provided by PHP are not required to be declared as a compiled language. Instead of being used directly, but if an unreasonable program variable is not a random

I think a few days later, maybe you won't even read it. if you only need an over variable, you can declare it as long as it does not conflict with other variables, and it conforms to the naming rules

It is recommended to use unset to remove it after use.

PHP also provides the concept of variable variables

As if someone had asked this question before.

here is the expression ~, in fact, this is the simplest variable variable method, I think if the C pointer and address familiar people understand this will be very fast.

<?php

$VVV = "Int_a";

$int _a= "VVV";

echo "VVV is:". $vvv; VVV is:int_a

echo "Int_a is:". $int _a; Int_a IS:VVV

Echo ' $ $vvv is: '. $ $VVV; $ $VVV IS:VVV

Echo ' $ $int _a is: '. $ $int _a; $ $int _a is:int_a

?>

another: At the beginning of contact When PHP is lost, it can be used (that is, the commit variable is directly using the value of the name tag as a variable). Over time, the development of the network

The Php.net development team found that there were a lot of people here. Cases:

<?php

for ($int _a; $int _a<100; $int _a++) {

echo "I am $int _a \r\n<br>";

}

?>

the above procedure logically, the program like the system to apply for a new variable $int _a because the default value is not assigned, the system will default to $int_a assigned to 0, this time should be printed 100 lines I am ...

But what if someone passes over this variable ??? For example http://url/count.php?int_a=99 the system will print only one line at this time. I'm just saying this briefly.

if the program is to manipulate your database, or manipulate your confidential files, you need private variables rather than the variables passed over by the outside world ..., I personally think that the PHP development team

The handling of this problem is a reference to the advantages of the compiled language .

But don't worry. If you feel that the page is not affected by external variables, and you do not need to improve to this level of security for the time being, you can use

if ($_global_array) {//To determine if there is an array of global variables

Extract ($_global_array,extr_prefix_same, "_global_array"); Import the variables in the array into the current variable table, and if there is a conflict, precede the global variable with the _global_array prefix

}

because The PHP team has increased the security of the system, so the distributed version has been set to off by default since php4.2.x, and can be turned on if you do not need to use the Register_global option.

Summarize:

PHP is a very good web CGI language, PHP's free and a lot of database close integration, he can complete other server scripts to complete the specific services, ASP provides the middleware, in PHP

It can be loaded into PHP via DL () or by using a static extension. and PHP's variables will be more and more perfect ...


PHP Introductory Training tutorial PHP variables use

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.