10 Day Learning PHP (1) _php Foundation

Source: Internet
Author: User
Tags logical operators switch loop

Previously written 10 days to learn the ASP, 10 days to learn asp.net what, now think about writing a PHP bar, also calculate more complete. PHP Debugging methods I do not say here, outside a lot of articles are introduced, there are many different combinations. I'm here for the moment with the Apache Web server and my SQL as Web servers and databases, under the php-4.3.3 environment. Of course, simple build and access to view the database phpMyAdmin must be minimal.

As for the form design, I do not want to say more, in the "Ten Days of the Institute of ASP" has been introduced.

The following is a brief introduction to the syntax of PHP.

1. Embedding Method:
Similar to the ASP <%,php can be <?php or yes, the end symbol is, of course, you can also specify.

2. Reference documents:
There are two ways to refer to a file: Require and include.
Require use methods such as require ("myrequirefile.php"); This function is usually placed at the top of the PHP program, PHP program before executing, will first read into the require designated to introduce the file, so that it becomes part of the PHP Program Web page. A commonly used function can be introduced into a Web page in this way.
Include use methods such as include ("myincludefile.php");. This function is typically placed in the processing section of the Process Control. The PHP Program page reads the included file when it is read. This way, you can simplify the process of executing a program.

3, annotation method:
<?php
echo "This is the first example. \ n "; This example is a comment for C + + syntax
/* This example uses multiple lines of
Comment Mode * *
echo "This is the second example. \ n ";
echo "This is the third example. \ n "; # This example uses the UNIX Shell syntax annotation
?>

4. Variable type:
$mystring = "I am a string";
$NewLine = "line changed \ n";
$int 1 = 38;
$float 1 = 1.732;
$float 2 = 1.4E+2;
$MyArray 1 = Array ("Zi", "ugly", "Yin", "Mao");

This leads to two questions: first PHP variables begin with $, and the second PHP statement ends with a possible ASP programmer that doesn't fit. These two omissions are also the most error in the procedure.

5. Operation Symbol:

Mathematical operations: Symbolic meaning + addition operation-subtraction operation * Multiplication operation/division operation% + + cumulative--decrement
String Operations:
There is only one operation symbol, the period of English. It can concatenate strings into a new merged string. Similar to & in ASP
?
$a = "PHP 4";
$b = "strong function";
echo $a. $b;
?>
Here also leads to two problems, first PHP output statement is echo, the second similar to the <%= variable%>,php in ASP can also <?= variable?>.

Logical operations:
Symbolic meaning < less than > greater than <= is less than or equal to >= greater than or equal to = = equals!= is not equal to && and (and) and and (and)
or (or) or or (or) XOR exclusive OR (XOR)! No (not) today, let's talk about Process control tomorrow.
The second day of Learning Purpose: Mastering PHP Process Control

1, If ... Else loop there are three kinds of structures the first is to use the IF condition as a simple judgment. Explained as "What to do if something happens". The syntax is as follows: if (expr) {statement} The expr in which the condition is judged, is usually a condition judged by the logical operational notation. and statement for the implementation of the conditions of the program, if the program has only one line, you can omit the curly braces {}. Example: This example omits curly braces. <?php
if ($state ==1) echo "haha";
?>

It is particularly noteworthy here that the decision whether equality is = = rather than =,asp programmer may make this error, = is assignment. Example: The execution section of this example has three lines and cannot omit curly braces. <?php
if ($state ==1) {
echo "haha;
echo "<br>";
}
?> Two is the condition of addition to if, plus else, can be explained as "if something happens, how to deal with it, how to solve". The syntax is as follows if (expr) {statement1} else {Statement2} example: The above example is modified to more complete processing. Where else is due to only one row of instructions, so no curly braces are added. <?php
if ($state ==1) {
echo "haha";
echo "<br>";
}
else{
echo "hehe";
echo "<br>";
}
The third type of?> is recursive if. else loops, usually used in a variety of decision judgments. It will be several if.. Else take to combine the use of processing. Look directly at the example below <?php
if ($a > $b) {
echo "A greater than B";
} elseif ($a = = $b) {
echo "a equals B";
} else {
echo "A is smaller than B";
}
The?> example uses only the two-level if. else loop, to compare A and b two variables. You actually want to use this recursive if. else loop, please use caution, because too many layers of the loop can make the logic of the design problem, or less dozen curly braces, will cause the program to appear inexplicable problems.

2, for loop on pure only one, unchanged, its syntax is as follows for (EXPR1; expr2; expr3) {statement} where EXPR1 is the initial value of the condition. EXPR2 is the condition of judgment, which is usually judged by the logic operation symbol (logical operators). EXPR3 the part to be executed after the execution of statement to change the condition for the next round of judgment, such as add one. Wait a minute. and statement for the implementation of the conditions of the program, if the program has only one line, you can omit the curly braces {}. The following example is an example written in the For loop. <?php
for ($i = 1; $i <= $i + +) {
echo "This is the first". $i. " Secondary cycle <br> ";
}
?>

3, switch cycle, usually deal with the conditions of the compound judgment, each child condition, is the case instruction part. If you use many similar if instructions on the implementation, you can synthesize it into a switch loop. The syntax is as follows switch (expr) {case expr1:statement1, break, Case Expr2:statement2, break, DEFAULT:STATEMENTN; condition, usually the variable name. The ExprN after the case usually represents the value of the variable. The colon is followed by the part that meets the condition. Notice that you want to jump off the loop with a break. <?php
Switch (date ("D")) {
Case "Mon":
echo "Today Monday";
Break
Case "Tue":
echo "Today Tuesday";
Break
Case "Wed":
echo "Today Wednesday";
Break
Case "Thu":
echo "Today Thursday";
Break
Case "Fri":
echo "Today Friday";
Break
Default
echo "Today's Holiday";
Break
}
?>

Here is the need to pay attention to the break; do not omit, default, omit is OK.
Obviously, the example above is cumbersome with the IF loop. Of course, in the design, the most likely to have the highest probability of the first, the least occurrence of the conditions on the last side, you can increase the efficiency of the implementation of the program. The previous example has the same probability of appearing every day, so don't pay attention to the order of the conditions. That's it for today, starting tomorrow with the use of the database.

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.