Ten days to learn php

Source: Internet
Author: User
Ten days to learn php-general Linux technology-Linux programming and kernel information, the following is a detailed description. Ten days to learn php (1) I wrote ten days to learn ASP, and ten days to learn ASP. NET or something. Now I want to write another PHP, which is quite complete. I will not talk about the PHP debugging method here. Many articles have been introduced and there are many different combinations. I am currently using Apache web server and my SQL as WEB server and database, in the php-4.3.3 environment to do the program. Of course, it is essential to simply build and access the PHPMYADMIN database. As for form design, I don't want to talk much about it here. I have already introduced it in "ten days learning ASP. The following describes the PHP syntax. 1. Embedding Method: similar to ASP's <%, PHP can be Of course, you can also specify it yourself. 2. File Reference: There are two methods to reference a file: require and include. The use of require is as follows: require ("MyRequireFile. php ");. This function is usually placed at the beginning of the PHP program. Before the PHP program is executed, it will first read the file specified by require to make it a part of the PHP program webpage. This method can also be used to introduce common functions into webpages. Include usage methods such as include ("mydomaindefile. php ");. This function is generally placed in the Process of process control. The PHP program webpage reads the include file. In this way, you can simplify the process during program execution. 3. annotation method: 4. Variable type: $ mystring = "I Am a string"; $ NewLine = "wrap \ n"; $ int1 = 38; $ float1 = 1.732; $ float2 = 1.4E + 2; $ MyArray1 = array ("child", "ugly", "Yin", "Mao"); two problems are raised here, first, PHP variables start with $, and the second PHP statement ends with;, which may be unsuitable by ASP programmers. These two omissions are also the cause of many program errors. 5. Operator number: mathematical operation: Symbol Meaning + addition operation-subtraction operation * Multiplication operation/division operation % remainder operation + + accumulate -- decrease string operation: there is only one operator number, it is the end of English. It can be used to connect strings and convert them into new merged strings. Similar & Two problems are also raised here. The output statement in PHP is echo, and the second is similar to <% = variable %> in ASP. PHP can also . Logical operation: Symbol Meaning <less than> greater than <= less than or equal to> = greater than or equal to = equal! = Not equal to & And (and) And (And) | Or (or) Or (Or) xor is different or (Xor )! No (Not) Let's talk about it today. Let's talk about process control tomorrow. The next day's learning objective: to master the php process control 1. if... else loop has three structures. The first one is to use the if condition as a simple judgment. It is interpreted as "how to deal with something ". The syntax is as follows: if (expr) {statement} Where expr is the condition for judgment. It is usually determined by a logical operator number. Statement is the execution part of the program that meets the conditions. If the program has only one line, you can omit braces {}. Example: braces are omitted in this example. It is important to note that, to determine whether the equality is equal to = rather than =, ASP programmers may often make this mistake, = is a value assignment. Example: The execution part of this example has three rows and the braces cannot be omitted. ";}?> In addition to the if clause, the first two conditions of else can be interpreted as "what to do if something happens, otherwise how to solve it ". The syntax is as follows: if (expr) {statement1} else {statement2} example: the above example is modified to a more complete processing. Because else only executes one line of commands, no braces are added. ";}Else {echo" Haha "; echo"
";}?> The third type is the recursive if... else loop, which is usually used for multiple decision making decisions. It combines several if... else values for processing. Let's look at the example below. $ B) {echo "a is larger than B";} elseif ($ a = $ B) {echo "a is equal to B ";} else {echo "a is smaller than B" ;}?> In the above example, we only use the layer-2 if .. else loop to compare the two variables a and B. Actually, we need to use this recursion if .. please be careful when using else loops, because too many layers of loops may cause problems in the design logic, or if you leave less braces, the program may encounter inexplicable problems. 2. The for loop has only one type and does not change. Its syntax is as follows: for (expr1; expr2; expr3) {statement} Where expr1 is the initial value of the condition. Expr2 is the condition for judgment. It is usually determined by a logical operator number (logical operators. Expr3 is the part to be executed after statement is executed. It is used to change the condition for the next loop judgment, such as adding one. Statement is the execution part of the program that meets the conditions. If the program has only one line, you can omit braces {}. The following is an example of using for loop writing. ";}?> 3. switch loop, usually processing compound condition judgment. Each sub-condition is part of the case instruction. In practice, if many similar if commands are used, they can be combined into a switch loop. The syntax is as follows: switch (expr) {case expr1: statement1; break; case expr2: statement2; break; default: statementN; break;}. The expr condition is usually the variable name. The exprN after case usually represents the variable value. The part that meets the condition after the colon. Note that you must use the break to skip the cycle. Here, you need to note break; Do not miss it, default. It is acceptable to omit it. Obviously, it is very troublesome to use the if loop in the above example. Of course, in the design, the conditions with the highest probability should be placed at the beginning, and the minimum conditions should be placed at the end, which can increase the execution efficiency of the program. In the previous example, because the probability of occurrence is the same every day, you do not need to pay attention to the order of conditions. Ten days to learn php (2) Day 3Objective: To learn how to build a database in ASP. For an ACCESS database, you can open ACCESS to edit the MDB file. for SQL server, you can open the Enterprise Manager to edit the SQL SERVER database, however, in PHP, the command line editing of my SQL may be troublesome for beginners. It doesn't matter. You can download a PHPMYADMIN and install it. You can establish a database for editing later. The following describes how to use it. After entering phpmyadmin, we first need to create a database, Language (*) Here select Simplified Chinese, then create a new database on the left here fill in the Database Name, click Create. Select the created database from the drop-down list on the left. Create a new table in the following database shop: Name: number of fields: Fill in the table name and roughly the number of fields you think (it doesn't matter if it is not enough or more, can be added later or by default. Then you can create a table. The first column is the name of the field; the second column selects the field type: we commonly use the following: 1) VARCHAR, text type 2) INT, integer type 3) FLOAT, floating point type 4) DATE, DATE type 5) You may ask, where is the automatically added ID? You only need to select the INT type, and select auto_increment in the additional section below. After creating a table, you can see the table you created on the left. After clicking it, you can: 1) view the structure on the right: view and modify the table structure; 2) view on the right: view data in the table 3) Press SQL on the right: run SQL statement 4) insert on the right: Insert a row of records 5) Clear on the right: delete all records in the table 6) to delete a table on the right: An Important Function to delete a table is import and export. When programs and databases are prepared on the local machine, a local image is required on the server, for asp access, you can directly upload the MDB file. for SQL SERVER, you can connect to the remote SERVER for import. In my SQL, you can export all the SQL statements to PHPMYADMIN on the remote server. After creating the database, press SQL and paste all the SQL statements generated at the current level that you just copied. Let's talk about database operations today. Day 4Objective: To learn to connect to the database PHP is simply a function library. A wide range of functions make PHP quite simple in some places. We recommend that you use a PHP function manual. Let me briefly talk about connecting to the MYSQL database. 1. mysql_connect opens the MySQL server connection. Syntax: int mysql_connect (string [hostname] [: port], string [username], string [password]); Return Value: integer this function establishes a connection with the MySQL server. All parameters can be omitted. When you use this function without adding any parameters, the default value of the hostname parameter is localhost, the default value of the username parameter is the owner of the PHP route execution, and the parameter password is a null string (that is, no password ). The hostname parameter can be followed by a colon and a port number, indicating which port is used to connect to MySQL. Of course, when using the database, you can use mysql_close () to turn off the connection earlier to save resources. 2. Select a database from mysql_select_db. Syntax: int mysql_select_db (string database_name, int [link_identifier]); Return Value: integer this function selects the database on the MySQL server for subsequent data query job (query) processing. True is returned for success, and false is returned for failure. The simplest example is: $ conn = mysql_connect ("127.0.0.1", "", ""); mysql_select_db ("shop"); Connect To my SQL database and open the SHOP database. In practical applications, we should strengthen the judgment of point errors. Let's talk about this today. Let's talk about database reading tomorrow. Day 5Objective: to read data first, read two functions: 1. Output a query string using mysql_query. Syntax: int mysql_query (string query, int [link_identifier]); Return Value: integer this function sends the query string for MySQL to perform related processing or execution. If the link_identifier parameter is not specified, the program automatically searches for the recently opened ID. If the query string is UPDATE, INSERT, or DELETE, the return value may be true or false. If the query string is SELECT, a new ID value is returned. If the return value is false, the query string is incorrect, instead of running successfully but without returning values. 2. mysql_fetch_object returns class data. Syntax: object mysql_fetch_object (int result, int [result_typ]); Return Value: This function is used to split the query result into class variables. If the result contains no data, false is returned. Let's look at a simple example: Username ."
";}?> Of course, the table user has a username field, which is similar to <% exec = "select * from user" set rs = server in asp. createobject ("adodb. recordset ") rs. open exec, conn, 1, 1 do while not rs. eof response. write "username:" & rs ("username ")&"
"Rs. movenext loop %> of course, you need to connect to the database first. Generally, we require_once ('conn. php'). In conn. php, the code used to connect to the database is described last time. Two small commands can be used to read data. Today we will talk about how to add, delete, and modify data. Day 6Objective: To learn how to add and delete the modified data mysql_query ($ exec). You can execute all the operations with this statement. The difference is that the SQL statement $ exec is added: $ exec = "insert into tablename (item1, item2) values ('". $ _ POST ['item1']. "',". $ _ POST ['item1']. ")"; delete: $ exec = "delete from tablename where... "; Modify: $ exec =" update tablename set item1 = '". $ _ POST ['item1']. "'where... "; here we will talk about the transfer of form and php variables. If one of If the form is submitted in POST mode, you can use $ _ POST ['item1'] to obtain the variable value for processing the Form file, similarly, $ _ GET ['item1'] is submitted with GET, isn't it easy? But usually $ exec will have a problem, because your SQL statement may be very long, you will miss the. connector, or 'to enclose the character field. Note mysql_query ($ exec); the statement uses echo $ exec; instead of $ exec to output $ exec to check whether the statement is correct. If you do not notice any error in $ exec, you can copy the SQL statement to phpmyadmin to check its error information. Note that we should not use sensitive strings as field names. Otherwise, problems may occur, such as date. Variable naming: Field naming follows a certain rule. Sometimes it is good for you. Beginners cannot ignore its importance.

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.