PHP basic syntax and basic data structure

巴扎黑
Release: 2016-11-11 10:15:24
Original
1346 people have browsed it

Php code

<?php   
$var_name = "苹果";  
$n =10;  
var_dump($var_name);var_dump($n);?>
Copy after login


Assignment statement; the "var_dump" function can display the data type of our variables.
In variables, because the space units occupied by variables are different, they are also divided into several data types. Just like the packaging bags of supermarket products, there are several different types, and different products use different packaging bags. We can get the current memory consumed by PHP by using "memory_get_usage".
In PHP, 8 primitive types are supported, including four scalar types, two composite types and two special types. PHP is a loosely typed language. There is no need to declare the data type of variables to PHP. PHP will automatically convert the variables into automatic data types, which lowers the threshold for learning PHP to a certain extent. If you have been exposed to C language or JAVA language, you will find that you need to declare the data type of the variable when declaring a variable.
What needs to be noted when using Boolean variables in php is that when we use the "echo" command to output the Boolean type, if it is "true", the output is "1", and "false" means nothing is output. We can use the "var_dump" function to get its real data type. For example:

Php code

<?php   
    $man = "男";  
 $flag = $man == "男";  
echo $flag ;  
 echo "<br />" ;  
 var_dump($flag);  
?>
Copy after login


Although the output result of using the "echo" instruction is 1, "var_dump" shows us that "$flag" is a Boolean type.
What should we do when your string contains quotation marks? There are three solutions:
The first solution: embed double quotes within single quotes;
The second solution: embed single quotes within double quotes;
The third solution: use the escape character "".
When the output statement contains a variable name:
When a variable is included in double quotes, the variable will be concatenated with the content in double quotes;
When a variable is included in single quotes, the variable will be output as a string.
For example:

Php code

<?php   
$love = "I love you!";   
$string1 = "cenzi,$love";  
$string2 = &#39;cenzi,$love&#39;;  
echo $string1;  
echo "<br />";  
echo $string2;  
?>
Copy after login


The output is:
cenzi,I love you!
cenzi,$love
What should I do when my string is very long?
We can use the Heredoc structure method to solve this problem, first using the delimiter to represent the string, then providing an identifier GOD, then the string, and finally ending the string with the provided identifier. The indicators can be defined by yourself, but they must be consistent. The ending identifier must be on a new line, and this line cannot have any other characters except "GOD" and ends with ";", neither before nor after it, including spaces, otherwise an error will occur.
The first special type—resource
Resource: Resources are created and used by specialized functions, such as opening files, data connections, and graphics canvases. We can operate on resources (create, use and release). Any resources should be released promptly when no longer needed. If we forget to release resources, the system automatically enables the garbage collection mechanism to recycle resources after the page is executed to prevent the memory from being exhausted. Suppose you find a file on the server and want to see what is written in it. PHP can do it! . Suppose there is a file on the server called "f.txt". At this time, we need to use the special data type of resource. The premise of the following test is that you have the file in the ("/data/webroot/resource/php/f.txt") path. Note: The f.txt text file must be set to UTF-8 format when saving to avoid garbled characters.

Php code

<?php   
//首先采用“fopen”函数打开文件,得到返回值的就是资源类型。  
$file_handle = fopen("/data/webroot/resource/php/f.txt","r");  
if ($file_handle){  
    //接着采用while循环一行行地读取文件,然后输出每行的文字  
    while (!feof($file_handle)) { //判断是否到最后一行  
        $line = fgets($file_handle); //读取一行文本  
        echo $line; //输出一行文本  
        echo "<br />"; //换行  
    }  
}  
fclose($file_handle);//关闭文件  
?>
Copy after login

The second special type - empty type

NULL (NULL): NULL is an empty type and is not case-sensitive. The NULL type has only one value, indicating that a variable has no value. When assigned a value of NULL, or has not been assigned a value, or is unset() (undefined), the variable is considered NULL in these three cases.


Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!