Beginner's view: Introduction to the use of variables in PHP

Source: Internet
Author: User
Tags definition ini

Like most structured programs, there are so-called global variables and local variables, and PHP has the same approach in this respect.

When a PHP program executes, the system retains a region of the global variable in memory. In practical use, you can remove the desired variable by $globals ["Variable name"]. In a user-defined function or program, you can use the $globals array to retrieve the required variables. Of course, don't forget that PHP variables are case-sensitive, wrong case but called 100 years, variables will not come out.

The $globals array is a special variable in a PHP program that does not have to be defined, and the system automatically matches the relevant variables inside. In a function, you can use it directly without having to $globals whether the array has been defined globally.

Similar to $globals variables, there are $php _errormsg string variables. If the track_errors option in the PHP configuration file (Php.ini/php3.ini) is turned on, there will be global variables $php _errormsg can see the wrong information.

In PHP, the valid scope (scope) of a global variable is limited to the main program and does not affect a variable with the same name in the function, that is, the global variable and the local variable are not violated. To make a variable transparent to a function, you need to use an array of $globals or a global definition.

For example, in a self-developed function, to obtain the file name of the currently executing PHP program page, you can use the $globals ["php_self"] to remove the value of the $php _self.

<?php
This program uses an array of $globals
function MyFunc () {
echo $globals ["php_self"];
}
MyFunc ();
?>

Here is the wrong demonstration, do not imitate, the above is the correct demonstration.

<?php
This is the wrong demonstration.
function Errfunc () {
echo $php _self;
}
Errfunc ();
?>

But the wrong example would be no problem if it were changed to the next case.

<?php
This program uses global definitions
function MyFunc () {
Global $php _self;
echo $php _self;
}
MyFunc ();
?>

To precede a variable with the name of global is to define the variable as a global variable. In this way, you do not need to use the $globals array, you can also allow the variable to enter the function of its own development.

Let's look at the examples of static variables

<?php
Examples of static variables
function MyFunc () {
Static $mystr;
$mystr. = "Ha";
echo $mystr. " <br>\n ";
}
MyFunc (); Ha
MyFunc (); Ha ha
MyFunc (); Ha ha
?>

The variables that are generated by the function at the end of the function disappear, sometimes because of the needs of the program, the function is in the loop, and the static variable (variable) comes in handy when you don't want the variable to disappear every time the function is done. In the example above, before you use the $MYSTR variable, you precede the variable with static, which means that the variable $mystr is a static variable, and the value of the $MYSTR increases every time the MyFunc () function is executed, with one more word for each execution. If you take away the static variable definition, there is no way to add the word.

<?php
Not an example of a static variable (wrong)
function MyFunc () {
$mystr. = "Ha";
echo $mystr. " <br>\n ";
}
MyFunc (); Ha
MyFunc (); Ha
MyFunc (); Ha
?>

Look at a more practical example, which is to process the color of the table, so that the colors of the interlacing are different.

<?php
function Tdbackcolor () {
Static $colorstr;
if ($colorstr = = "808080") {
$colorstr = "C0C0C0";
} else {
$colorstr = "808080";
}
return ($COLORSTR);
}
echo "<table border=1>\n";
For ($i =0 $i <10; $i + +) {
$colorstr =tdbackcolor ();
echo "<tr><td bgcolor=". $colorstr. " > This is the first ". $i." Line </td></tr>\n ";
}
echo "</table>";
?>

The most incredible thing about PHP's variable-use techniques is the variable's variable (variable variable). This is a special technique for leveraging PHP features

<?php
$a = "Hello";
$ $a = "world";
echo "$a, $hello"; Hello, world
echo "$a, {$a}"; Also Hello, world
?>

Mdean@kcnet.com a more absolute example (24-apr-1999), which he called a variable function (variable function)

<?php
function Mycallbackfunction ()
{
Print ("Hello from callback");
}
function MyFunction ($callback)
{
$callback ();
}
Call to MyFunction passing callback
function as parameter
MyFunction ("Mycallbackfunction");
?>

What do you do with the information that the user enters in form? If in the PHP configuration file, Track_vars is set to ON, use the variable name directly. The following example, next.php in the implementation, the system will automatically generate two variables $username and $sex, direct use is good, compared to the traditional CGI to their own parsing, PHP is really amazing.

<form action=next.php method=post>
Name: <input type=text name= "username" ><br>
Sex: <input type=text name= "Sex" ><br>
<input type=submit>
</form>

Because many PHP syntax is a copy of the C language, so PHP in the use of variables, you can use the new variable, as long as the variable initialization before use, not as rigorous as Pascal language, all the variables to be used are defined beforehand. This is of course good and bad: The advantage is to use convenience, freedom, the disadvantage is often because of these freedoms and pay a considerable price adjustment. In the program code short PHP program is certainly not a problem, when the program in hundreds of thousands of lines, even include or require several layers, the question emerges. In any case, keeping good writing habits is the way to avoid wasting your youth.



Related Article

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.