Php faq set correction edition (21 Q &)

Source: Internet
Author: User
Tags php error
The php faq set fixes some details of online communication errors ;. 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:

The code is as follows:


For ($ I = 0; I I <100; $ I ++ ){
Echo $ I;
}


7. how can I get the variables in php in javascript?
A: The example is as follows:

The code is as follows:


$ Username = $ _ POST ["username"];
?>
Script
Var username =" ";
Script


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

The code is as follows:


/*
: 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:

The code is as follows:


Function getRssContent ($ url ){
$ Handle = fopen ($ url, "rb ");
$ Contents = "";
$ Count = 0;
Do {
$ Data = fread ($ handle, 1000000 );
$ Count ++;
If (strlen ($ data) = 0 ){
Break;
}
$ 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

The code is as follows:


$ 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:

The code is 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:

The code is 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.
15. if you use the HTML online editor in your project, FCKEditor may be a good choice. FCKEditor can download it online by yourself. There are many download places. I will introduce the calling method:
First, put the FCKEditor directory under the root directory of the website. Suppose you want to reference FCKEditor in edit. php under the/modules/cms/directory of the website root directory. the code is as follows:

The code is as follows:


$ SBasePath = ".../fckeditor/"; // fckeditor is the directory of FCKEditor.
$ OFCKeditor = new FCKeditor ('content ');
$ OFCKeditor-> BasePath = $ sBasePath;
$ OFCKeditor-> Value = "";
$ OFCKeditor-> Width = "666px ";
$ OFCKeditor-> Height = "300px"
?>


Create ();?>



16. how to store data in sessions?
First, you must start the session mechanism. in addition to making certain settings for apache, you must first call the session_start () method on the php page of the session to use the session on this page. The method for storing data in a session is as follows:

The code is as follows:


Session_start ();
$ Username = "admin ";
Session_register ("username ");
?>
[Code]
On other pages, you want to obtain the username in the session as follows:
[Code]
$ Username = $ _ SESSION ["username"];
?>


Similarly, to determine whether the currently accessed user has logged on, you can also use the above method: After the user logs on, register the user name in the session, add judgment to the php page that requires session control, for example:

The code is as follows:


If (! Session_is_registered ("username ")){
Header ("Location: login. php ");
}


The above is achieved by checking whether the username variable is registered in the session.
17. in PHP, how do I define the attributes and operations of classes and their members and call them?
For example, you can tell the above question:
Defines a string processing tool class: StringUtils

The code is as follows:


Class StringUtils {
Function StringUtils (){
}
Function getLength ($ str ){
Return strlen ($ str );
}
}
?>


On the php page, the call method is as follows:

The code is as follows:


Include 'classes/com/xxx/StringUtils. php ';
$ Length = StringUtils: getLength ("abcde ");
// Or
$ Instance = new StringUtils;
$ Length = $ instance-> getLength ("abcde ");
?>


For a class method, there are generally two call methods, one is to call as a static method, through: connector, one is to call as an instance method, use a connector. Although a call can be called in two ways, the actual method of a class is a static method, which is already logically defined. Therefore, each method is often called in only some way, for example, methods in a service class should basically be instance methods, while methods in a tool class are basically class methods or static methods, for example:

The code is as follows:


Class UserService {
Var $ dbhost = "";
Var $ dbuser = "";
Var $ dbpw = "";
Var $ dbname = "";
Function UserService (){
}
Function login ($ username, $ password ){
$ Link = mysql_connect ($ this-> dbhost, $ this-> dbuser, $ this-> dbpw) or die ("cocould not connect:". mysql_error ());
Mysql_select_db ($ this-> dbname );
$ Rs = mysql_query ("select count (*) as value from cieqas_users where userid = '". $ username. "' and password = '". $ password ."'");
$ Row = mysql_fetch_object ($ rs );
$ Value = $ row-> value;
Mysql_close ($ link );
Settype ($ value, "integer ");
If ($ value <= 0 ){
Return false;
}
Return true;
}
?>


In addition, it is meaningful to call $ this in the instance method.
18. how do I set the type of a variable?
PHP can be regarded as a type of weak language and does not need to define variables forcibly. for example:
$ Username = "admin ";
$ Length = 0;
$ Obj = new MyClass;
In many cases, how do I convert a string variable to an int variable or vice versa? You can use the settype method to specify the variable type. the signature is as follows:
Settype (var, type)
The types of values include boolean (bool), integer (int), float, string, array, object, and null.
For example:
$ State = "0 ";
Settype ($ state, "int ");
If ($ state = 0 ){
...
}
19. how do I sort an array in reverse order?
Implemented using the array_reverse method, for example:

The code is as follows:


$ Arr = array ();
$ Arr [0] = 1;
$ Arr [1] = 2;
$ Arr2 = array_reverse ($ arr );


20. how can I display a time correctly in PHP?
In php, the time () method is used to return the number of seconds from the Unix New Age (January 1, 1970 00:00:00 Greenwich Mean time) to the current time. how can we correctly display the time as the correct local time, in many cases, we use the setLocale method in php to specify the current region, but we often cannot get the correct time to introduce other solutions, it is solved through the combination of Javascript and php, for example:

The code is as follows:


Var time =" ";
Var time = parseInt (time );
Var date = new Date (time * 1000 );
Var pattern = "yyyy-MM-dd hh: mm: ss ";
Var df = new SimpleDateFormat ();
Var str = df. format (date );
Document. write (str );


Therefore, you can pass the time () value in php to Javascript and use it as a parameter of the Date object. then, you can use the Javascript open-source class library JsJava for processing.
21. PHP is a very popular language today. so far, a large number of function libraries have been formed, for example, it processes strings, mathematics, XML, files, SOAP, and networks. However, it is still lacking in object orientation, however, it does not mean that object orientation is a language. However, in actual website or project development, a large number of function libraries sometimes do not feel special convenience, in particular, in some cases, we need to abstract the architecture layers and various objects for the business. it is more appropriate to define a set of appropriate business class libraries. after all, when we face development at a higher business layer, we need to encapsulate a higher level, so classes and objects will be put on the agenda at this time. However, it is very convenient and powerful to use various php functions, this makes me complain about object-oriented languages like Java, and all logic should be implemented by using a lot of classes. it seems that there is a need to learn from each other rather than attack each other, solving problems and promoting the development of the industry and society is the most fundamental.

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: info-contact@alibabacloud.com 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.