PHP Beginners FAQ Set Revision (21 Questions and answers) _php basics

Source: Internet
Author: User
Tags explode instance method strlen
1, how to connect two strings?
A: Connecting two strings in PHP can be used directly by the "." Action symbols, such as $newstr= "Zhang". SAN, in Java, is the use of the "+" action symbol, do not confuse.
2, how to calculate the length of a string?
A: $str = "Test"; $length =strlen ($STR), even with strlen (str) functions.
3, how do I split a string by one of the delimiters?
A: Use 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 you use $_get[paramname for a GET request, use $_post[paramname for POST requests, for example: $email =$_post["Usermail"].
Can I use classes like Java in 5,php?
A: Yes, but the mechanism may not be the same as the exact way it is used.
6, can you give an example of using a for loop?
Copy Code code 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 Code code as follows:

$username =$_post["username"];
var username= "<?php Echo $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 to 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 complain?
A: note the reference to class members in PHP, the above reference should be $name= $user->getname (), that is, using the-> symbol, not
The "." Used in Java Resolution
10, I applied for a PHP virtual space without MySQL support, how can I access the application data?
A: Access to data does not necessarily use the database, the use of the file system is also good, and even if the use of the database, you do not have to use like MySQL
, Oracle and so on, you can also use some text databases, such as txtSQL, so that you do not have to rent a higher cost of MySQL
Database space.
11, I applied for a database without the PHP space, my current application data is present in the file, but this will have a security problem, is
Visitors can see the contents of these files through the URL, how do I protect the contents of these files?
A: There are three different ways to propose:
1 If your leased PHP space allows you to set the HTTP access permissions for the directory, then set it up.
2 can encrypt the contents of the file, so even if downloaded, there is not much value.
3 The suffix of these files can be changed to. php, which uses PHP files to store application information, so that visitors cannot access the
The real content of these files, of course, the contents of these files must be the correct PHP syntax, and content to use the PHP syntax in the hidden syntax
Hide information, such as a file that holds account information as follows:
Copy Code code as follows:

::: User1:password1::user2:password2::user3:password3:::

12. How do I turn a string to a code?
Using PHP's Iconv function, the signature is:
For example:
$str = "php string transcoding";
$str =iconv ("Utf-8", "GBK", $str);//convert string from Utf-8 format to GBK format
Transcoding is a very important issue, for example, many of the RSS feeds currently provided by Utf-8 are returned, so they need to be transformed to display correctly.
13, how to read the HTML content of a Web page?
The concept of file in PHP and the concept of file flow in Java is similar, many files read the function, its acceptance of the input stream is not only local file system, or network files, the following describes one of the ways:
Copy Code code as follows:

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;

How does the MySQL database operate in 14,php?
To facilitate beginners to get started with MySQL operation, I introduce some common operations:
1 database connection and shutdown
Copy Code code 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 the specific operation of the database, the following example no longer write the database connection and shutdown operation
Mysql_close ($link);

2 Insert new data into the table
mysql_query (INSERT INTO mytable (id,name) VALUES ('. $id. "', '". $name. ")");
The above is to insert a piece of data into the ID and name field of the MyTable table.
3 Query the data from the table
$rs =mysql_query ("SELECT * FROM MyTable mt where ' 001 '");
4 Delete data from the table
$rs =mysql_query ("Delete from mytable mt where ' 001");
5 for complex queries, such as the SELECT clause, mysql3.22 the following version is not supported, so many times PHP will not be able to write complex SQL results, it is not PHP's fault, but the version of MySQL is lower.
6 for the result set returned by SELECT, you can do the following:
For the return of a result, you can do the following:
Copy Code code as follows:

$row =mysql_fetch_object ($RS);
$id = $row->id;//id is the name of the field or the alias of the field to the same
$title = $row->title;
$asker = $row->asker;

For multiple results to be returned, you can do the following:
Copy Code code as follows:

while ($row =mysql_fetch_object ($rs)) {
$id = $row->id;
$title = $row->title;
$asker = $row->asker;

Of course, there are methods to make the returned results an array, access can also be based on the position of the field index value access, this can be queried related manuals, not introduced.
15, if you use the HTML online editor in your project, then FCKeditor may be a better choice, FCKeditor can go online to download, download a lot of places, I introduce the way to call:
First, place the FCKeditor directory in the root directory of the site, assuming that you want to refer to FCKeditor in the edit.php in the/modules/cms/directory of the site's root directory, the code is as follows:
Copy Code code as follows:

$sBasePath = ". /.. /fckeditor/";//fckeditor is the FCKeditor directory.
$oFCKeditor = new FCKeditor (' content ');
$oFCKeditor->basepath= $sBasePath;
$oFCKeditor->value= "";
$oFCKeditor->width= "666px";
$oFCKeditor->height= "300px"
<?php $oFCKeditor->create ();? >

16, how to store data in session?
First to start the session mechanism, in addition to the Apache itself to do a certain set, in the use of the session of the PHP page, first call the Session_Start () method, indicating the use of session on this page. The specific way to store data in session is as follows:
Copy Code code as follows:

Session_Start ();
$username = "admin";
Session_register ("username");
So on the other pages, you want to get the username in the session, as follows:
$username =$_session["username"];

Similarly, to determine whether the currently visited user has logged in can also be done in the way above: when the user login, in the session to register the user name, in need of Session control PHP page to add a judgment, for example:
Copy Code code as follows:

if (!session_is_registered ("username")) {
Header ("Location:login.php");

The above is accomplished by judging whether the username variable is registered in the session.
How do you define a class and its member properties and operations in 17,php, and how do you call?
To give an example directly, you should be able to illustrate the above question:
Define a string processing tool class: StringUtils
Copy Code code as follows:

Class stringutils{
function StringUtils () {
function GetLength ($STR) {
return strlen ($STR);

Called in the PHP page as follows:
Copy Code code as follows:

Include ' classes/com/xxx/stringutils.php ';
$length =stringutils::getlength ("ABCDE");
$instance =new StringUtils;
$length = $instance->getlength ("ABCDE");

For a class method, there are generally two ways of calling, one being called as a static method, by:: A connector, one called as an instance method, through a-> connector. Although calls can be invoked in two ways, but the actual method of a class is a static method, which is often logically defined, so each method is often invoked only in some way, such as a method in a service class, which should basically be an instance method, and a method in a tool class, Basically, there are class methods or static methods, such as:
Copy Code code 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, it is meaningful to invoke $this in an instance method.
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 a mandatory type definition of a variable, for example:
$username = "admin";
$length = 0;
$obj =new MyClass;
Many times, you need to convert a string variable to an int variable, or VICE-VERSA, and so on, how do you operate? You can actually use the Settype method, which can specify the type of the variable, and the signature is as follows:
Settype (Var,type)
The values of the types are Boolean (bool), integer (int), float, string, array, object, NULL
For example:
$state = "0";
Settype ($state, "int");
if ($state ==0) {
19, how do I put an array in reverse order?
Implemented through the Array_reverse method, for example:
Copy Code code as follows:

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

20, in PHP How to put a time to the correct display?
In PHP, the time () method is used to return the number of seconds from the Unix epoch (GMT January 1, 1970 00:00:00) 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 geography, but we often don't get the right time to introduce you to a different way of solving it through JavaScript and PHP, for example:
Copy Code code as follows:

var time= "<?php Echo 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 a parameter to the Date object, and then through the JavaScript open source class Library Jsjava processing.
21,php is a very popular language today, and so far has formed a large number of function libraries, such as processing strings, math, XML, file, SOAP, network, etc., but in terms of object-oriented, there is a certain degree of lack of, However, it is not to say that object-oriented can be regarded as a language, however, in the actual web site or project development, just a large number of libraries sometimes feel not special convenience, especially in some cases, the business needs us to abstract the architecture level and the individual objects, This is a good time to define a suitable business class library, after all, when we are facing higher levels of business development, we need to encapsulate higher levels, then the class and objects are on the agenda, but the current use of PHP functions, the feeling is very convenient, and very powerful, This makes me a little bit like Java, such as object-oriented language, what logic is to rely on a large number of classes to achieve, it seems that language needs to learn from each other, rather than attack each other, solve problems to promote the industry and social development is the most fundamental.
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.