PHP in progress-variable description and string dynamic Insert variable related explanation

Source: Internet
Author: User
Tags what php
This article mainly introduces the PHP is in progress-variable details and string dynamic insertion of variable methods, very good, with reference value, the need for friends can refer to the next

In PHP, variables are $+ variable names, and variable names follow the naming rules for identifiers, which can be preceded by letters and underscores, and can be made into valid variable names by numbers, underscores, and letters.

Variable declaration

All variables should be declared before they are used, and it is best to take notes, although you can not display the declared variables in PHP. After declaring a variable, you can assign a value to the variable, and the assignment of the variable has two types of value assignment and the reference assignment value.

<?php  #合法的声明变量  $_name;  $account;  $show _title;  #赋值  $color = "Red";  #引用赋值  $user _color=& $color;? >

Scope of the variable

Variables can be declared anywhere in the PHP script, but scopes vary depending on the location.

Local variables

The general local variable is declared in the function, that is, it can only be referenced in the function, the change in function exit and its value is destroyed. If the variable is used outside of the function, PHP treats it as a different variable, with no relation to the variable in the function.

<?php  $x =5;  function show_x () {    $x =3;    echo $x;  }  Show_x ();  echo $x;? >

function parameters

function parameters are more transitive in two ways, passed by value and passed by reference, and if passed by value, the scope of the parameter is limited to the function, and the argument is destroyed after the function exits. But passed by reference, the scope of the parameter is not only within the function.

<?php  $cost _fish=20.10;  $cost _apple=2.45;  #引用参数前面需要加上 & Symbol  function rise ($cost _fish,& $cost _apple) {    $cost _fish++;    $cost _apple++;  }  Rise ($cost _fish, $cost _apple);  echo $cost _fish. " Value Pass Parameters <---> Reference pass parameters. $cost _apple;? >

Global variables

You can access global variables anywhere in the script, but you need to use the keyword Global explicit declaration when you want to modify global variables in a function.

<?php  $x =5;  function show_x () {    global $x;    $x + +;  }  Show_x ();? >

Global variables can also be declared by $global arrays

<?php  $x =5;  function show_x () {    global $x;    $x + +;  }  Show_x ();? >

It is important to use global variables with caution, which can easily cause code clutter when used too much.

static variables

Static variables differ from function arguments in that static variables are declared with the static keyword, so that static variables are not destroyed after the function exits, and this value is retained when the function is called again.

<?php  function spend () {    static $date =0;    $date + +;    echo $date;  }  Spend ();  Spend ();? >

PHP pre-defined variables

Many variables are pre-defined in PHP and can be accessed anywhere the script is executed, mainly providing a lot of information about the environment.

  Print out $_server predefined variable information  foreach ($_server as $var + $value) {    echo "$var + = $value <br/>";  }

First look at what PHP is doing-the string dynamically inserts the contents of the variable. Detailed details are as follows:

In PHP, strings are generally enclosed in double or single quotation marks.

  echo "Dick and Harry Harry called Ober together to ' drink '."

If you want to dynamically insert data into a string, we can also use {} to identify the dynamic part of the string, in addition to using the. Number concatenation.

<?php  $name = "Zhao Liu";  Echo ' dick and Harry Harry \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ' "Drink \ '.";  echo "</br>";  echo "Dick and Harry Harry \ \ $name go to \ ' drink '.";  echo "</br>";  echo "Dick and Harry Harry \ nthe {$name} to go ' drinking '.";  echo "</br>";  Echo ' dick and Harry Harry \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ' Drink \ '.  echo "</br>";  Echo ' dick and Harry Harry \ {$name} go ' drinking '.  echo "</br>";? >

Through the above code we find:

    • Inserting a variable directly in double quotation marks is not valid;

    • The backslash () character can be escaped in double quotes, but the single quotation mark in double quotes does not need to be escaped by a backslash;

    • In the single quotation mark, in addition to escaping the single quotation mark, the other character backslash escape is invalid;

    • You can insert variables dynamically in a double-quoted string by {}.

Summarize

The above is a small part of the introduction of PHP is in progress-variable details and string dynamic insertion variables, I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of PHP Chinese network!

Articles you may be interested in:

PHP uses telegram interface to implement a free message notification function

PHP implementation of machine learning naïve Bayesian algorithm detailed

PHP implementation of single-link list flipping Operation example explained

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.