Home Backend Development PHP Tutorial Learn php in ten days (1)_PHP tutorial

Learn php in ten days (1)_PHP tutorial

Jul 21, 2016 pm 04:09 PM
php tutorial

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 <%, PHP can be . 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 语法注释 
?>
Copy after login

4. Variable type:

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

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; 
?>
Copy after login

in ASP, this also leads to two questions. First, the output statement in PHP is echo. Second, it is similar to <%=variable%> in ASP. In PHP, Can.
Logical operations:
Symbol Meaning < less than > greater than <= less than or equal to >= 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 "哈哈" ; 
?>
Copy after login

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>" ; 
} 
?>
Copy after login

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>"; 
} 
?>
Copy after login

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 小" ; 
} 
?>
Copy after login

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>" ; 
} 
?>
Copy after login

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; 
} 
?>
Copy after login

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


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/314614.htmlTechArticle以前写了十天学会ASP,十天学会ASP.NET什么的,现在想想再写个PHP吧,也算比较全了。 PHP的调试方法我这里就不说了,外面很多文章都有介...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles