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:
CopyCode The Code is as follows: 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:Copy codeThe Code is as follows: <? PHP
$ Username = $ _ post ["username"];
?>
<SCRIPT>
VaR username = "<? PHP echo $ username?> ";
</SCRIPT>
8. How do I delete an object?
A: Use the unlink (filename) function. Of course Program You 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. phpCopy code The Code is as follows: <? PHP
/*
: User1: password1: user2: password2: user3: password3 :::
*/
?>
12. How do I transcode a string?
use the PHP iconv function with the signature:
$ STR = iconv (fromencode, toencode, STR);
example:
$ STR = "php string transcoding";
$ STR = iconv ("UTF-8", "GBK", $ Str ); // converting a string from UTF-8 to GBK
transcoding is an important issue. For example, the RSS feeds provided by many blogs are returned by UTF-8, therefore, it must be converted to display the data correctly.
13. How do I read the HTML content of a webpage?
the concept of file in PHP is similar to that of file stream in Java. The input stream accepted by many File Reading functions is not just a local file system, it can also be a network file. one of the following methods is described: copy Code 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;
}< br> $ contents. = $ data;
}while (true);
fclose ($ handle);
return $ contents;
}
14. How does one operate MySQL Databases in PHP?
to help beginners get started with MySQL, I will introduce some common operations:
1) database connection and shutdown copy Code 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 close operations are no longer 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:Copy codeThe 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:Copy codeThe 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 on its own. There are many places to download it, I will introduce the call method:
first, put the FCKeditor directory under the root directory of the website. Assume that you are editing under the/modules/CMS/directory of the root directory of the website. to reference FCKeditor in PHP, the Code is as follows: copy Code the code is as follows: $ sbasepath = ".. /.. /FCKeditor/"; // FCKeditor is the FCKeditor directory
$ 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:Copy codeThe Code is as follows: <? PHP
Session_start ();
$ Username = "admin ";
Session_register ("username ");
?>
[Code]
On other pages, you want to obtain the username in the session as follows:
[Code]
<? PHP
$ 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:Copy codeThe 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: stringutilsCopy codeThe Code is as follows: <? PHP
Class stringutils {
Function stringutils (){
}
Function getlength ($ Str ){
Return strlen ($ Str );
}
}
?>
On the PHP page, the call method is as follows:Copy codeThe Code is as follows: <? PHP
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: copy Code the code is as follows: class userservice {
var $ dbhost = "";
var $ dbuser = "";
var $ dbpw = "";
var $ dbname = "";
function userservice () {
}< br> 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;
}< br> return true;
}< BR >?>
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 weak type language and does not need to be forced to define variables. For example:
$ username = "admin ";
$ length = 0;
$ OBJ = new myclass;
in many cases, you need to convert a string variable to an int variable, or vice versa, what should I do? The settype method can be used to specify the variable type. The signature is as follows:
settype (VAR, type)
the type value is Boolean (bool), INTEGER (INT), float, String, array, object, null
example:
$ state = "0";
settype ($ state, "int");
if ($ state = 0) {
...
}< br> 19. How do I sort an array in reverse order?
implemented through the array_reverse method, for example, copy Code the code is as follows: $ arr = array ();
$ arr [0] = 1;
$ arr [1] = 2;
$ arr2 = array_reverse ($ ARR);
20. In PHP, how do I display a time correctly?
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 by combining JavaScript with PHP, for example: copy Code 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. A large number of function libraries have been formed so far, 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.