Home > Backend Development > PHP Tutorial > The difference between PHP assignment by value and assignment by reference_PHP Tutorial

The difference between PHP assignment by value and assignment by reference_PHP Tutorial

WBOY
Release: 2016-07-21 15:32:52
Original
1093 people have browsed it

Assignment by value: When assigning the value of an expression to a variable, the value of the entire original expression is assigned to the target variable. This means that, for example, changing the value of one variable while the value of one variable is assigned to another variable will not affect the other variable.

Copy code The code is as follows:

$a=123; $a =123;
$b=$a; $b=&$a;
$a=321; $a=321;
Echo”$a,$b”;//display “321,123” Echo "$a,$b";//Display "321,321"
?> ?>
Reference assignment: The new variable simply references the original variable, changing the new variable Will affect the original variable using reference assignment, simply add an & symbol in front of the variable to be assigned (source variable)
Type trick PHP does not require (or does not support) explicit type definition in variable definition; variable type is determined by the context in which the variable is used. That is, if you assign a string value to the variable var, var becomes a string. If you assign an integer value to var, it becomes an integer.
Type cast
The allowed casts are: (int), (integer) - converted to integer (bool), (boolean) - converted to Boolean (float), (double), (real) - Convert to floating point type (string) - Convert to string (array) - Convert to array (object) - Convert to object Settype() for type conversion
Function Settype()
[code]
$foo = "5bar"; // string
$bar = true; // boolean

settype($foo, "integer"); // $foo is now 5 (integer)
settype($bar, "string"); // $bar is now "1" (string)
?>

Variable scope The scope of the variable is The context it defines (that is, its scope of effect). Most PHP variables have only a single scope. This single scope span also includes files introduced by include and require.
Static variable Another important feature of variable scope is static variable. Static variables only exist in the local function scope, but their values ​​are not lost when program execution leaves this scope.
Array An array in PHP is actually an ordered graph. A graph is a type that maps values ​​to keys. This type is optimized in many ways, so it can be used as a real array, or a list (vector), a hash table (an implementation of a graph), a dictionary, a set, a stack, a queue, and many more possibilities. Since you can use another PHP array as a value, you can also simulate a tree easily.
Define array() You can use the array() language structure to create a new array. It accepts a number of comma-separated key => value parameter pairs.
array( key => value , ... )
// key can be integer or string
// value can be any value
Copy code The code is as follows:

// Create a simple array foreach ($ array as $i => $value) {
$array = array(1, 2, 3, 4, 5); unset($array[$i]);
print_r($array); }
print_r($array);
//Add a cell (note that the new key name is 5, not 0 as you might think)
$array[] = 6;
print_r($ array); // Reindex:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>

The unset() function allows to unset keys in an array. Be aware that the array will not be reindexed.
Copy code The code is as follows:

$a = array( 1 => 'one ', 2 => 'two', 3 => 'three' );
unset( $a[2] );
/* will produce an array, defined as
$a = array ( 1=>'one', 3=>'three');
instead of
$a = array( 1 => 'one', 2 => 'three');
*/
$b = array_values($a);
// Now $b is array(0 => 'one', 1 =>'three')
?>

Constructor
void __construct ([ mixed $args [, $... ]] )
PHP 5 allows developers to define a method as a constructor in a class. Classes with a constructor will call this method every time an object is created, so it is very suitable for doing some initialization work before using the object.
Note: If a constructor is defined in a subclass, the constructor of its parent class will not be called implicitly. To execute the parent class's constructor, you need to call parent::__construct() in the child class's constructor.
Example#1 Using the new standard constructor
Copy the code The code is as follows:

class BaseClass {
function __construct() {
print "In BaseClass constructorn";
}
}
class SubClass extends BaseClass {
function __construct() {
parent::__construct();
print "In SubClass constructorn";
}
}
$obj = new BaseClass();
$obj = new SubClass();
?>

The fields in double quotes will be interpreted by the compiler and then output as html code. The words inside the single quotes are not interpreted and are output directly. $abc='my name is tom'; echo $abc//The result is my name is tom; echo'$abc'//The result is $abc; echo "$abc"//The result is my name is tom


Access control Access control of properties or methods is achieved by adding the keywords public, protected or private in front. Class members defined by public can be accessed from anywhere; class members defined by protected can be accessed by subclasses and parent classes of the class in which they are located (of course, the class in which the member is located can also be accessed); and by private The defined class members can only be accessed by the class in which they are located.
Copy code The code is as follows:

class MyClass
{
public $ public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}

Abstract classes Abstract classes and abstractions were introduced in PHP 5 method. It is not allowed to create an instance of a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract are merely a signal for declaring methods and do not define their implementation.
When inheriting from an abstract class, the declaration of tags for all abstract methods in the parent class must be defined by the subclass; in addition, these methods must be defined with the same access attributes. For example, if a method is defined as a protected type, the execution function must be defined as protected or public.
Interface Object interface allows you to create execution code for methods of a specified class without having to explain how these methods are operated (processed) . The interface is used to define the interface keyword, again as a standard class, but no methods have their contents defined. All methods in an interface must be declared public. This is a characteristic of the interface. implements (execution, implementation) In order to implement an interface, the implements operation is used. All methods in an interface must be implemented inside a class; omitting these will result in a fatal error. A class can implement multiple interfaces if desired by separating each interface with a comma.
Overloaded method calls and member access can be loaded through the __call, __get and __set methods. These methods will only be triggered when you try to access an object or inherited object that does not contain members or methods. Not all overloaded methods must be defined as static. Starting from PHP 5.1.0, you can also overload the isset() and unset() functions one by one through the __isset() and __unset() methods.
The PHP $_GET variable obtains the "value" from the form through the get method. When using the "$_GET" variable, all variable names and values ​​will be displayed in the URL address bar; therefore, you can no longer use this method when the information you send contains passwords or other sensitive information.
The purpose of the PHP $_POST variable is to get the form variables sent by the method = "post" method.
Case
Copy code The code is as follows:


Enter your name:

Enter your age:




Cookies are usually used to authenticate or identify a user. A cookie is a small file sent to the user's computer through the server. Each time, when the same computer requests a page through the browser, the previously stored cookie is also sent to the server. You can use PHP to create and get cookie values.
Copy code The code is as follows:

setcookie("user", "Alex Porter", time()+3600); ?>

Get cookie value// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>

The purpose of PHP session variables is to store the user’s session information or change the user’s session settings. The Session variable stores information about a single user and can be used by all pages.

The Mvc pattern separates the presentation of the application from the underlying application logic into three Part: Model View Controller
When the Zend_controllers route sends a user request, it will automatically look for a file named nameController.php in the controller directory, where name corresponds to the specified controller name, which means that the name is The news controller corresponds to a file named newscontroller.php
Smarty is a template engine written in PHP, which allows you to easily separate application output and presentation logic from application logic
ZEND configuration
1 , create local parsing C:WINNTsystem32driversetchosts
127.0.0.1 phpweb20 127.0.0.1 phpmyadmin
2. httpd.conf D:AppServApache2.2conf
(1) Open the rewrite engine hpptd.conf (the one without # can Open module) #LoadModule rewrite_module
Remove the preceding #
(2) Open the virtual host #Include conf/extra/httpd-vhosts.conf Remove the preceding #
3. httpd-vhosts.conf
Copy code The code is as follows:


ServerName phpweb20
DocumentRoot "d:appservwwwphpweb20htdocs"

AllowOverride All
Options All

php_value include_path ".;d:appservwwwphpweb20include;D:AppServphp5ext"


4. Create .htaccess
5. Modify php.ini
C:WINNT
Import
php_pdo.dll
php_pdo_mysql.dll

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322746.htmlTechArticleAssignment by value: When the value of an expression is assigned to a variable, the value of the entire original expression is assigned to the target variable. This means that, for example, when the value of one variable is assigned to another variable...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template