Chubby Learn PHP summary 2-----PHP b variable and assignment

Source: Internet
Author: User
Tags learn php
I. GENERAL statement

Although PHP is a weakly typed language, it is sometimes necessary to use a type conversion. Here's a look at type conversions and defining variables and assigning values to variables.

1.1 Type conversions

The type conversion in PHP is as simple as the C language, with only the type name enclosed in parentheses before the variable.

[PHP] View plaincopy

  1. //Type conversion Description: When using an operator to convert a variable, the original value is not changed, and the original value is changed when using the Settype function conversion
  2. $num = ' 3.1415926r ' ;
  3. Echo ' Use the (integer) operator to convert the variable $num type: '. (integer) $num ;
  4. Echo '

    ';

  5. Echo ' The value of the output variable $num: '. $num ;
  6. Echo '

    ';

  7. Echo ' Use the Settype function to convert a variable $num type result: '. Settype ($num,' integer ' );
  8. Echo '

    ';

  9. Echo ' The value of the output variable $num: '. $num ;
  10. Echo '

    ';

  11. ?>

the Settype () function in which the specified variable can be converted to the specified data type.

There are some functions in PHP that can be used to detect whether a variable is a specified type, such as Is_bool () is to detect whether it is a Boolean type, is_string () to detect whether it is a string type, and so on.

1.2 Defining constants

Constants can be understood as values unchanged, constant values are defined, can not be changed anywhere else in the script, the syntax is: define (constant_name,value,case_sensitive), three parameters are constant name (required), the value of the constant (required), is case sensitive (optional). There are two ways to get a constant: the first is to use the variable name directly, and the second is to get it through the constant () function. To determine if a constant is already defined, you can use the defined (Stringname) function to return true successfully, otherwise false.

[PHP] View plaincopy

    1. !--? php
    2. //Defines constant: define (), gets the value of the constant: constant (), determines whether the constant is defined: defined ()
    3. define ( ' Message ' , ' constant value ' ');
    4. echo Message. '
      ' ;
    5. define ( ' count1 ' , ' constant value 2 ' );
    6. echo count1;
    7. $name = ' count1 ' ;
    8. echo constant ( $name ). '
      ' ; //This constant of the count of the output is actually
    9. echo defined ( ' Message ' ). '
      ' ;
    10. ?>

[PHP] View plaincopy

    1. //Pre-defined constants
    2. Echo ' current file path: '. __file__ . '
      ';
    3. Echo ' Current number of rows: '. __line__ . '
      '; //82
    4. Echo ' current PHP version information: '. Php_version. '
      ';
    5. Echo ' current operating system: '. Php_os;
    6. Echo '

      ';

    7. ?>

1.3 Defining variables and assigning values to variables

Unlike many languages, it is not necessary to declare a variable before using it in PHP (PHP4.0 needs to declare a variable before), just assign a value to the variable.

[PHP] View plaincopy

  1. //Assign value of variable
  2. ///First: variable direct assignment, e.g. $e= ' SS ';
  3. ///second: the assignment between variables, the assignment between the variables refers to the assignment of the two variables using their own memory, gu no interference;
  4. ///Third: Reference assignment, the concept of reference is that when you change the value of one of the variables, the other changes as well, using the & symbol to represent the reference.
  5. assignment between//variables
  6. $string 1 = ' SPCN ' ;
  7. $string 2 = $string 1 ;
  8. $string 1 = ' Zhuangjia ' ;
  9. Echo The value of the ' variable string2 is: '. $string 2 . '
    ';
  10. Echo The value of the ' variable string1 is: '. $string 1 . '
    ';
  11. Echo '

    ';

  12. //reference Assignment
  13. $i = ' SPCN ' ;
  14. $j = & $i ;
  15. $i = "Hello, $i" ;
  16. Echo The value of ' J ' is: '. $j . '
    ';
  17. Echo The value of ' I ' is: '. $i . '
    ';
  18. ?>

[PHP] View plaincopy

  1. //global variable can be accessed anywhere in the program, but is not available in user-defined functions. Use the Global keyword statement if you want to use it.
  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. //variable variable
  12. $change _name = ' Trans ' ;
  13. $trans = ' You're met ' ;
  14. Echo $change _name. '
    ';
  15. Echo $ $change _name ; //Implementation principle similar to escape character, $change _name represents trans, and then adds a $ symbol, that is, the output is $trans
  16. Echo '

    ';

  17. ?>


1.4 operator

Operators, like other languages, include +-*/() | ^ ~ <<>< span=""><>

[PHP] View plaincopy

    1. !--? php
    2. //@ operator: Masking error messages
    3. $err = @ (5/0);
    4. echo $err . '
      ' ;
    5. ?>
    6. !--? php
    7. //ternary operator
    8. $value = +;
    9. $res = ( $value = =)? ' ternary operation ' : ' No change value ' ;
    10. echo ' haha ' . $res . '
      ' ;
    11. echo '

      ' ;

    12. ?>

1.5 functions

function is to write some of the reused functionality in a separate code fast, call it separately when needed, create the function syntax: Functions fun_name ($str 1, $str 2 ... $strn) {}, and then call Fun_name (XXX).

[PHP] View plaincopy

  1. //Simple functions
  2. function CountNumber ($num 1,$num 2) {
  3. return "$num 1 * $num 2 =". $num 1 * $num 2 . '
    ';
  4. }
  5. Echo CountNumber (10,10);
  6. Echo '

    ';

  7. pass parameters between//functions
  8. //By Value delivery method
  9. function Example ($m) {
  10. $m = $m * 5 + ten;
  11. Echo ' The value of $m within the function is: '. $m . '
    ';
  12. }
  13. $mm = 1;
  14. Example ($mm);
  15. Echo ' The value of $m outside the function is: '. $mm . '
    ';
  16. Echo '

    ';

  17. //By reference delivery method
  18. function example1 (&$m) {
  19. $m = $m * 5 + ten;
  20. Echo ' The value of $mmm within the function is: '. $m . '
    ';
  21. }
  22. $mmm = 1;
  23. Example1 ($mmm);
  24. Echo ' The value of $mmm outside the function is: '. $mmm . '
    ';
  25. Echo '

    ';

  26. //optional parameters, where $tax is an optional parameter, fill the line, do not fill the line
  27. function values ($price,$tax="") {
  28. $price += $tax ;
  29. Echo ' price is: '. $price . '
    ';
  30. }
  31. Values (100,20);
  32. VALUES (100);
  33. Echo '

    ';

  34. //reference to the function itself
  35. function &rexample2 ($tmp=0) {
  36. $tmp = $tmp . ' 123456 ' ;
  37. return $tmp;
  38. }
  39. $STR 5 = &example2 ("Kankan");
  40. Echo $str 5. '

    ';

  41. ?>


The above introduces the small Fat Learning PHP summary 2-----PHP b variables and assignments, including the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.