Odd and variables in PHP

Source: Internet
Author: User
Tags variable scope
This article mainly and you introduced the PHP odd and variables related data, this example code combined with text description, to introduce you to the very detailed, need to refer to friends, hope to help everyone.

Example


<?php$string = "Beautiful"; $time = "Winter"; $str = ' This is a $string $time morning! '; echo $str. "<br/>"; eval ("\ $str = \" $str \ ";"); Echo $str;? >

Output:

This is a $string $time morning!
This is a beautiful winter morning!

Analysis:

In PHP,

The variables within the single quotation mark are not executed,

The variables inside the double quotes are executed,

The variables within the single quotation mark inside the double quotes are executed,

But a double-quote variable plus a backslash can make the variable non-executable, such as:


<?php$a=5;echo ' $a '. " \ n "; The result output $aecho "$a \ n"; Result output 5echo "' $a ' \ n '; Results output ' 5 ' echo ' \ $a = '. $a; Result output $a = 5, dot is connector? >eval ("\ $str = \" $str \ ";");

The first \ $str is the meaning, before the equivalent of $str =, is to assign a value to the $STR this variable;
The trailing two backslashes are escaped, which means the quotation marks, so the following is the "$str", the $STR will be executed, that is, "" "The $string with my $name in it. "
Here again, there is a double-quote nested single quote problem, such as the SQL statement
$sql = "SELECT * from user where id = ' $id '";
Here there are double quotation marks nested single quotes, the variables $id will be executed ~
That is, double quotation marks are nested in single quotes and the variables in single quotes are also executed,
Or it can be summed up simply: The variable inside the double quotes is executed if there is no special case of the preceding backslash;
The code that will be executed within this eval double quotation mark is $str = "' This is a $string with my $name in it. ' "

Add:

PHP is a loosely-typed language

In the example above, please note that we do not have to tell the data type of the PHP variable.
PHP automatically converts the variable to the correct data type based on its value.
In languages such as C and C + + and Java, a programmer must declare its name and type before using a variable.
PHP variable Scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable refers to the part of the script that a variable can be referenced/used.
PHP has three different scope of variables:
Local (partial)
Global (globally)
Static (statically)
Local and Global Scopes

Variables declared outside the function have Global scope and can only be accessed outside of the function.

Variables declared inside a function have a local scope and can only be accessed inside the function.

The following example tests for variables with local and global scopes:

Instance


<?php$x=5; Global scope function MyTest () {$y = 10;//local scope echo "<p> variable:</p> inside the test function"; echo "variable x is: $x"; echo "<br>"; ech o "Variable y is: $y";}

MyTest ();


echo "<p> variables outside the test:</p>"; echo "variable x is: $x"; echo "<br>" echo "Variable y is: $y";? >

In the example above, there are two variables $x and $y, and a function myTest (). $x is a global variable because it is declared outside of a function, and $y is a local variable because it is declared within a function.

If we output a value of two variables inside the myTest () function, $y will output the locally declared value, but cannot $x value because it is created outside of the function.
Then, if you output a value of two variables outside of the myTest () function, the value of the $x is output, but the $y value is not output because it is a local variable and is created inside MyTest ().

Note: You can create local variables of the same name in different functions, because local variables can only be recognized by the function in which they are created.

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.