前言:最近想开始好好提升一下自己的英语水平,因为感觉熟练的英语是每一个优秀软件工程师的基本功,虽然是一枚过了六级的选手,但是感觉英文阅读和写作都还是有些压力。正巧听闻有个PHP高手说他把PHP官方手册读了不下8遍,深以为然,孰能生巧这个道理放在哪里都没有错。所以第一步我打算开始重温PHP手册,在加强自己英语水平的同时来达到温故而知新的目的。

1、PHP tags

When PHP parses a file, it look for opening and closing tags, which are <?phpand ?> which tell PHP to start and stop interpreting the code between them.
if a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.

1
2
3
4
5
<?php
echo "hello chenjiehui!";
//...more code
echo "last statement.";
// the script ends here with no PHP closing tag

2、Escaping from HTML

Everything outside of a pair of opening and closing tags is ignored by the PHP parser which allows PHP files to have mixed content. This allows PHP to be embedded in HTML documents.
But there is one exception that in the middle of a conditional statement in which case the interpreter will determine the outcome of the confitional before making a decision of what to skip over. See the next example. Using structures with conditions.

1
2
3
4
5
<?php if($expression == true) : ?>
This will show if the expression is true.
<?php else : ?>
Otherwise this will show.
<?php endif; ?>

In this example PHP will ship the blocks where the condition is not met, even though they are outside of the PHP open/close tags;
For outputting large blocks of text, dropping out of PHP parsing mode is generally more effcient than sending all of text through echo or print.

Starting with PHP 5.4, short echo tag <?= is always recongnized and vaild, regardless of the short_open_tag setting.

3、Comments

PHP support ‘C’,’C++’ and Unix shell-style(Perl style) comments. For example:

1
2
3
4
5
6
7
<?php
echo 'this is a test'; //this is a one line c++ style comment
/* This is a multi line comment
yet another line of comment */
echo 'This is yet another test';
echo 'one Final Test'; # This is a one-line shell-style comment
?>

##4、type
PHP supports eight primitive types(原始数据类型):

Four scalar types(标量类型):

  • boolean
  • integer
  • float(aka(又称作) double)
  • string

Three compound types:

  • array
  • object
  • callable

And finally two special types

  • resource
  • NULL

This manual also introduces some pseudo-types(伪类型) for readability reasons:

  • mixed
  • number
  • callback
  • array|object
  • void

And the pseudo-variable $…
The type of the variable is not usually set by programmer; rather(确切的说), it is decided at runtime by PHP depending on the context in which that variable is used.

Note: To check the type and variable of the expression, use the var_dump() function.
To get a human-readable representation of a type for debugging, use the gettype() function. To check for a certain type, do not use gettype(), but rather the is_type functions. Some examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$a_bool = TRUE ;
$a_str = "foo";
$a_str2 = 'foo';
$an_int = 12;
echo gettype($a_bool);//print out: boolean
echo gettype($a_str);//print out: string
if(is_int($an_int)){
$an_int+=4;
}
if (is_string($a_bool)) {
echo "String: $a_bool";
}
?>
>

To forcibly(强制地) convert a variable to a certain type, either cast(eg: (bool)$a_str) or use the settype() function on it.
Note that a variable may be evaluated(被评估) with diffrent values in certain situations, depending on what type it is at the time.