php variable usage and declaration

1. Languages ​​such as HTML/CSS/JavaScript can be written in the php file, but the code between the start editor <?php and the end tag?> is parsed by the php application server, and the parsed code is sent to the client browser , unexpected codes in this tag are sent directly to the client.

2. There are 5 ways to write php tags. Normally, the first one is used. If the following code is php code, it is recommended to add ?> end tag

<?php
    echo "111111111<br>";
    $hello = "2222";
?>

<? = $hello?>

<?
    echo "33333<br>";
?

<%
    echo "444444<br>";
%>

<script language = "php">
    echo "55555<br>";
</script>

Three, variable declaration

  • PHP is a weakly typed language, the type of the variable is determined by the stored value
  • $variable name = variable value;
  • Isset(); Determine if a variable exists
  • Unset(); release a variable

Fourth, the assignment of variables

$one =10;
$two = &$one;//Modify one of the parameters and the other parameter also changes, they specify the same space
$three = $one;//Modifying the parameter value will not affect the other parameter

Five, the 8 types of variables

  • 4 scalars:
  1. Integer: int integer
  2. Boolean: bool boolean
  3. Floating point: float/double/real
  4. string: string
  • 2 compound types
  1. array: array
  2. object: object
  • 2 special types
  1. Resource Type: resourcee
  2. Empty type: null

Var_dump(variable or value);//You can view both the type of variable or value and the data

6. There are many ways to declare a string

  1. Both single and double quotes can declare strings
  2. declares that the string has no length limit
  3. In the double-quote character, you can either parse the variable directly or use the escape character directly
  4. In single-quoted strings, variables cannot be parsed, nor can escape characters be used (the single quote itself can be escaped, or the character "\" can be escaped)
  5. Double quotes cannot be used within double quotes, and single quotes cannot be used within single quotes
  6. Better to use single quotes
  7. The delimiter symbol declares a string, a large number of strings, hello is a custom string, there cannot be any spaces after the starting hello, and there cannot be any characters before the ending hello. Delimiter escapes, variables, double quotes, and single quotes can all be used
    $str=<<<hello
    	ffasf"cds'\nshide{$int}fdsfsa
    hello;

7. Conversion between data types

  1. Coercion: setType(variable, type);//This function will change the type of the original variable
  2. $a = (variable type) variable value or value; // will not change the type of the original variable
  3. $variable=intval(variable value or value); $variable=floatval(variable value or value); $variable=stringval(variable value or value);//This is the specific function, the function is the same as 2
  4. Custom conversions: Variables are automatically converted according to the runtime environment

Note: Integer occupies 4 bytes in memory, 2.147e9. A floating point type occupies 8 bytes in memory.

8. Common functions related to variables and types

isset();//Determine whether the variable exists, if the value is null, it means it does not exist
empty();//Check if the variable is empty
unset();//Release the variable
setType();//Set the data type of the variable
getType();//Get the data type of the variable
var_dump();//It is also the data type of the variable, and the variable value will be output

Variable test function:

is_bool();
is_int();
is_integer();
is_scalar();
is_numberic();
is_callable();
........

Nine, constant declarations and variables

  1. A constant is an identifier for a simple value
  2. After the constant is defined, its value cannot be changed, nor can it be canceled by unset()
  3. Constants can be defined and accessed anywhere, regardless of variable scope rules
  4. Constants use define("variable name", "value");
  5. Constant names are declared and used without using $
  6. Always use uppercase for constant names
  7. The value of a constant can only be of variable type (int, float, bool, string)
  8. defined("constant");//Determine whether the constant exists

Predefined constants and magic constants, please see api for details

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325518305&siteId=291194637