Definition and usage of variable constants in PHP

Source: Internet
Author: User
Tags arrays file upload http cookie http post php define php tutorial first row type null

Variables are used to store values, such as numbers, text strings, or arrays.

Once a variable is set, we can use it repeatedly in the script.

All the variables in PHP start with the $ symbol.

The correct way to set variables in PHP is:

$var _name = value; Beginners of PHP tend to forget the $ symbol in front of the variable. If you do that, the variable will be invalid.

Let's try to create a variable that has a string, and a variable that holds a value:


If you have a definition of variables and constants, how many things do you notice? You might have thought:

How do you define a variable, and how is it different from languages like C #?
Is the variable case-sensitive?
Are there other important variables in PHP?

Is the definition of constants and variables the same?
Tell it separately.
1. How do I define a variable, and how is it different from languages like C #?
Variables in PHP are represented by a dollar sign followed by a variable name. Variable names are case-sensitive. For example:
<?php Tutorial
$var = ' Jim ';
$VAR = ' Kimi;
echo "$var, $VAR";//Output "Jim,kimi"
?> You may also be concerned about the naming of variables, as in most languages.
2. Are variables case-sensitive?
As 1 said, case-sensitive.
Note that it is important to note that since PHP4, the introduction of the concept of reference assignment is similar to that in most languages, but I think the most similar one is C + +. Because it also uses the "&" symbol. For example: 1 <?php
2 $foo = ' Bob '; Assign ' Bob ' to Foo
3 $bar = & $foo; Reference by $bar. Note & Symbols
4 $bar = "My name is $bar"; Modify $bar
5 echo $bar;
6 echo $foo; $foo have also been revised.
7?>, like any other language, can only be referenced to variables that have variable names.
3. Other important points in PHP
Predefined variables
Predefined variables are an important concept in PHP. PHP provides a large number of predefined variables. Because many of these variables depend on the version and settings of the running server, and other factors, there is no detailed documentation. Some predefined variables do not take effect when PHP runs on the command line.

Note that in PHP 4.2.0 and later versions, the default value for the PHP instruction Register_globals is off. This is a major change in PHP. Having the register_globals value off will affect the validity of the predefined set of variables in the global scope. For example, in order to get the value of a document_root, you would have to use $_server[' document_root ' instead of the $DOCUMENT _root, as in the case of a $_get[' id ' instead of the $id from the URL http://www.exa Get the ID value in the mple.com/test.php?id=3, or use $_env[' home ' instead $HOME get the value of the environment variable home.

Starting with PHP 4.1.0, PHP provides an additional set of predefined arrays that contain data from the Web server (if available), the running environment, and user input. These arrays are very special, and they automatically take effect on a global scale, for example, automatically in any scope. Therefore, it is often referred to as an automatic global variable (autoglobals) or a Super global variable (superglobals). (There is no mechanism in PHP for users to customize Super global variables.) The super global variable is listed below, and you will notice that the old predefined array ($HTTP _*_vars) still exists. Long-form PHP predefined variables can be masked by setting register_long_arrays from PHP 5.0.0.
The following table is a super global variable for PHP:
Super global variable
Describe

$GLOBALS contains a reference to a variable in the global scope that is valid for each current script. The array's key name is the name of the global variable. $GLOBALS array is present starting with PHP 3.
The $_server variable is set by the Web server or directly associated with the execution environment of the current script. Similar to the old array $HTTP _server_vars array (still valid, but opposed to use).
$_get a variable submitted to the script via a URL request. Similar to the old array $HTTP _get_vars array (still valid, but opposed to use).
$_post the variables submitted to the script via the HTTP POST method. Similar to the old array $HTTP _post_vars array (still valid, but opposed to use).
$_cookie the variables submitted to the script via the HTTP cookie method. Similar to the old array $HTTP _cookie_vars array (still valid, but opposed to use).
$_files the variables submitted to the script via an HTTP POST file upload. Similar to the old array $HTTP _post_files array (still valid but against use)
$_env the variables that the execution environment submits to the script. Similar to the old array $HTTP _env_vars array (still valid, but opposed to use).
$_request A variable that is submitted to a script via the get,post and COOKIE mechanisms, so the array is not trustworthy. The existence of all variables contained in the array and the order of the variables are defined according to the Variables_order configuration instructions in php.ini. This array does not have a direct version prior to PHP 4.1.0. See Import_request_variables ().
$_session the variable currently registered to the script session. Similar to the old array $HTTP _session_vars array (still valid but against use)
Scope of application of variables
Each variable has an application scope, so how does PHP define it? Let's take a look at the following code:
1 <?php
2 $var = 0;
3 function test ($INDEX)
4 {
5 $var = $var +1;
6 echo "the". $index. "Number is". $var. " <br> ";
7}
8 test (1);
9 Test (2)
?> What do you think the above code will show?
If you think it is the following:
Result 1:
The 1 is 1
The 2 number is 2 Sorry, your results are wrong.
In fact, the correct result should be:
Result 2
The 1 is 1
The 2 is 1 so what did you find out about it? We can see that although the code in line 2nd is defined outside, the variable in line 5th is not the same as it is. The variable in line 5th is only used in this function. Further, if I want to call the variable in the first row and display the result 2. The code can be as follows:
1 <?php
2 $var = 0;
3 function test ($INDEX)
4 {
5 Global $var;
6 $var = $var +1;
7 echo "the". $index. "Number is". $var. " <br> ";
8}
9 test (1);
Test (2)
One?> what's the difference between this code snippet and the code snippet above? Note that line 5th has one more global keyword. Get it.
So is there any other way? The answer is yes.
The code is as follows:
1 <?php
2 $var = 0;
3 function test ($INDEX)
4 {
5
6 $GLOBALS ["var"]= $GLOBALS ["var"]+1;
7 echo "the". $index. "Number is". $GLOBALS ["var"]. " <br> ";
8}
9 test (1);
Test (2)
Is there anything special about?> code? That is to use the $globals variable.
PHP also has the argument of static variables. However, static variables are generally used in functions, only local variables. Look at the following code:
1 <?php
2 function Test ()
3 {
4 static $a = 0;
5 echo $a. " <br> ";
6 $a + +;
7}
8 Test ();
9 Test ();
?> results are
1
2
PHP also has a rather exciting feature: variable variables
Variable variables, the variable names of a variable can be dynamically set and used.
Take a look at the following example:
1 <?php
2 $a = "Hello";
3 $hello = "World";
4 echo $a. " ". $ $a;
The results of the 5?> output are actually hello,world. It's amazing. $ $a is actually $hello, because the value of $a is hello.
This is a lot of variables. Look at the constants below.

Constant
Are the constants of PHP not preceded by a const? Let's have a look.
No. PHP must be defined in the following way.
BOOL Define (string name, mixed value [, BOOL case_insensitive])
Name is a constant name and value is a constant. Case_insensitive] is case sensitive. Default is sensitive. For example:
1 <?php
2 define ("CONSTANT", "Hello world.");
3 echo CONSTANT; Outputs "Hello world."
4 echo Constant; Outputs "Constant" and issues a notice.
5
6 Define ("greeting", "Hello you.", true);
7 echo greeting; Outputs "Hello you."
8 echo greeting; Outputs "Hello you."
9
Ten?>

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.

Instance

<?php
$txt = "Hello world!";
$number = 16;
? >php is a loosely typed language (loosely Typed Language)
In PHP, you do not need to declare a variable before you set it.

In the example above, you see that you do not have to declare the data type of the variable to PHP.

Depending on how the variable is set, PHP automatically converts the variable to the correct data type.

In a strongly typed programming language, you must declare the type and name of the variable before using it.

In PHP, variables are declared automatically when they are used.


<?php
Reference
$one = "Test";
two=&amp; $one;//equivalent to address, two variables to an address

Dynamic variables
$one = "######";
$two = "one";
$three = "two";

echo $three. " &lt;br&gt; "; /output "two"
echo $ $three. " &lt;br&gt; "; /output "one"
echo $$ $three. " &lt;br&gt; "; /output "######"

There are 8 types in PHP
4 kinds of scalar: int integer
BOOL Boolean
Float,double,real
String
2 Types of composite: array
Object
2 Special types: Resource type Resource
Empty type null


Declaration of integers
$int = 10; Decimal declaration
$int =045;//Octal statement
$int =0xff;//Hexadecimal Declaration
$float =3.14e+5;//Scientific Counting method
$float =3.14e-5;

It's all false.
$bool =false;
$bool = 0;
$bool = 0.000;
$bool =null;
$bool = "";
$bool = "";
$bool = "0";
$bool =array ();

//String declaration
//1. Single and double quotes can declare a string
//2. The declared string has no length limit
//3. In a double quote string, you can either parse the variable directly and use the escape character directly (you can escape the single quotation mark itself, You can also escape the escape character "")
//4. In a single quote string, you cannot parse a variable directly, nor can you use the escape character,
//5. Double quotes are no longer available in double quotes, and single quotes
//6 are not used in single quotes. It is best to use single quotes,
$str = ' AAAAA ';
$str = "AAAA";
//Delimiter declaration string, a large number of strings
//test is a custom string that cannot be followed by any characters, and no spaces can be
//or end with test this custom string, no characters
$str =&lt; &lt;&lt;test
This write content ...
Test;
?

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.