Home  >  Article  >  Backend Development  >  Learn php in ten days (1)_PHP tutorial

Learn php in ten days (1)_PHP tutorial

PHPz
PHPzOriginal
2016-07-21 16:09:111997browse

I used to write to learn ASP in ten days, learn ASP.NET in ten days, etc. Now I think about writing PHP, which is more comprehensive. I won’t go into details about PHP debugging methods here. Many articles outside have introduced them, and there are many different combinations. For the time being, I am using Apache web server and MY SQL as the WEB server and database, and I am doing the program in the environment of php-4.3.3. Of course, PHPMYADMIN is indispensable for simple construction and access to view the database.
As for form design, I don’t want to say more here. It has already been introduced in "Learning ASP in Ten Days".
The following is a brief introduction to the syntax of PHP.
1. Embedding method:
Similar to ASP's 6ee964644cafa0573d7a72bf0d534c1d. Of course, you can also specify it yourself.
2. Reference files:
There are two ways to reference files: require and include.
Require is used like require("MyRequireFile.php"); . This function is usually placed at the front of the PHP program. Before the PHP program is executed, it will first read in the file specified by require and make it a part of the PHP program web page. Commonly used functions can also be introduced into web pages in this way.
include is used like include("MyIncludeFile.php"); . This function is generally placed in the processing part of flow control. The PHP program webpage only reads the include file when it reads it. In this way, the process of program execution can be simplified.
3. Annotation method:

<?php 
echo "这是第一种例子。\n" ; // 本例是 C++ 语法的注释 
/* 本例采用多行的 
注释方式 */ 
echo "这是第二种例子。\n" ; 
echo "这是第三种例子。\n" ; # 本例使用 UNIX Shell 语法注释 
?>

4. Variable type:

$mystring = "我是字符串" ; 
$NewLine = "换行了\n" ; 
$int1 = 38 ; 
$float1 = 1.732 ; 
$float2 = 1.4E+2 ; 
$MyArray1 = array( "子" , "丑" , "寅" , "卯" );

Two questions arise here. First, PHP variables start with $. The second PHP statement ends with;, which may not suit ASP programmers. These two omissions are where most errors in the program lie.
5. Operation symbols:
Mathematical operations: symbol meaning + Addition - Subtraction * Multiplication / Division operation % Take the remainder ++ Accumulate -- Decrement
String operation: There is only one operator symbol for
, which is the English period. It can concatenate strings into new merged strings. Similar to &

<?
$a = "PHP 4" ; 
$b = "功能强大" ; 
echo $a.$b; 
?>

in ASP, this also leads to two questions. First, the output statement in PHP is echo. Second, it is similar to 6585de3afd64c80af2cd10cad4828737 in ASP. In PHP, Canffe1ab6959be9ec62a9e1e6bf4b07ebf.
Logical operations:
Symbol Meaning f29579fdc0048b979ae432c521b89148 greater than 57262dc4ef08ba4f5f2f784d0feb8a53= greater than or equal to == equal to != not equal to && And and And
Or (Or) or Or xor Xor ! Not That’s it for today, let’s talk about process control tomorrow.
The next day Learning purpose: Master the process control of PHP
1. If..else loop has three structures The first is to only use the if condition and treat it as a simple judgment. Interpreted as "what to do if something happens". The syntax is as follows: if (expr) { statement } Among them, expr is the condition for judgment, and logical operation symbols are usually used as the condition for judgment. The statement is the execution part of the program that meets the conditions. If the program has only one line, the curly brackets {} can be omitted. Example: This example omits the curly braces.

 <?php 
if ($state==1)echo "哈哈" ; 
?>

Special attention here is that the judgment of equality is == instead of =. ASP programmers may often make this mistake, = is assignment. Example: The execution part of this example has three lines, and the curly brackets cannot be omitted.

<?php 
if ($state==1) { 
echo "哈哈 ; 
echo "<br>" ; 
} 
?>

The second type is that in addition to if, an else condition is added, which can be interpreted as "what to do if something happens, or how to solve it otherwise". The syntax is as follows if (expr) { statement1 } else { statement2 } Example: Modify the above example into a more complete process. Since there is only one line of instructions for executing else, there is no need to add braces.

<?php 
if ($state==1) { 
echo "哈哈" ; 
echo "<br>";
} 
else{
echo "呵呵";
echo "<br>"; 
} 
?>

The third type is the recursive if..else loop, which is usually used in various decision-making judgments. It combines several if..else statements for processing. Look directly at the example below

<?php 
if ( $a > $b ) { 
echo "a 比 b 大" ; 
} elseif ( $a == $b ) { 
echo "a 等于 b" ; 
} else { 
echo "a 比 b 小" ; 
} 
?>

The above example only uses a two-level if..else loop to compare the two variables a and b. When actually using this kind of recursive if..else loop, please use it with caution, because too many levels of loops can easily cause problems with the design logic, or missing braces, etc., can cause inexplicable problems in the program.
2. There is only one type of for loop with no changes. 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, and logical operators are usually used as the condition for judgment. expr3 is the part to be executed after statement is executed. It is used to change the conditions for the next loop judgment, such as adding one, etc. The statement is the execution part of the program that meets the conditions. If the program has only one line, the curly brackets {} can be omitted. The following example is written using a for loop.

<?php 
for ( $i = 1 ; $i <= 10 ; $i ++) { 
echo "这是第".$i."次循环<br>" ; 
} 
?>

3、 switch 循环,通常处理复合式的条件判断,每个子条件,都是 case 指令部分。在实作上若使用许多类似的 if 指令,可以将它综合成 switch 循环。 语法如下 switch (expr) { case expr1: statement1; break; case expr2: statement2; break; default: statementN; break; } 其中的 expr 条件,通常为变量名称。而 case 后的 exprN,通常表示变量值。冒号后则为符合该条件要执行的部分。注意要用 break 跳离循环。 

<?php 
switch ( date ( "D" )) { 
case "Mon" : 
echo "今天星期一" ; 
break; 
case "Tue" : 
echo "今天星期二" ; 
break; 
case "Wed" : 
echo "今天星期三" ; 
break; 
case "Thu" : 
echo "今天星期四" ; 
break; 
case "Fri" : 
echo "今天星期五" ; 
break; 
default: 
echo "今天放假" ; 
break; 
} 
?>

这里需要注意的是break;别遗漏了,default,省略是可以的。
很明显的,上述的例子用 if 循环就很麻烦了。当然在设计时,要将出现机率最大的条件放在最前面,最少出现的条件放在最后面,可以增加程序的执行效率。上例由于每天出现的机率相同,所以不用注意条件的顺序。 今天就说到这里,明天开始说数据库的使用。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/314614.htmlTechArticle以前写了十天学会ASP,十天学会ASP.NET什么的,现在想想再写个PHP吧,也算比较全了。 PHP的调试方法我这里就不说了,外面很多文章都有介...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn