Introduction of PHP
PHP (full name: PHP: Hypertext Preprocessor) is an open source general-purpose computer scripting language, especially suitable for web development and embedded in HTML. The syntax of PHP draws on the characteristics of popular computer languages such as C language, Java and Perl, and is easy for general programmers to learn. The main goal of PHP is to allow web developers to quickly write dynamic pages, but PHP is also used in many other areas.
The PHP file extension is .php
, E.g index.php
. As mentioned above, PHP
can directly embed HTML
.
Usage
When the browser reads <?php
from top to bottom, it executes PHP
and ends with ?>
. Such as:
Precautions
In PHP
, there is no other HTML
after the PHP
directive is completed. It is recommended not to end with ?>
. It will cause trouble if it is closed.
PHP comments
Almost most web programming languages can comment, and the single-line way of PHP
is //
and the multi-line way is /* Some text here */
.
Echo
The simplest syntax in PHP
is Echo
. The content of Echo
must be enclosed in double quotes ""
or single quotes ''
. Each syntax must end with a semicolon ;
.
Content in Echo
can also use HTML
syntax. For example, I used <br/>
here, so that the line can be broken.
Variable
We can assign a value to a variable to make it easier for us to write PHP
, similar to Javascript
. We will use the Dollar sign $
.
There are some rules to follow when we name variables.
Example | Description | Rules |
---|---|---|
$value | Lowercase | ✅ |
$Value | Uppercase | ✅ |
$_value | Underscore | ✅ |
$1value | Start from number | ❌ |
There is a difference between uppercase
and lowercase
in variable names. Uppercase variables are not equal to lowercase variables.
Variable Naming Tips
Camel case
When a variable name and a function name are linked together by two or more words to form a unique identification word, the use of "camel case" to represent, can increase the readability of variables and functions. Because we can't have spaces
in variables.
Snake case
Snake case (like snake_case) refers to a writing style in which every space is replaced with an underscore ( _ ) character and the first letter of every word is written in lowercase.
String operator
There are two string operators. The first is the concatenation operator ("."), which returns the concatenated string of its left and right arguments. The second is the concatenation assignment operator (".="), which appends the right-hand argument to the left-hand argument.