Home >php教程 >PHP开发 >Excerpted from the PHP Manual [1] – Basics to note

Excerpted from the PHP Manual [1] – Basics to note

黄舟
黄舟Original
2016-12-22 10:08:571227browse

Tianya recently used his free time to read the manual completely, and will post some things on the blog that he thinks we can easily overlook. Not much to say, the first article.

Note: About line breaks Although the actual significance of line breaks in HTML is not great, proper use of line breaks can make HTML code readable and beautiful. PHP will automatically remove a newline after the end character ?> when outputting. This function is mainly designed for embedding multiple pieces of PHP code in a page or containing PHP files without substantial output. At the same time, it also caused some confusion. If a newline is output after the PHP terminator ?>, you can add a space after it, or add a newline in the last echo/print statement.

Note: The end tag of the PHP code segment at the end of the file is not required. In some cases, it is better to omit it when using include() or require(), so that unexpected white spaces will not appear at the end of the file, and it will still be there afterwards. Response headers can be output. It's also convenient when using output buffering, so you don't see the unwanted white spaces generated by include files.

Note: Unlike the other two syntaxes, variables and escape sequences appearing in single-quoted strings will not be replaced by the value of the variable.
【Tianya Note】In other words, variables inside single quotes will not be parsed and will be output as strings.

Characters in a string can be accessed and modified by specifying the zero-based offset of the desired character using curly braces after the string.

$str = 'Hello World!';

echo $str{1}; // Output e

?>

Note: The unset() function allows you to unset a key in an array. Be aware that the array will not be reindexed.

You should always put quotes around array indices expressed as strings. For example, use $foo['bar'] instead of $foo[bar]. But why is $foo[bar] wrong? You may have seen the following syntax in old scripts:




$foo[bar] = 'enemy';
echo $foo[bar];
?>


This is how it works Wrong, but works fine. So why is it wrong? The reason is that there is an undefined constant (bar) in this code instead of a string ('bar' - note the quotes), and PHP may define this constant later, unfortunately you have the same name in your code. It works because PHP automatically converts a bare string (a string without quotes and not corresponding to any known symbol) into a normal string whose value is that bare string. For example, if there is no constant defined as bar, PHP will replace it with ‘bar’ and use that.
Note: Again, in a double-quoted string, it is legal to unquote the index so "$foo[bar]" is legal.

The allowed casts are:




(int), (integer) - converted to integer type
(bool), (boolean) - converted to Boolean type
(float), (double), (real) - Convert to floating point type
(string) - Convert to string
(array) - Convert to array
(object) - Convert to object


Note that spaces and tabs are allowed within brackets

Note : HTML forms do not pass integers, floats or booleans, they only pass strings. To check whether a string is a number, you can use the is_numeric() function.
Note: When the variable $x is not defined, usage such as if ($x) will result in an E_NOTICE level error. Therefore, you can consider using the empty() or isset() function to initialize variables.

Note: Although ! has higher precedence than =, PHP still allows expressions like the following: if (!$a = foo()), in this case the output of foo() is assigned to $a.

The above is excerpted from the PHP manual [1] - the basic knowledge that needs to be paid attention to. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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