Home >Backend Development >PHP Tutorial >Chapter 4_PHP Basic Syntax_PHP Tutorial
1. The first PHP program
<span 1</span> <span //</span><span 弱爆了有木有</span> <span 2</span> <?<span php </span><span 3</span> <span echo</span> "<span Hello PHP!'; </span><span 4</span> ?>
2.PHP supports a total of 8 basic data types, including 4 scalar types: boolean (Boolean type), integer (integer type), float (floating point type, also called double), string (string), 2 composite types: array (array), object (object), 2 special types: resource (resource), NULL (empty type)
3. The function var_dump can be used to print relevant information about variables.
<span 1</span> <?<span php </span><span 2</span> <span var_dump</span>((bool) <span false</span><span ); </span><span 3</span> <span var_dump</span>((bool) 0<span ); </span><span 4</span> <span var_dump</span>((bool) 0.0<span ); </span><span 5</span> <span var_dump</span>((bool) ''<span ); </span><span 6</span> <span var_dump</span>((bool) "0"<span ); </span><span 7</span> <span var_dump</span>((bool) <span array</span><span ()); </span><span 8</span> <span var_dump</span>((bool) <span NULL</span><span ); </span><span 9</span> ?>
4. About integers.
In 32-bit operating systems, the valid range of integers is -2147483648~+2147483647. If it exceeds this range, it will be interpreted as a floating point number (float) by the PHP engine.
Add 0 in octal and 0x in hexadecimal (the number 0 instead of the letter O)
<span 1</span> <?<span php </span><span 2</span> <span $dec_int</span>=63<span ; </span><span 3</span> <span var_dump</span>(<span $dec_int</span><span ); </span><span 4</span> <span 5</span> <span $oct_int</span>=077<span ; </span><span 6</span> <span var_dump</span>(<span $oct_int</span><span ); </span><span 7</span> <span 8</span> <span $hex_int</span>=0x3f<span ; </span><span 9</span> <span var_dump</span>(<span $hex_int</span><span ); </span><span 10</span> ?>
5. String type
There are three ways to define strings, namely single quotes ('), double quotes (") and delimiters (<<<). The difference between the first two is that if the string contains variables Name, the variable name in double quotes will be replaced by the actual value, and the variable name in single quotes will be output directly as ordinary characters
.<span 1</span> <?<span php </span><span 2</span> <span $name</span>='麦兜'<span ; </span><span 3</span> <span $age</span>=19<span ; </span><span 4</span> <span echo</span> '本人大名$name,年方$age'<span ; </span><span 5</span> <span echo</span> '<p>'<span ; </span><span 6</span> <span echo</span> "本人大名<span $name</span>,年方<span $age</span>"<span ; </span><span 7</span> ?>
The use of delimiters is as follows:
<span 1</span> <?<span php </span><span 2</span> <span echo</span> <<<<span MY_STR </span><span 3</span> <span 我是PHP初学者 </span><span 4</span> <span MY_STR; //最后结尾的标识符前面不能有空格或者缩进,必须顶咯 </span><span 5</span> ?>
6. Object type
<span 1</span> <?<span php </span><span 2</span> <span class</span><span Person { </span><span 3</span> <span public</span> <span $name</span><span ; </span><span 4</span> <span private</span> <span $age</span><span ; </span><span 5</span> <span 6</span> <span function</span> Person(<span $name</span>,<span $age</span><span ){ </span><span 7</span> <span $this</span>->name=<span $name</span><span ; </span><span 8</span> <span $this</span>->age=<span $age</span><span ; </span><span 9</span> <span } </span><span 10</span> <span 11</span> <span function</span><span getInfo(){ </span><span 12</span> <span echo</span> '我叫'.<span $this</span>->name.'<br>我今年'.<span $this</span>->age.'岁了'<span ; </span><span 13</span> <span } </span><span 14</span> <span } </span><span 15</span> <span $boy</span> = <span new</span> Person("kimi",5<span ); </span><span 16</span> <span $boy</span>-><span getInfo(); </span><span 17</span> ?>
7. Type conversion
print_r() Displays easy-to-understand information about a variable. If given is string, integer or float, the variable will be printed value itself. If array is given, the keys and elements
will be displayed in a certain format.
<span 1</span> <?<span php </span><span 2</span> <span $temp_var</span>=3.141592653<span ; </span><span 3</span> <span $var1</span>=(<span string</span>)<span $temp_var</span><span ; </span><span 4</span> <span echo</span> "<p><span $var1</span>"<span ; </span><span 5</span> <span 6</span> <span $temp_var</span>='Kimi'<span ; </span><span 7</span> <span $var2</span>=(<span array</span>)<span $temp_var</span><span ; </span><span 8</span> <span echo</span> '<p>'<span ; </span><span 9</span> <span print_r</span>(<span $var2</span><span ); </span><span 10</span> <span 11</span> <span $temp_var</span>='Cindy'<span ; </span><span 12</span> <span $var3</span>=(<span unset</span>)<span $temp_var</span><span ; </span><span 13</span> <span echo</span> '<p>'<span ; </span><span 14</span> <span var_dump</span>(<span $var3</span><span ); </span><span 15</span> ?>