Php faq set for Beginners (21 Q &)

Source: Internet
Author: User
1. How to connect two strings? A: to connect two strings in php, you can directly use the. Operator symbol, for example, $ newStr = Zhang. san. The + operator symbol is used in java. Do not confuse it. 2. How to calculate the length of a string? A: $ str = test; $ length = strlen ($ str); use the strlen (str) function. 3. How to score a specific score ">

1. How to connect two strings?
A: You can use ". "Operation symbol, such as $ newStr =" Zhang ". "san", which uses the "+" operator symbol in java. Do not confuse it.

2. How to calculate the length of a string?
A: $ str = "test"; $ length = strlen ($ str); use the strlen (str) function.

3. How to split a string based on a separator?
A: Use the explode (delim, str) function, for example, $ arr = explode (":", "a: bdf: dfsdf"). This function returns an array. You can use the split function of the String object in java.

4. How do I obtain the parameter values in an http request?
A: For a GET request, use $ _ GET [paramName]. For a POST request, use $ _ POST [paramName]. For example: $ email = $ _ POST ["usermail"].

5. Can classes be used in php like in Java?
A: Yes, but the mechanism and usage may be different.

6. Can I give an example of using the for loop?
A: for ($ I = 0; $ I <100; $ I ++ ){
Echo $ I;

7. How can I get the variables in php in javascript?
A: The example is as follows:
$ Username = $ _ POST ["username"];
Var username ="";

8. How do I delete an object?
A: To use the unlink (filename) function, the program must have the permission to delete the file. The php virtual space we use may be
Some files are restricted, so permission errors may occur.

9. I have defined a class User and declared a method getName () for this class. Why is an error reported when I use $ user = new User; $ name = $ user. getName?
A: pay attention to the reference method of class members in php. The above reference should be $ name = $ user-> getName (), that is, use the-> symbol instead
"." Used in Java.

10. I applied for a php virtual space without mysql support. How can I access application data?
A: You do not have to use a database to access data. It is also good to use a file system. Even if you use a database, you do not have to use a file system like mysql.
Such databases as oracle can also use some text databases, such as txtsql, so that you do not have to rent a mysql with a high cost.
Database space.

11. I applied for a php space without a database. My current application data is stored in a file, but there is a security problem, that is
Visitors can view the content of these files through URLs. How can I protect the content of these files?
A: There are three recommended methods:
1) If your rented php space allows you to set the http access permission for the directory, set it.
2) the file content can be encrypted, so even if it is downloaded, there is little value.
3) you can change the suffix of these files to. php, that is, using the php file to store application information. In this way, visitors will not be able to access
The actual content of these files, of course, the content of these files must be correct php syntax, and the content should use the hidden syntax in php syntax
Hide information. For example, a file storing account information is as follows:
Users. php
: User1: password1: user2: password2: user3: password3 :::

12. How to transcode a string?
Use the php iconv function and the signature is:
$ Str = iconv (fromEncode, toEncode, str );
For example:
$ Str = "php string transcoding ";
$ Str = iconv ("UTF-8", "gbk", $ str); // convert a string from UTF-8 to gbk.
Transcoding is a very important issue. For example, the rss feeds provided by many blogs are returned by utf-8. Therefore, transcoding is required for correct display.

13. How to read the HTML content of a webpage?
The file concept in PHP is similar to the file stream concept in Java. For many file reading functions, the input stream is not only a local file system, but also a network file, one of the following methods is described:
Function getRssContent ($ url ){
$ Handle = fopen ($ url, "rb ");
$ Contents = "";
$ Count = 0;
Do {
$ Data = fread ($ handle, 1000000 );
$ Count ++;
If (strlen ($ data) = 0 ){
$ Contents. = $ data;
} While (true );
Fclose ($ handle );
Return $ contents;

14. How does one operate mysql databases in PHP?
To help beginners get started with mysql operations, I will introduce some common operations:
1) database connection and closure
$ Dbhost = "";
$ Dbuser = "";
$ Dbpw = "";
$ Dbname = "";
$ Link = mysql_connect ($ dbhost, $ dbuser, $ dbpw) or die ("cocould not connect:". mysql_error ());
Mysql_select_db ($ dbname );
... // Here is the specific operation on the database. In the following example, the database connection and shutdown operations will not be written.
Mysql_close ($ link );
2) insert new data into the table
Mysql_query ("insert into mytable (id, name) values ('". $ id. "', '". $ name ."')");
Insert a data entry to the id and name fields of the mytable table.
3) query data from a table
$ Rs = mysql_query ("select * from mytable mt where mt. id = '001 '");
4) delete data from the table
$ Rs = mysql_query ("delete from mytable mt where mt. id = '001 '");
5) for complex queries, such as the select clause, which is not supported by MySQL or earlier versions, the results will not be obtained when php writes complex SQL statements. This is not a php error, it is because of the low version of mysql.
6) you can perform the following operations on the result set returned by the select statement:
You can return a result as follows:
$ Row = mysql_fetch_object ($ rs );
$ Id = $ row-> id; // id is the field name or alias of the field.
$ Title = $ row-> title;
$ Asker = $ row-> asker;
Multiple results can be returned as follows:
While ($ row = mysql_fetch_object ($ rs )){
$ Id = $ row-> id;
$ Title = $ row-> title;
$ Asker = $ row-> asker;
Of course, there are other ways to make the returned results an array, and the access can also be accessed based on the index value of the field location. This can be found in the relevant Manual and will not be introduced.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.