PHP Beginner FAQ Set Revision (21 quiz) _php tutorial

Source: Internet
Author: User
Tags php error
1, how do I connect a string of two strings?
A: Connecting two strings in PHP can be used directly with "." Operation symbols, such as $newstr= "Zhang". " San ", which is used in Java with the" + "operation symbol, do not confuse.
2, how to calculate the length of a string?
Answer: $STR = "Test"; $length =strlen ($STR), even with the strlen (str) function.
3, how do I split a string by a separator?
A: Using the explode (DELIM,STR) function, such as $arr=explode ("::", "A::BDF::d fsdf"), the function returns an array. You can use the Split function of a string object in Java.
4, how do I get the parameter values in the HTTP request?
A: If for GET request, use $_get[paramname], if for POST request, use $_post[paramname], for example: $email =$_post["Usermail"].
Can I use classes like Java in 5,php?
A: Yes, but the mechanism and specific usage may not be the same.
6, can you give an example of using a for loop?
For:
Copy CodeThe code is as follows:
for ($i =0; $i <100; $i + +) {
echo $i;
}

7, how do I get the variables in PHP in JavaScript?
A: The examples are as follows:
Copy CodeThe code is as follows:
$username =$_post["username"];
?>


8, how do I delete a file?
A: Using the unlink (filename) function, of course, the program must have permission to delete the file, we use the PHP virtual space may be a
Some files are restricted, so permission errors may occur.
9, I defined a class user, declared a method of the class GetName (), why I use $user=new user; $name = $user. GetName () will be an error?
A: Note that the reference to a class member in PHP, the above reference should be $name= $user->getname (), that is, the use of the symbol, instead of
The "." Used in Java No.
10, I applied for a PHP virtual space without MySQL support, how can I access the application data ah?
A: Access to data does not have to use the database, the use of the file system is also good, and even if the use of databases, do not have to use like MySQL
, Oracle, and so on, you can also use some text databases, such as txtSQL, so you don't have to hire a higher-cost MySQL
Database space.
11, I applied for a PHP space without a database, my current application data is in the file, but there is a security issue, that is
Visitors can see the contents of these files by URL, how do I protect the contents of these files?
A: There are three ways to suggest:
1) If the PHP space you rented allows you to set HTTP access to the directory, then set it up.
2) The contents of the file can be encrypted, and even if it is downloaded, it does not have much value.
3) You can change the suffix of these files to. php, which is the use of PHP files to store application information so that visitors cannot access it via HTTP
The actual contents of these files, of course, the contents of these files must be the correct PHP syntax, and the content to use the PHP syntax of the hidden syntax
Hiding information, such as a file that holds account information, is as follows:
users.php
Copy CodeThe code is as follows:
/*
::: User1:password1::user2:password2::user3:password3:::
*/
?>

12, how to transcode a string?
Using PHP's Iconv function, the signature is:
$str =iconv (FROMENCODE,TOENCODE,STR);
For example:
$str = "php string transcoding";
$str =iconv ("Utf-8", "GBK", $str);//convert string from Utf-8 format to GBK format
Transcoding is an important issue, for example, many blogs now offer RSS feeds that are returned by UTF-8 and therefore need to be converted to be displayed correctly.
13, how to read the HTML content of a webpage?
The concept of files in PHP and the concept of file flow in Java is similar, many file read function, its accepted input stream is not only the local file system, can also be a network file, one of the following is described below:
Copy CodeThe 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;
}

How to operate MySQL database in 14,php?
In order to facilitate beginners to get started with MySQL operation, I introduce some common operations:
1) database connection and shutdown
Copy CodeThe code is as follows:
$dbhost = "";
$dbuser = "";
$DBPW = "";
$dbname = "";
$link = mysql_connect ($dbhost, $dbuser, $DBPW) or Die ("Could Not Connect:". Mysql_error ());
mysql_select_db ($dbname);
...//Here is a database specific operation, the following example is no longer write database connection and shutdown operation
Mysql_close ($link);

2) inserting new data into the table
mysql_query ("INSERT INTO mytable (id,name) VALUES ('". $id. "', '". $name. "')");
Above is to insert a data into the ID and name field of the MyTable table.
3) querying 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, mysql3.22 the following versions are not supported, so many times PHP will not get results when writing complex SQL, this is not a PHP error, but the version of MySQL is lower.
6) For the result set returned by SELECT, you can do the following:
For returning a result, you can do the following:
Copy CodeThe code is as follows:
$row =mysql_fetch_object ($RS);
$id = $row->id;//id is the field name, or the alias of the field, as
$title = $row->title;
$asker = $row->asker;

For returning multiple results, you can do the following:
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 ways to make the returned result an array, access can also be accessed according to the location of the field index value, this can query the relevant manual, not introduced.
15, if your project using HTML online editor, then FCKeditor may be a good choice, FCKeditor can go online to download, download a lot of places, I introduce the way to call:
First put the FCKeditor directory in the root directory of the Web site, assuming you are in the site root directory of the/modules/cms/directory edit.php to refer to FCKeditor, the specific code is as follows:
Copy CodeThe 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 session?
First of all, to start the session mechanism, in addition to the Apache itself to make certain settings, in the use of the session of the PHP page, to first call the Session_Start () method, indicating that the session is used on this page. Here's how to store data in the session as follows:
Copy CodeThe code is as follows:
Session_Start ();
$username = "admin";
Session_register ("username");
?>
[Code]
Then on the other page, want to get the user name in session, as follows:
[Code]
$username =$_session["username"];
?>

In the same way, to determine whether the current user is logged in or not, you can do this by: when the user logs in, register the user name in the session, add judgment in 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 judging whether the username variable is registered in the session.
How do I define a class and its member properties and operations in 17,php, and how do I invoke it?
To give an example directly, it should be possible to illustrate the above problem:
Defines a string processing tool class: StringUtils
Copy CodeThe code is as follows:
Class stringutils{
function StringUtils () {
}
function GetLength ($STR) {
return strlen ($STR);
}
}
?>

In the PHP page the method is called:
Copy CodeThe code is as follows:
Include ' classes/com/xxx/stringutils.php ';
$length =stringutils::getlength ("ABCDE");
Or
$instance =new StringUtils;
$length = $instance->getlength ("ABCDE");
?>

For a method of a class, there are generally two methods of invocation, one is called as a static method, through: A connector, a call as an instance method, through a connector. Although the invocation can be invoked in two ways, the actual method of a class is static, often logically defined, so each method is often used only in some way, such as a service class method, basically should be an instance method, and a tool class method, are basically class methods or static methods, such as:
Copy CodeThe 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 ("Could 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, the invocation of $this in an instance method has practical significance.
18, how do I set the type of a variable?
PHP can be counted as one of the weakly typed languages and does not require coercion of type definitions for variables, such as:
$username = "admin";
$length = 0;
$obj =new MyClass;
Many times, you need to convert a string variable to an int variable, or vice versa, how do you do it? You can actually use the Settype method, which can specify the type of the variable, signed as follows:
Settype (Var,type)
Where the type has a value of Boolean (bool), integer (int), float, string, array, object, NULL
For example:
$state = "0";
Settype ($state, "int");
if ($state ==0) {
...
}
19, how do you reverse-arrange an array?
Implemented by the Array_reverse method, for example:
Copy CodeThe code is as follows:
$arr =array ();
$arr [0]=1;
$arr [1]=2;
$arr 2=array_reverse ($arr);

20, how to display a time correctly in PHP?
In PHP, the time () method is used to return the number of seconds since the beginning of the Unix era (00:00:00 GMT, January 1, 1970) to the current time, so how to correctly display the timing as the local correct time, many times we use the SetLocale method in PHP, Specify the current region, but we often do not get the right time, to introduce you to another solution, is to use JavaScript and PHP to solve, for example:
Copy CodeThe 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 value of time () in PHP to JavaScript, as the parameter of the Date object, and then through the JavaScript open source class Library Jsjava processing.
21,php is now a very popular language, so far has formed a large number of function libraries, such as processing strings, mathematical, XML, file, SOAP, network, and so on, but in terms of object-oriented, there is a certain degree of lack, However, it is not to say that object-oriented can be regarded as a language of how, but in the actual site or project development, just a large number of function library sometimes feel not special convenience, especially sometimes, the business needs us to abstract the structure of the hierarchy and the various objects, This is a good time to define a suitable business class library, after all, when we face the higher business level of development, we need to encapsulate a higher level, then this time the class and objects on the agenda, but the current use of PHP's various functions, the feeling is very convenient, and very powerful, That makes me a little bit more like the object-oriented language such as Java, what logic to do with a large number of classes to achieve, it seems that language needs to learn from each other, rather than attack each other, solve the problem to promote the development of industry and society is the most fundamental.

http://www.bkjia.com/PHPjc/321467.html www.bkjia.com true http://www.bkjia.com/PHPjc/321467.html techarticle 1, how do I connect a string of two strings? A: Connecting two strings in PHP can be used directly with "." Operation symbols, such as $newstr= "Zhang". " San ", in Java is the use of the" + "action symbol, ...

  • 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.