Home > Backend Development > PHP Tutorial > Summary of Xiaopang learning PHP 2-----PHP's b variables and assignment

Summary of Xiaopang learning PHP 2-----PHP's b variables and assignment

WBOY
Release: 2016-07-30 13:29:47
Original
905 people have browsed it

1. Overview

Although PHP is a weakly typed language, type conversion is still needed sometimes. Let's write about type conversion, variable definition and variable assignment.

1.1 Type conversion

Type conversion in PHP is the same as C language, very simple, just add the type name enclosed in parentheses before the variable.

[php] view plaincopy

  1. //Type conversion Description: When using the operator to convert a variable, the original value will not be changed, but when using the settype function to convert, the original value will be changed Value
  2. $num = '3.1415926r';
  3. echo 'Use the (integer) operator to convert the variable $num type:' . (integer)$num;
  4. 'Output the value of variable $num: '. $num; 'Use the settype function to convert the variable $num Type result: '
  5. . settype($num,'integer'); echo
  6. 'Output the value of variable $num:'. $num; ;
  7. ?> The settype() function can convert the specified variable to the specified data type. There are some functions in PHP that can be used to detect whether a variable is of a specified type, such as is_bool() to detect whether it is a Boolean type, is_string() to detect whether it is a string type, etc. 1.2 Define constants Constants can be understood as quantities whose values ​​do not change. After the constant value is defined, it cannot be changed anywhere else in the script. The syntax is: define(constant_name, value, case_sensitive), three The parameters are the name of the constant (required), the value of the constant (required), and whether it is case-sensitive (optional). There are two ways to obtain constants: the first is to obtain them directly by using the variable name; the second is to obtain them through the constant() function. To determine whether a constant has been defined, you can use the defined(stringName) function, which returns true if successful, otherwise false.
  8. [php] view plaincopy
  9. //Define constants: define(), get the value of the constant: constant(), determine whether the constant is defined: defined()
  10. define( 'count1','Constant value 2'
  11. );

echo count1;

'count1';

echo constant(

$name

).

'
'
  1. ; //In fact, this constant of count output
  2. echo defined(
  3. 'Message'). '
    '
    ;
  4. ?>

    [php] view plaincopy

    1. //Predefined constants
    2. echo'Current file path:' . __FILE__. '
      '
      ;
    3. echo 'Current number of lines: '; / /82 echo 'Current PHP version information: '
    4. . PHP_VERSION. '
      '
      ; echo
      ' Current operating system: '
    5. .PHP_OS; echo'
    6. 1.3 Define variables and assign values ​​to variables Unlike many languages, you do not need to declare variables before using them in PHP (you need to declare variables before PHP 4.0), you only need to assign values ​​to variables.
    7. [php] view plaincopy

//Assignment of variables

    //The first type: direct assignment of variables, such as $E='ss';
  1. //Second: Assignment between variables. Assignment between variables means that the two variables use their own memory after assignment, regardless of interference;
  2. //Third Type: reference assignment. The concept of reference is that when the value of one variable is changed, the other one will also change. Use the & symbol to indicate the reference.
  3. //Assignment between variables
  4. $string1
  5. =
  6. 'spcn'
  7. ;
  8. $string2
  9. =
  10. $string1; $string1
  11. =
  12. 'zhuangjia'; 'The value of variable string2 is:'. $string2
  13. .
  14. '
    '; .'< ;br>';
  15. //Reference assignment $i = 'spcn';
  16. $j = & $i; $i = "hello ,$i";
  17. echo
  18. 'j's value is: '. $j.'
    '
  19. ;
  20. The value of echo 'i is: '
  21. . $i.'
    '
    ;
  22. ?> [php] view plaincopy
    1. //Global variables can be accessed anywhere in the program, but are not available in user-defined functions. If you want to use it, use the global keyword declaration.
    2. $zy = 'won't see';
    3. $zyy = 'will see' ;
    4. function lxt(){
    5. // echo $zy .'
      ';
    6. global $zyy;
    7. echo$zyy.'
      '
      ;
    8. }
    9. lxt();
    10. ?>
    11. $trans
    12. = ' you are met';
    13. echo $change_name.
    14. echo
    15. $$change_name ; //The implementation principle is similar to escape characters, $change_name represents trans, and then adds a $ symbol, that is, the output is $trans
    16. echo '

      '; ?> <

    17. [php] view plaincopy
    18. //@Operator: Shield error messages
    19. $err = @(5/0);
    20. echo

    $err.

    '
    ';

    ?>
    1. / /ternary operator
    2. $value = 100;
    3. $res = ($value == 100) ?'Ternary operation' :
    4. 'No value changed'
    5. ; gt;'
    6. ;
    7. echo
    8. '

      ';

    9. ?> Function is to write some repeatedly used functions in In an independent code block, call it separately when needed. Create a function syntax: function fun_name($str1,$str2....$strn){}, and then call it with fun_name(XXX).
    10. [php] view plaincopy
      1. //Simple function
      2. function countNumber($num1, $num2){
      3. return"$num1 * $num2 = ".$num1 * $num2.'
        '
        ;
      4. }
      5. echo countNumber(10,10);
      6. //Transfer between functions Parameters //Pass by value
      7. functionexample($m
      8. ){ $m
      9. = $ m * 5 + 10; echo
      10. ' The value of $m within the function is: '.$m.'
      11. ; } $mm = 1; example($mm
      12. ); echo
      13. '$m outside the function The value is: '.$mm
      14. .'
        '
        ; ;
      15. //Pass by reference function example1(&
      16. $m
      17. ){ $m =
      18. $m
      19. * 5 + 10;
      20. echo
      21. 'The value of $mmm within the function is:'
      22. .$m.'
        ' ;
      23. } $mmm = 1;
      24. example1($mmm); The value of $mmm outside the echo ' function is: '.$mmm.'
        '
      25. ;
      26. //Optional parameters, where $tax is an optional parameter, you can fill it in or leave it out
      27. function
      28. values(
      29. $price,$tax =""){ $price
      30. +=
      31. $tax; 'The price is:'.$ price
      32. .'
        '
      33. ; }
      34. values(100,20); echo '

        ';

      35. //Reference to the function itself &rExample2($tmp
      36. =0){                                                                                                                                         return$tmp; }
      37. $str5 = &example2("kankan");
      38. echo $str5. '

        ';

      39. ?>

      The above introduces the Xiaopang Learning PHP Summary 2-----PHP's b variables and assignments, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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